0% found this document useful (0 votes)
49 views22 pages

Unit 3

This document discusses different tree traversal methods including in-order, pre-order and post-order traversal. It also covers binary search tree operations like search, insertion and deletion and provides examples of each.

Uploaded by

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

Unit 3

This document discusses different tree traversal methods including in-order, pre-order and post-order traversal. It also covers binary search tree operations like search, insertion and deletion and provides examples of each.

Uploaded by

Rohan Ajee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

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.

Following is the algorithm for traversing the tree,

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Visit the root node.
Step 3 − Recursively traverse right subtree.

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.

Following is the algorithm for traversing the tree,

Until all nodes are traversed −


Step 1 − Visit the root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

By using the above algorithm, we are going to transverse this tree into In-order.

We start from A, and following pre-order traversal, we first


visit A itself and then move to its left subtree B. B is also
traversed pre-order. The process goes on until all the nodes
are visited. The output of pre-order traversal of this tree
will be −

A→B→D→E→C→F→G

● Pre-order traversal is used to create a copy of the tree.


● Pre-order traversal is also used to get prefix expression on of an expression tree.

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.

Following is the algorithm for transversing the tree,

Until all nodes are traversed −


Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit the root node.

By using the above algorithm, we are going to transverse this tree into In-order.

We start from A, and following Post-order traversal, we first


visit the left subtree B. B is also traversed post-order. The
process goes on until all the nodes are visited. The output of
post-order traversal of this tree will be −
D→E→B→F→G→C→A
● Post-order traversal is used to delete the tree.
● Post-order traversal is also useful to get the postfix expression of an expression
tree.

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.

● There must be no duplicate nodes.


OPERATIONS:
In a binary tree, every node can have a maximum of two children but there is no
need to maintain the order of nodes basing on their values. In a binary tree, the elements
are arranged in the order they arrive at the tree from top to bottom and left to right.
A binary tree has the following time complexities...
1. Search Operation - O(n)
2. Insertion Operation - O(1)
3. Deletion Operation - O(n)
To enhance the performance of the binary tree, we use a special type of binary tree
known as the Binary Search Tree. Binary search tree mainly focuses on the search
operation in a binary tree. Binary search tree can be defined as follows...
In a binary search tree, all the nodes in the left subtree of any node contain smaller values
and all the nodes in the right subtree of any node contains larger values as shown in the
following figure...

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,

● Case 1: Deleting a Leaf node (A node with no children)


● Case 2: Deleting a node with one child
● Case 3: Deleting a node with two children

Case 1: Deleting a leaf node:


We use the following steps to delete a leaf node from BST...
● Step 1 - Find the node to be deleted using search operation

● Step 2 - Delete the node using a free function (If it is a leaf) and terminate the
function.

Case 2: Deleting a node with one child:


We use the following steps to delete a node with one child from BST...
● Step 1 - Find the node to be deleted using the search operation

● 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.

Case 3: Deleting a node with two children


We use the following steps to delete a node with two children from BST,
● Step 1 - Find the node to be deleted using the search operation

● 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.

In the following diagram, we considered the greatest value in-order successor,

Here we considered the least value in-order successor,

What Is a Threaded Binary Tree?


In a threaded binary tree, either of the NULL pointers of leaf nodes is made to point to their
successor or predecessor nodes

What is the need for a Threaded Binary Tree?


● In a regular tree, the space complexity for insertion/ deletion can range from logarithmic
to linear time. We get log time in a balanced binary tree and linear time is the worst case
in an unbalanced tree.
● In applications where we have a constraint of space, or even when there is a use case of
storing large trees in memory, we might need to save up on the extra space invested in the
recursion depth. A threaded binary tree is advantageous in such scenarios where space
usage is critical
Types of Threaded Binary Trees
Following are Types of Threaded Binary Trees:

o One-way threaded Binary Tree


o Two-way threaded Binary Tree

One-way threaded Binary trees:

In one-way threaded binary trees, a thread will appear either in


the right or left link field of a node. If it appears in the right link
field of a node then it will point to the next node that will appear
on performing in order traversal. Such trees are called Right
threaded binary trees. If thread appears in the left field of a node
then it will point to the nodes inorder predecessor. Such trees are
called Left threaded binary trees.

Two-way threaded Binary Trees:


in two-way threaded Binary trees, the right link field of a node
containing NULL values is replaced by a thread that points to
nodes inorder successor and left field of a node containing NULL
values is replaced by a thread that points to nodes inorder
predecessor.
Advantages of Threaded Binary Tree
 In this Tree it enables linear traversal of elements.
 It eliminates the use of stack as it perform linear traversal, so save memory.
 Enables to find parent node without explicit use of parent pointer

Node Structure in a Threaded Binary Tree


// single threaded

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

Inorder Traversal Using Threads


Let us see how inorder traversal actually works in a single threaded binary tree now

