0% found this document useful (0 votes)
6 views37 pages

DS Unit 4

The document provides an overview of tree data structures, focusing on binary trees and their properties, types, and representations. It explains concepts such as node relationships, tree height, and binary search trees, including AVL trees and their balancing operations. Additionally, it covers traversal methods and the time complexities associated with various operations on binary trees and binary search trees.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
6 views37 pages

DS Unit 4

The document provides an overview of tree data structures, focusing on binary trees and their properties, types, and representations. It explains concepts such as node relationships, tree height, and binary search trees, including AVL trees and their balancing operations. Additionally, it covers traversal methods and the time complexities associated with various operations on binary trees and binary search trees.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 37

UNIT 4

Tree Data Structures


Trees are non-linear data structures that represent nodes connected by edges. Each tree consists of a
rootnode as the Parent node, and the left node and right node as Child nodes.

Root → The topmost node of the hierarchy is called the root of the tree.
Child → Nodes next in the hierarchy are the children of the previous node.
Parent → The node just previous to the current node is the parent of the current
node. Siblings → Nodes with the same parent are called siblings.
Ancestors → Nodes which are higher in the hierarchy are ancestors of a given node.
Descendents → Nodes which are lower in the hierarchy are descendants of a given
node. Internal Nodes → Nodes with at least one child are internal nodes.
External Nodes/Leaves → Nodes which don't have any child are called leaves of a
tree. Edge → The link
between two nodes is called an edge.

Level → The root of a tree is at level 0 and the nodes whose parent is root are at level 1 and so
on.
Height → The height of a node is the number of nodes (excluding the node) on the
longest pathfrom the node to a leaf.
Height of Tree → Height of a tree is the height of its root.
Depth → The depth of a node is the number of nodes (excluding the node) on the path from
the rootto the node.

Node Degree → It is the maximum number of children a node has.


Tree Degree → Tree degree is the maximum of the node degrees. So, the tree degree in
the abovepicture is 3.
Properties of a Tree

A tree must have some properties so that we can differentiate from other data structures.
The numbers of nodes in a tree must be a finite and nonempty set.
There must exist a path to every node of a tree i.e., every node must be connected to some other node.
There must not be any cycles in the tree. It means that the number of edges is one less than the number
ofnodes.
Binary Trees
A binary tree is a tree in which every node has at most two children.

As you can see in the picture given above, a node can have less than 2 children but not more than

that.We can also classify a binary tree into different categories. Let's have a look at them:

Full Binary Tree → A binary tree in which every node has 2 children except the leaves is known as a
fullbinary tree.
Complete Binary Tree → A binary tree which is completely filled with a possible exception at the
bottomlevel i.e., the last level may not be completely filled and the bottom level is filled from left to

right.


Perfect Binary Tree → In a perfect binary tree, each leaf is at the same level and all the interior nodes havetwo
children.

Thus, a perfect binary tree will have the maximum number of nodes for all alternative binary trees of thesame
We know that the maximum number of nodes will be in a perfect binary tree.
Number of nodes at level 0 = 2^0 =1
Number of nodes at level 1 = 2^1 =2
Similarly, the number of nodes at level h =
2^h
Array Representation of Binary Tree

We start by numbering the nodes of the tree from 1 to n(number of nodes).


As you can see, we have numbered from top to bottom and left to right for the same level. Now, these numbers
represent the indices of an array (starting from 1) as shown in the picture given below.

We can also get the parent, the right child and the left child using the properties of a complete binary tree we have
discussed above i.e., for a node i, the left child is 2i and the right child is 2i+1.

So, we represented a complete binary tree using an array and saw how to get the parent and children of
anynode. Let's discuss about doing the same for an incomplete binary tree.

Array Representation of Incomplete Binary Tree

To represent an incomplete binary tree with an array, we first assume that all the nodes are present to
makeit a complete binary tree and then number the nodes as shown in the picture given below.

Now according to these numbering, we fill up the array.


Coding a Binary Tree

RIGHT_CHILD(index)

if (T[index] != null and (2*index + 1) <= T.size)

return (2*index + 1)

else

return null

Similarly, we can get the left child.

LEFT_CHILD(index)

if (T[index] != null and (2*index) <= T.size)

return (2*index)

else

return null

Similarly, we can also write the code to get the parent.

PARENT(index)

if (T[index] != null and (floor(index/2)) =! null)

return floor(index/2)

else

return null
Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a double linked list, every node consists of three
fields. First field for storing left child address, second for storing actual data and third for storing right
child address.
In this linked list representation, a node has the following structure...
/
\
Applications of Binary tree

Binary trees are mainly used for searching and sorting as they provide a means to store data
hierarchically. ∙ Routing Table

∙ Decision Trees

∙ Expression Evaluation

Binary Tree Traversal


Generally, there are three types of traversals:
▪ Inorder Traversal

▪ Preorder Traversal

▪ Postorder Traversal

Inorder Traversal:
To traverse a non empty tree in inorder the following steps are followed
recursively. ▪ Visit the Root
▪ Traverse the left subtree
▪ Traverse the right subtree
The inorder traversal of the tree shown below is as follows.
INORDER(n)

