0% found this document useful (0 votes)
22 views36 pages

Binary Tree and Search Algorithms Guide

The document discusses various tree traversal methods including in-order, pre-order, post-order, and level-order, each with distinct node visiting sequences. It also covers linear and binary search algorithms, highlighting their advantages and disadvantages, as well as their applications in different data structures. Additionally, the document explains the bubble sort algorithm, noting its simplicity and limitations for large datasets.

Uploaded by

maheshdarji20060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views36 pages

Binary Tree and Search Algorithms Guide

The document discusses various tree traversal methods including in-order, pre-order, post-order, and level-order, each with distinct node visiting sequences. It also covers linear and binary search algorithms, highlighting their advantages and disadvantages, as well as their applications in different data structures. Additionally, the document explains the bubble sort algorithm, noting its simplicity and limitations for large datasets.

Uploaded by

maheshdarji20060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Binary Tree Traversals

• Binary tree traversal refers to the process of visiting each node in the
tree exactly once in a systematic way.
• There are four main types of binary tree traversals:
• in-order, pre-order, post-order, and level-order.
• Each traversal method visits nodes in a different order.
Tree Traversal
• Tree Traversal refers to the process of visiting or accessing each node
of the tree exactly once in a certain order.
• Tree traversal algorithms help us visit and process all the nodes of
the tree.
• Since a tree is not a linear data structure, there can be multiple
choices for the next node to be visited.
• Hence we have many ways to traverse a tree.
1. In-order Traversal (Left, Root, Right)

In in-order traversal, the nodes are recursively visited in this order: left
subtree, root node, and then the right subtree.

• In-order Traversal: 4, 2, 5, 1, 3
Steps:
• Visit the left subtree: 4
• Visit the root: 2
• Visit the right subtree: 5
• Visit the root: 1
• Visit the right subtree: 3
2. Pre-order Traversal (Root, Left, Right)

In pre-order traversal, the nodes are recursively visited in this order: root node,
left subtree, and then the right subtree.
• Pre-order Traversal: 1, 2, 4, 5, 3
Steps:
• Visit the root: 1
• Visit the left subtree: 2
• Visit the left subtree: 4
• Visit the right subtree: 5
• Visit the right subtree: 3
3. Post-order Traversal (Left, Right, Root)

In post-order traversal, the nodes are recursively visited in this order:


left subtree, right subtree, and then the root node.

• Post-order Traversal: 4, 5, 2, 3, 1
Steps:
• Visit the left subtree: 4
• Visit the right subtree: 5
• Visit the root: 2
• Visit the right subtree: 3
• Visit the root: 1
4. Level-order Traversal (Breadth-First)

In level-order traversal, the nodes are visited level by level from left to
right. This traversal uses a queue data structure to keep track of nodes.
• Level-order Traversal: 1, 2, 3, 4, 5
Steps:
• Visit the root: 1
• Visit the nodes at the next level:
2, 3
• Visit the nodes at the next level:
4, 5
LINEAR SEARCH
• Suppose we are searching a target element in an array. In linear
search we begin with the first position of the array, and traverse the
whole array in order to find the target element. If we find the target
element we return the index of the element.
• Otherwise, we will move to the next position. If we arrive at the last
position of an array and still can not find the target, we return -1. This
is called the Linear search or Sequential search.
Example of Linear Search

• Imagine you have a list of numbers: [5, 8, 1, 3, 7, 9, 2] and you want


to find the position of the number 7.
• Step 1: Start with the first element, 5. Compare it with 7. Since 5 is
not 7, move to the next element.
• Step 2: Compare the next element, 8, with 7. Again, it's not a match,
so move to the next element.
• Step 3: Continue this process with 1, 3, until you reach 7.
• Step 4: When you reach 7, it's a match! The algorithm returns the
position of 7 in the list, which is 4 (if you start counting from 0).
Applications of Linear Search Algorithm:

• Unsorted Lists: When we have an unsorted array or list, linear search


is most commonly used to find any element in the collection.
• Small Data Sets: Linear Search is preferred over binary search when
we have small data sets with
• Searching Linked Lists: In linked list implementations, linear search is
commonly used to find elements within the list. Each node is checked
sequentially until the desired element is found.
• Simple Implementation: Linear Search is much easier to understand
and implement as compared to Binary Search or Ternary Search.
Advantages of Linear Search Algorithm:

