Unit 3
Unit 3
TREE TRAVERSAL:
Traversal is a process to visit all the nodes of a tree and may print their values too.
Because all nodes are connected via edges (links) we always start from the root (head)
node. we cannot randomly access a node in a tree. There are three ways which we use to
traverse a tree,
● In-order Traversal
● Pre-order Traversal
● Post-order Traversal
Generally, we transverse a tree to search and locate a given item or key in the tree
or to print all the values it contains. Now let us learn about that transverse.
1. INORDER TRAVERSAL
In this traversal method, the left subtree is visited first, then the root and later
the right sub-tree. We should always remember that every node may represent a
subtree itself.
By using the above algorithm, we are going to transverse this tree into In-order.
We start from A, and following in-order traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on until all the nodes are visited. The
output of in-order traversal of this tree will be,
D→B→E→A→F→C→G
In case of binary search trees (BST), Inorder traversal gives nodes in non-
decreasing order. To get nodes of BST in non-increasing order, a variation of
Inorder traversal where Inorder traversal s reversed can be used.
2. PRE-ORDER TRAVERSAL:
In this traversal method, the root node is visited first, then the left subtree and
finally the right subtree.
By using the above algorithm, we are going to transverse this tree into In-order.
A→B→D→E→C→F→G
3. POST-ORDER TRAVERSAL:
In this traversal method, the root node is visited last, hence the name. First, we
traverse the left subtree, then the right subtree and finally the root node.
By using the above algorithm, we are going to transverse this tree into In-order.
EXAMPLE:
BINARY SEARCH TREE REPRESENTATION:
Binary Search Tree is a node-based binary tree data structure which has the
following properties:
● The left subtree of a node contains only nodes with keys lesser than the
node’s key.
● The right subtree of a node contains only nodes with keys greater than
the node’s key.
● The left and right subtree each must also be a binary search tree.
EXAMPLE:
The following tree is a Binary Search Tree. In this tree, left subtree of every node
contains nodes with smaller values and right subtree of every node contains larger values.
SEARCH OPERATION:
In a binary search tree, the search operation is performed with O (log n) time
complexity. The search operation is performed as follows,
● Step 1 - Read the search element from the user.
● Step 2 - Compare the search element with the value of the root node in the tree.
● Step 3 - If both are matched, then display "Given node is found!!!" and terminate
the function
● Step 4 - If both are not matched, then check whether the search element is smaller
or larger than that node value.
● Step 5 - If the search element is smaller, then continue the search process in the
left subtree.
● Step 6- If the search element is larger, then continue the search process in the right
subtree.
● Step 7 - Repeat the same until we find the exact element or until the search
element is compared with the leaf node
● Step 8 - If we reach to the node having the value equal to the search value then
display "Element is found" and terminate the function.
● Step 9 - If we reach to the leaf node and if it is also not matched with the search
element, then display "Element is not found" and terminate the function.
INSERTION OPERATION:
In a binary search tree, the insertion operation is performed with O (log n) time
complexity. In a binary search tree, a new node is always inserted as a leaf node. The
insertion operation is performed as follows,
● Step 1 - Create a newNode with the given value and set its left and right to
NULL.
● Step 2 - Check whether the tree is Empty.
● Step 3 - If the tree is Empty, then set root to newNode.
● Step 4 - If the tree is Not Empty, then check whether the value of newNode is
smaller or larger than the node (here it is root node).
● Step 5 - If newNode is smaller than or equal to the node then move to its left
child. If newNode is larger than the node then moves to its right child.
● Step 6- Repeat the above steps until we reach the leaf node (i.e., reaches to
NULL).
● Step 7 - After reaching the leaf node, insert the new node as a left child if the
newNode is smaller or equal to that leaf node or else insert it as right child.
In the following example, we are inserting some numbers inside the tree:
DELETION OPERATION:
In a binary search tree, the deletion operation is performed with O (log n) time
complexity. Deleting a node from Binary search tree includes the following three cases,
● Step 2 - Delete the node using a free function (If it is a leaf) and terminate the
function.
● Step 2 - If it has only one child then create a link between its parent node and
child node.
● Step 3 - Delete the node using a free function and terminate the function.
● Step 2 - If it has two children, then find the largest node in its left subtree
(OR) the smallest node in its right subtree.
● Step 3 - Swap both deleting node and node which is found in the above step.
● Step 4 - Then check whether deleting node came to case 1 or case 2 or else go
to step 2
● Step 5 - If it comes to case 1, then delete using case 1 logic.
● Step 6- If it comes to case 2, then delete using case 2 logic.
● Step 7 - Repeat the same process until the node is deleted from the tree.
struct Node{
int data ;
Node *left ;
Node *right ;
bool rightThread ;
}
// double threaded
struct Node{
int data ;
Node *left ;
Node *right ;
bool leftThread ;
bool rightThread ;
}
What Is the Significance of a Bool Variable in a Structure?
Consider a single threaded binary tree. Any node in the binary tree can have its right pointer
either to a child or the next successor node (which might not be the child node).
Hence we need a boolean variable that tells us whether the right pointer is pointing to a child or
the next successor node
Applications of Trees
Hierarchical Structure: Trees are used to model hierarchical structures, such as the
file system in a computer or the organizational chart in a company. The tree
structure allows for a natural representation of parent-child relationships, making it
easy to understand and visualize the data.
Searching Efficiency: Trees provide an efficient way to search for data. For
example, in a binary search tree, searching for a value takes time proportional to the
logarithm of the number of elements, which is much faster than searching in a linear
data structure like an array or a linked list.
Sorting: Trees can be used to sort data efficiently. For example, in a self-balancing
binary search tree, the data is automatically sorted as it is inserted into the tree,
making it easy to find the minimum, maximum, and other values in the tree.
Dynamic Data: Trees are dynamic data structures, which means that they can grow
and shrink as needed. This makes them well-suited for applications where the data
changes frequently, such as in real-time systems.
Efficient Insertion and Deletion: Trees provide efficient algorithms for inserting
and deleting data, which is important in many applications where data needs to be
added or removed frequently.
Easy to Implement: Trees are relatively easy to implement, especially when
compared to other data structures like graphs. This makes them a popular choice for
many programming projects.
Applications of BST
● BSTs are used for a lot of applications due to its ordered structure.
● BSTs are used for indexing and multi-level indexing.
● They are also helpful to implement various searching algorithms.
● It is helpful in maintaining a sorted stream of data.
● TreeMap and TreeSet data structures are internally implemented using self-balancing
BSTs
AVL Tree
● AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL
in honour of its inventors.
● AVL Tree can be defined as height balanced binary search tree in which each node is associated
with a balance factor which is calculated by subtracting the height of its right sub-tree from that
of its left sub-tree.
● Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree
will be unbalanced and need to be balanced.
Left-Right Rotation:
A left-right rotation is a combination in which first left rotation takes place after that right
rotation executes.
Right-Left Rotation:
A right-left rotation is a combination in which first right rotation takes place after that left
rotation executes.
Applications of AVL Tree:
1. It is used to index huge records in a database and also to efficiently search in that.
2. For all types of in-memory collections, including sets and dictionaries, AVL Trees are
used.
3. Database applications, where insertions and deletions are less common but frequent data
lookups are necessary
4. Software that needs optimized search.
5. It is applied in corporate areas and storyline games.
3. Due to its rather strict balance, AVL trees provide complicated insertion and removal
operations as more rotations are performed.
Min-Heap − Where the value of the root node is less than or equal to
either of its children. A[Parent(i)] <= A[i]
Max-Heap − Where the value of the root node is greater than or equal
to either of its children. A[Parent(i)] >= A[i]
Both trees are constructed using the same input and order of arrival.