if(n != null)

INORDER(n.left)

print(n.data)
INORDER(n.right)

11
Preorder Traversal
Algorithm Pre-order(tree)
1. Visit the root.
2. Traverse the left sub-tree, i.e., call Pre-order(left-sub-tree) 3.
Traverse the right sub-tree, i.e., call Pre-order(right-sub-tree)

PREORDER(n)

if(n != null)

print(n.data) // visiting root

PREORDER(n.left) // visiting left subtree

PREORDER(n.right) // visiting right subtree

(a) Inorder (Left, Root, Right) : 4 2 5 1 3


(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1

Postorder Traversal
Algorithm Post-order(tree)
1. Traverse the left sub-tree, i.e., call Post-order(left-sub-tree) 2.
Traverse the right sub-tree, i.e., call Post-order(right-sub-tree)
3. Visit the root.
POSTORDER(n)

if(n != null)
PREORDER(n.left) // visiting left subtree

PREORDER(n.right) // visiting right subtree

print(n.data) // visiting root

Binary Search Tree

In a binary tree, every node can have a maximum of two children but there is no need to maintain the
orderof 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 binary tree, we use a special type of binary tree known as Binary Search
Tree. Binary search tree mainly focuses on the search operation in a binary tree. Binary search tree can
be defined as follows...

Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree
and only larger values in its right subtree.

In a binary search tree, all the nodes in the left subtree of any node contains smaller values and all the
nodesin 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
withsmaller values and right subtree of every node contains larger values.
Every binary search tree is a binary tree but every binary tree need not to be binary search tree.

Operations on a Binary Search Tree

The following operations are performed on a binary search tree...

1. Search

2. Insertion

3. Deletion
Search Operation in BST

In a binary search tree, the search operation is performed with O(log n) time complexity.
def search(root,key):
# Base Cases: root is null or key is present at root
if root is None or root.val == key:
return root
# Key is greater than root's key
if root.val < key:
return search(root.right,key)
# Key is smaller than root's key
return search(root.left,key)
Insertion Operation in BST

In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary
searchtree, new node is always inserted as a leaf node. The insertion operation is performed as follows...

∙ Step 1 - Create a newNode with given value and set its left and right to NULL. ∙

Step 2 - Check whether 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 newNodeis 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

newNodeis larger than the node then move to its right child.

∙ Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL). ∙ Step 7 -

After reaching the leaf node, insert the newNode as left child if the newNode is smaller orequal to
that leaf node or else insert it as right child.
Deletion Operation in BST
In a binary search tree, the deletion operation is performed with O(log n) time complexity. Deleting a
nodefrom Binary search tree includes 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 free function (If it is a leaf) and terminate the function.

50 50
/ \ delete(20) / \
30 70 ---------> 30 70
/\/\\/\
20 40 60 80 40 60 80

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 search operation

∙ Step2 - If the node to be deleted has only one sub tree then the corresponding child is moved up to its
parent’s position
50 50
/ \ delete(30) / \
30 70 ---------> 40 70
\/\/\
40 60 80 60 80
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 search operation

Step 2 - If the node to be deleted has both sub tree then it is replaced with its in order successor.
50 60
/ \ delete(50) / \
40 70 ---------> 40 70
/\\
60 80 80
Example

Construct a Binary Search Tree by inserting the following sequence of numbers...


10,12,5,4,20,8,7,15 and 13

Above elements are inserted into a Binary Search Tree as follows...


AVL Tree

AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary search tree but
itis a balanced tree. A binary tree is said to be balanced if, the difference between the heights of left and
right subtrees of every node in the tree is either -1, 0 or +1. In other words, a binary tree is said to be
balanced if the height of left and right children of every node differ by either -1, 0 or +1. In an AVL tree,
every node maintains an extra information known as balance factor. The AVL tree was introduced in the
year 1962 by

G.M. Adelson-Velsky and E.M. Landis.

An AVL tree is defined as follows...

An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every node is either -
1, 0 or +1.

Balance factor of a node is the difference between the heights of the left and right subtrees of that node.
The balance factor of a node is calculated either height of left subtree - height of right subtree (OR)
height of right subtree - height of left subtree. In the following explanation, we calculate as follows...

Balance factor = heightOfLeftSubtree - heightOfRightSubtree


Example of AVL Tree
The above tree is a binary search tree and every node is satisfying balance factor condition. So this
tree issaid to be an AVL tree.

Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.

AVL Tree Rotations

In AVL tree, after performing operations like insertion and deletion we need to check the balance factor
of every node in the tree. If every node satisfies the balance factor condition then we conclude the
operation otherwise we must make it balanced.

Whenever the tree becomes imbalanced due to any operation we use rotation operations to make the
tree balanced.
Rotation is the process of moving nodes either to left or to right to make the tree balanced.

Rotation operations are used to make the tree balanced.


There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)

In LL Rotation, every node moves one position to left from the current position. To understand LL Rotation,let us
consider the following insertion operation in AVL Tree...
Single Right Rotation (RR Rotation)