• Linear search can be used irrespective of whether the array is sorted


or not. It can be used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.

Disadvantages of Linear Search Algorithm:


• Linear search has a time complexity of O(N), which in turn makes it
slow for large datasets.
• Not suitable for large arrays.
When to use Linear Search Algorithm?

• When we are dealing with a small dataset.


• When you are searching for a dataset stored in contiguous memory.
BINARY SEARCH
• Binary Search is a more optimized form of searching algorithm.
It cuts down the search space in halves achieving logarithmic time
complexity on a sorted data.
• We take two extremes lower bound and upper bound and compare
our target element with the middle element.
• In the process we discard one half where we are sure our target
element can not be found and update our lower and upper bound
accordingly.
What is Binary Search?

• Binary search is an efficient search algorithm used to find the position


of a target element within a sorted list or array. Unlike linear search,
which checks each element one by one, binary search works by
repeatedly dividing the search interval in half.
• The key requirement for binary search is that the list must be sorted.
This allows the algorithm to eliminate half of the remaining elements
in each step, making it much faster than linear search for large
datasets.
• The time complexity of binary search is O(log n), where n is the
number of elements in the list.
How Binary Search Works?

• Binary search operates by following these steps:


Step 1: Initialize Search Range
• Start by setting two pointers: low at the beginning of the list and high at the end.
Step 2: Find the Middle Element
• Calculate the middle index of the current search range: middle = (low + high) // 2.
• Compare the middle element with the target element.
Step 3: Compare and Narrow the Search Range
• If the middle element matches the target, return the middle index.
• If the target element is less than the middle element, narrow the search to the
left half by setting high = middle - 1.
• If the target element is greater than the middle element, narrow the search to
the right half by setting low = middle + 1.
• Step 4: Repeat
• Repeat steps 2 and 3 until the target element is found or the search
range becomes invalid (i.e., low > high).

• Step 5: Target Not Found


• If the target element is not found after the search range is invalid,
return a signal (e.g., -1 or None) indicating that the target is not
present in the list.
Advantages of Binary Search

• Time complexity is O(log n), making it much faster than Linear Search
for large datasets, especially when the data is sorted.
• Takes full advantage of sorted data, halving the search space with
each step, which drastically reduces the number of comparisons
needed.
• Performs well on large datasets, maintaining efficiency even as the
dataset size grows.
Disadvantages of Binary Search

• The list or array must be sorted before applying Binary Search, which
adds an extra step and can be time-consuming if the data isn’t already
sorted.
• More complex to implement and understand than Linear Search,
especially when dealing with recursive implementations.
• Cannot be applied directly to unsorted data, limiting its versatility in
scenarios where the dataset is frequently updated or unsorted.
• The recursive version of Binary Search has O(log n) space complexity
due to the stack space used by recursive calls.
Bubble Sort Algorithm

• Bubble Sort is the simplest sorting algorithm that works by repeatedly


swapping the adjacent elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its average and worst-
case time complexity are quite high.
• We sort the array using multiple passes. After the first pass, the
maximum element goes to end (its correct position). Same way, after
second pass, the second largest element goes to second last position
and so on.
• In every pass, we process only those elements that have already not
moved to correct position. After k passes, the largest k elements must
have been moved to the last k positions.
• In a pass, we consider remaining elements and compare all adjacent
and swap if larger element is before a smaller element. If we keep
doing this, we get the largest (among the remaining elements) at its
correct position.
Advantages of Bubble Sort:

• Bubble sort is easy to understand and implement.


• It does not require any additional memory space.
• It is a stable sorting algorithm, meaning that elements with the same
key value maintain their relative order in the sorted output.
Disadvantages of Bubble Sort:

• Bubble sort has a time complexity of O(n2) which makes it very slow
for large data sets.
• Bubble sort has almost no or limited real world applications. It is
mostly used in academics to teach different ways of sorting.
References
• [Link]
and-postorder/
• [Link]
• [Link]
tm
• [Link]
• [Link]
• [Link]
References
• [Link]
• [Link]
• [Link]

You might also like