The following steps take place in the diagram above:


1. We go to the left most node 1
2. Node 1 has boolean rightThread as true, hence next we visit its inOrder successor node 2
3. Node 2 has a right child, so we visit Node 3 next
4. Node 3 does not have a right child, ie the rightThread boolean is true, so we visit Node 4
next
5. Node 4 has a right child so we visit Node 5 and finish traversal

Time and Space Complexity of Operations


● The time complexity for insertion, deletion, and searching in a threaded binary tree is the
same as that of a BST, as we need to perform operations maximum up to the depth of the
tree. The depth of a threaded BST is also log(n) where n is the total number of nodes in
the tree
● Hence all operations take up O(log(n)) time complexity
● However, since we do not use any extra space for any of the operations, the auxilliary
space complexity is O(1)

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.

Balance Factor (k) = height (left(k)) - height (right(k))


● If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal
height.
● If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right
sub-tree.
● An AVL tree is given in the following figure. We can see that, balance factor associated with
each node is in between -1 and +1. therefore, it is an example of AVL tree.
Complexity
Algorithm Average case Worst case

Space o(n) o(n)

Search o(log n) o(log n)

Insert o(log n) o(log n)

Delete o(log n) o(log n)

Operations on an AVL Tree:


● Insertion
● Deletion
● Searching [It is similar to performing a search in BST]

Rotating the subtrees in an AVL Tree:


An AVL tree may rotate in one of the following four ways to keep itself balanced:
Left Rotation:
When a node is added into the right subtree of the right subtree, if the tree gets out of balance, we
do a single left rotation.
Right Rotation:
If a node is added to the left subtree of the left subtree, the AVL tree may get out of balance, we
do a single right rotation.

Right-Rotation in AVL Tree

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.

Advantages of AVL Tree:


1. AVL trees can self-balance themselves.
2. It is surely not skewed.
3. It provides faster lookups than Red-Black Trees
4. Better searching time complexity compared to other trees like binary tree.
5. Height cannot exceed log(N), where, N is the total number of nodes in the tree.
Disadvantages

1. It has high constant factors for some of the operations.

2. Less used compared to Red-Black trees.

3. Due to its rather strict balance, AVL trees provide complicated insertion and removal
operations as more rotations are performed.

4. Take more processing for balancing.

Heap Data Structure


Heap is a special case of balanced binary tree data structure where the root-node key is compared
with its children and arranged accordingly. If α has child node β then − key(α) ≥ key(β)
As the value of parent is greater than that of child, this property
generates Max Heap. Based on this criteria, a heap can be of two types
−For Input → 35 33 42 10 14 19 27 44 26 31

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.

Max Heap Construction Algorithm


We shall use the same example to demonstrate how a Max Heap is created. The procedure to
create Min Heap is similar but we go for min values instead of max values.
We are going to derive an algorithm for max heap by inserting one element at a time. At any
point of time, heap must maintain its property. While insertion, we also assume that we are
inserting a node in an already heapified tree.

Step 1 − Create a new node at the end of heap.


Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.

Max Heap Deletion Algorithm


Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap always
happens at the root to remove the Maximum (or minimum) value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Operations on Min-heap Data Structure and their Implementation:
Here are some common operations that can be performed on a Heap Data Structure,

1. Insertion in Min-Heap Data Structure:


Elements can be inserted into the heap following a similar approach as discussed above for
deletion. The idea is to:
● The insertion operation in a min-heap involves the following steps:
● Add the new element to the end of the heap, in the next available position in the last level
of the tree.
● Compare the new element with its parent. If the parent is greater than the new element,
swap them.
● Repeat step 2 until the parent is smaller than or equal to the new element, or until the new
element reaches the root of the tree.
● The new element is now in its correct position in the min heap, and the heap property is
satisfied.

2. Deletion in Min-Heap Data Structure:


Removing the smallest element (the root) from the min heap. The root is replaced by the last
element in the heap, and then the heap property is restored by swapping the new root with its
smallest child until the parent is smaller than both children or until the new root reaches a leaf
node.
● Replace the root or element to be deleted with the last element.
● Delete the last element from the Heap.
● Since the last element is now placed at the position of the root node. So, it may not follow
the heap property. Therefore, heapify the last node placed at the position of the root.

Application of Heap Data Structure:


 Priority queues: The heap data structure is commonly used to implement
priority queues, where elements are stored in a heap and ordered based on
their priority.
Heapsort algorithm: The heap data structure is the basis for the heapsort
algorithm, which is an efficient sorting algorithm with a worst-case time
complexity of O(n log n).
Memory management: The heap data structure is used in memory
management systems to allocate and deallocate memory dynamically.
Graph algorithms: The heap data structure is used in various graph
algorithms.
Job scheduling: The heap data structure is used in job scheduling algorithms,
where tasks are scheduled based on their priority or deadline.

You might also like