In RR Rotation, every node moves one position to right from the current position. To understand RR
Rotation, let us consider the following insertion operation in AVL Tree...
Left Right Rotation (LR Rotation)

The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation,
at first, every node moves one position to the left and one position to right from the current position. To

understand LR Rotation, let us consider the following insertion operation in AVL Tree...

Right Left Rotation (RL Rotation)

The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at
first every node moves one position to right and one position to left from the current position. To
understand RL Rotation, let us consider the following insertion operation in AVL Tree...
Operations on an AVL Tree

The following operations are performed on AVL tree...

1. Search

2. Insertion

3. Deletion

Search Operation in AVL Tree

In an AVL tree, the search operation is performed with O(log n) time complexity. The search operation
inthe AVL tree is similar to the search operation in a Binary search tree. We use the following steps to
search an element in AVL tree...

∙ Step 1 - Read the search element from the user.

∙ Step 2 - Compare the search element with the value of 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 search element is smaller or larger than that

node value.

∙ Step 5 - If search element is smaller, then continue the search process in left subtree.

∙ Step 6 - If search element is larger, then continue the search process in right subtree. ∙ Step

7 - Repeat the same until we find the exact element or until the search element is
comparedwith the leaf node.
∙ Step 8 - If we reach to the node having the value equal to the search value, then display

"Element isfound" and terminate the function.

∙ Step 9 - If we reach to the leaf node and if it is also not matched with the search element,

thendisplay "Element is not found" and terminate the function.

Insertion Operation in AVL Tree

In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a
newnode is always inserted as a leaf node. The insertion operation is performed as follows...

∙ Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic. ∙

Step 2 - After insertion, check the Balance Factor of every node.

∙ Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation. ∙ Step 4 -

If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to beimbalanced.
In this case, perform suitable Rotation to make it balanced and go for next operation.

Example: Construct an AVL Tree by inserting numbers from 1 to 8.


Deletion Operation in AVL Tree

The deletion operation in AVL Tree is similar to deletion operation in BST. But after every deletion
operation, we need to check with the Balance Factor condition. If the tree is balanced after deletion go for
next operation otherwise perform suitable rotation to make the tree Balanced.

Introduction to M-Way Search Trees:


A multiway tree is a tree that can have more than two children. A multiway tree of order m (or
an m-way tree) is one in which a tree can have m children.
As with the other trees that have been studied, the nodes in an m-way tree will be made up of key
fields, in this case m-1 key fields, and pointers to children.
Multiday tree of order 5
To make the processing of m-way trees easier some type of order will be imposed on the keys
within each node, resulting in a multiway search tree of order m (or an m-way search
tree). By definition an m-way search tree is a m-way tree in which:
∙ Each node has m children and m-1 key fields

∙ The keys in each node are in ascending order.

∙ The keys in the first i children are smaller than the ith key

∙ The keys in the last m-i children are larger than the ith key
4-way search tree
M-way search trees give the same advantages to m-way trees that binary search trees gave tobinary trees - they
provide fast information retrieval and update. However, they also have the same problems that binary search trees
had - they can become unbalanced, which means that the construction of the tree becomes of vital importance.

Heap
A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Generally, Heaps can be
of two types:
1. Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of
it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. 2. Min-Heap: In a
Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in that Binary Tree.

Applications of Heaps:
1) Heap Sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
2) Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it supports insert(),
delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are
variations of Binary Heap. These variations perform union also efficiently.
3) Graph Algorithms: The priority queues are especially used in Graph Algorithms like Dijkstra’s Shortest
Path and Prim’s Minimum Spanning Tree.
4) Many problems can be efficiently solved using Heaps. See following for example.
a) K’th Largest Element in an array.
b) Sort an almost sorted array/
c) Merge K Sorted Arrays.
Operations on Heap:

Delete operation

The standard deletion operation on Heap is to delete the element present at the root node of the Heap. That is if it is a
Max Heap, the standard deletion operation will delete the maximum element and if it is a Min heap, it will delete the
minimum element.
Process of Deletion:
Since deleting an element at any intermediary position in the heap can be costly, so we can simply replace the
element to be deleted by the last element and delete the last element of the Heap.
∙ Replace the root or element to be deleted by 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 root.

Suppose the Heap is a Max-Heap as:


10
/ \
5 3
/ \
2 4

The element to be deleted is root, i.e. 10.

Process:
The last element is 4.

Step 1: Replace the last element with root, and delete it.
4
/ \
5 3
/
2

Step 2:Heapify root.


Final Heap:
5
/ \
4 3
/
2

Time complexity: O(logn) where n is no of elements in the heap

Insert Operation

Process of Insertion: Elements can be inserted to the heap following a similar approach as discussed above for
deletion. The idea is to:
∙ First increase the heap size by 1, so that it can store the new element.

∙ Insert the new element at the end of the Heap.

∙ This newly inserted element may distort the properties of Heap for its parents. So, in order to keep the properties of
Heap, heapify this newly inserted element following a bottom-up approach.

Suppose the Heap is a Max-Heap as:


10
/ \
5 3

You might also like