0% found this document useful (0 votes)
20 views

Unit3 Trees

Uploaded by

zmusic769
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Unit3 Trees

Uploaded by

zmusic769
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 175

Trees

What is a Tree
• Tree is non-linear Data
Structure. Computers”R”Us
• A tree is a finite nonempty set
of elements.
• It is an abstract model of a Sales Manufacturing R&D
hierarchical structure.
• Consists of nodes with a
parent-child relation. US International Laptops Desktops
• Applications:
– Organization charts
– File systems Europe Asia Canada
– Programming
environments

11/7/2024 1
Trees
Tree Terminology
• Root: node without parent (A)
• Subtree: tree consisting of a
• Siblings: nodes share the same parent
node and its descendants
• Degree of a Node: Number of subtrees of a node
in a given tree.
• Degree of Tree: It is the maximum degree of a
node in a tree
A
• Internal: node with at least one child (A, B, C, F)
• External or terminal or leaf node: node without
children (E, I, J, K, G, H, D) or degree 0
B C D
• Ancestors of a node: parent, grandparent, grand-
grandparent, etc.
• Descendant of a node: child, grandchild, grand-
grandchild, etc. E F G H
• Edge: It is a connecting line between two nodes:
like (A,B) or (B,F) etc.
• Path: It is a sequence of consecutive edges from I J K
the source node to destination node. In the subtree
example tree the path from A to K is:
11/7/2024 (A,B), (B,F) & (F,K) or A,B,F,K 2
Trees
Tree Terminology
Depth of the tree- no. of edges from root to leaf Level 0
node in max path.
Height of a tree: no. of edges from leaf node to root
node in max path.
Depth of the node – no. of edges from root node to
that node. Level 1
Height of the node – no. of edges from leaf node
to that node.
Level: The entire tree structure is levelled in such a
way that the root node is always at level 0. Then its Level 2
immediate children are at level 1 and their
immediate children are at level 2 and so on. In
general if a node is at level n then its children will
be at n+1 level.
Non-Terminal Nodes: Any node (except the Level 3
root node) whose degree is not zero is called non-
terminal node.

11/7/2024 3
Trees
Tree Properties
Property Value
A
Number of nodes
Height
B C
Root Node
Leaves
Ancestors of H
D E F
Descendants of B
Siblings of E
Right subtree of A
G
Degree of this tree

H I

11/7/2024 4
Trees
Binary Tree

Binary tree is a data structure in which each node can have at most 2
children. The node present at the top most level is called the root
node. A node with the 0 children is called leaf node. Binary Trees are
used in the applications like expression evaluation and many more.

Binary Search Tree


Binary search tree is an ordered binary tree. All the elements in the left
sub-tree are less than the root while elements present in the right
sub-tree are greater than or equal to the root node element.
Binary search trees are used in most of the applications of computer
science domain like searching, sorting, etc.

11/7/2024 5
Trees

11/7/2024 6
Trees
Binary Tree
A tree whose elements have at most 2 children is called a binary tree.
Since each element in a binary tree can have only 2 children, we typically
name them the left and right child.

A Binary Tree node contains following parts.


1.Data
2.Pointer to left child
3.Pointer to right child

11/7/2024 7
Trees
Binary Tree(Properties)
1) The maximum number of nodes at level ‘L’ of a binary tree is 2L.
Here level is the number of nodes on the path from the root to the node
(including root and node). Level of the root is 0.
This can be proved by induction.
For root, L = 0, number of nodes = 20 = 1
Assume that the maximum number of nodes on level ‘L’ is 2L
Since in Binary tree every node has at most 2 children, next level would have
twice nodes, i.e. 2 * 2L = 2L+1 for L+1 level
2. The Maximum number of nodes in a binary tree of height ‘h’ is 2h+1 – 1.
3. In a Binary Tree with N nodes, minimum possible height or the minimum
number of levels is? log2(N+1) -1

11/7/2024 8
Trees

Types of Binary Tree

1. Complete Binary Tree


2. Full Binary Tree or Strictly Binary Tree
3. Balanced Binary Tree

11/7/2024 9
Trees
Full Binary Tree or Strictly Binary Tree: A Binary
Tree is a full binary tree if every node has 0 or 2
children. The following are the examples of a full
binary tree. We can also say a full binary tree is a
binary tree in which all nodes except leaf nodes have
two children.

In a Full Binary Tree, The number of leaf nodes is the


number of internal nodes plus 1
L=I+1
Where L = Number of leaf nodes, I = Number of
internal nodes

11/7/2024 10
Trees
FULL Binary Tree theorem

Theorem: Let T be a nonempty, full binary tree Then:

(a) If T has I internal nodes, the number of leaves is L = I + 1.

(b) If T has I internal nodes, the total number of nodes is N = 2I + 1.

(c) If T has a total of N nodes, the number of internal nodes is I = (N – 1)/2.

(d) If T has a total of N nodes, the number of leaves is L = (N + 1)/2.

(e) If T has L leaves, the total number of nodes is N = 2L – 1.

(f) If T has L leaves, the number of internal nodes is I = L – 1.

11/7/2024 11
Trees
Complete Binary Tree: A Binary Tree is a complete Binary Tree if all the levels
are completely filled except possibly the last level and the last level has all keys as
left as possible
The leftmost side of the leaf node must always be filled first.
The following are examples of Complete Binary Trees

11/7/2024 12
Trees
Perfect Binary Tree: A Binary tree is a Perfect
Binary Tree in which all the internal nodes have two
children and all leaf nodes are at the same level.
The following are the examples of Perfect Binary
Trees.

A Perfect Binary Tree of height h (where the height of


the binary tree is the longest path from the root node
to any leaf node in the tree) has 2h+1 – 1 node.

11/7/2024 13
Trees
A degenerate (or pathological) tree: A Tree where every internal node has one
child. Such trees are performance-wise same as linked list.

11/7/2024 14
Trees

11/7/2024 15
Trees

11/7/2024 16
Trees

Binary Tree Representations

A binary tree data structure is represented using two methods. Those methods
are as follows...

Array Representation
Linked List Representation

11/7/2024 17
Trees

1. Array Representation of Binary Tree


In array representation of a binary tree, we use one-dimensional array (1-D
Array) to represent a binary tree.

11/7/2024 18
• The Root of the tree is stored at T[1]
• If node N occupy T[k], then its left child is stored in T[2*k]
and right child T[2*k+1].
• If T[1]= Null Then tree is empty.
• A tree with depth d required an array approximately 2𝑑+1

11/7/2024 Megha Gupta KCS 301: DS Unit 3 19


Trees
2. 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.

11/7/2024 20
Trees

11/7/2024 21
Trees
Binary Tree Traversal
– Preorder (Node, Left, Right)
– Inorder(Left, Node, Right)
– Postorder (Left, Right, Node)

11/7/2024 22
Trees

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


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

11/7/2024 23
Trees

PreOrder : (NLR)

Ans – 1 7 2 6 3 8 5 9 4

InOrder: (LNR)

Ans – 2 7 3 6 8 1 5 4 9

PostOrder: (LRN)

Ans - 2 3 8 6 7 4 9 5 1

11/7/2024 24
11/7/2024 25
Trees
Algorithm Inorder(tree
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)

Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.

11/7/2024 26
Traversal Algorithms
• In-order Traversal:
void inorder(node *p){
if(p!=NULL){
inorder(p->left);
printf(“%d\n”, p->num);
inorder(p->right);
}
} Post-order Traversal:
void postorder(node *p){
if(p!=NULL){

Pre-order Traversal: postorder(p->left);


void preorder(node *p){ postorder(p->right);
if(p!=NULL){ printf(“%d\n”, p->num);
printf(“%d\n”, p->num); }
preorder(p->left); }
preorder(p->right);
}
}

11/7/2024 27
Trees
Construct Tree from given Inorder and Postorder traversals
1. Pickup last key of post order sequence and create node of each.

2. Create left subtree of root node recursively by selecting keys which are
preceding the root node in Inorder.

3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder

11/7/2024 28
Trees
Construct Tree from given Inorder and Postorder traversals

Inorder: 1 2 3 4 5 7 8
Postorder: 1 3 4 2 7 8 5

11/7/2024 29
Trees
Construct Tree from given Inorder and Preorder traversals

1. Pickup first key of pre order sequence and create node of each.

2. Create left subtree of root node recursively by selecting keys which are preceding
the root node in Inorder.

3. Create right subtree of root node recursively by selecting keys which are
following the root node in Inorder

11/7/2024 30
Trees
Construct Tree from given Inorder and Preorder traversals

Inorder: 1 2 3 4 5 7 8
Preorder: 5 2 1 4 3 8 7

Inorder: 1 2 3 4 5 6 7 8
Preorder: 5 3 1 2 4 6 8 7

11/7/2024 31
Create Tree
• Preorder: A B D G C E H I F
• Inorder: D G B A H E I C F

11/7/2024 32
Trees

1. Write the C function to count number of nodes in a binary tree.

int countnodes(struct node *r)


{
if(r==NULL)
return 0;
return countnodes(r→left)+ countnodes(r→right)+1;
}

11/7/2024 33
Trees

2. Write the C function to count number of leaf nodes in a binary tree.

int countleafnode(struct node *r)


{
if(r==NULL)
return 0;
if(r→left==NULL&&r→right==NULL)
return 1;

return countleafnode(r→left)+ countleafnode(r→right);


}

11/7/2024 34
Trees

3. Write the C function to count number of non-leaf nodes in a binary tree.

int countleafnode(struct node *r)


{
if(r==NULL)
return 0;
if(r→left==NULL&&r→right==NULL)
return 0;

return countleafnode(r→left)+ countleafnode(r→right)+1;


}

11/7/2024 35
Trees

4. Write the C function to count height of the binary tree.

int height(struct node *r)


{
if(r==NULL)
return 0;
lh = height(r→left);
rh = height(r→right);
if(lh>=rh)
return lh+1;
else
return rh+1;
}

11/7/2024 36
Trees

Binary Search Tree Representation


Binary Search tree exhibits a special behavior.

A node's left child must have a value less than its parent's value and the
node's right child must have a value greater than its parent value.

11/7/2024 37
Trees
Binary Search Tree Representation
Post Order Traversal of BST is 10,9,23,22,27,25,15,95,60,40,29.
Find PreOrder traversal.

11/7/2024 38
Trees

BST
Tree Node
The code to write a tree node would be similar to what is
given below. It has a data part and references to its left and
right child nodes.

struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

11/7/2024 39
Trees
BST Basic Operations
The basic operations that can be performed on a binary search tree data
structure, are the following −

•Insert − Inserts an element in a tree/create a tree.

•Search − Searches an element in a tree.

•Preorder Traversal − Traverses a tree in a pre-order manner.

•Inorder Traversal − Traverses a tree in an in-order manner.

•Postorder Traversal − Traverses a tree in a post-order manner.


We shall learn creating (inserting into) a tree structure and searching a
data item in a tree and learn about tree traversing methods in the coming
chapter.
11/7/2024 40
Insertion in BST
Insert function is used to add a new element in a binary search tree at
appropriate location. Insert function is to be designed in such a way
that, it must node violate the property of binary search tree at each
value. Following are the steps to insert a node.
• Allocate the memory for tree.
• Set the data part to the value and set the left and right pointer of tree,
point to NULL.
• If the item to be inserted, will be the first element of the tree, then
the left and right of this node will point to NULL.
• Else, check if the item is less than the root element of the tree, if this
is true, then recursively perform this operation with the left of the
root.
• If this is false, then perform this operation recursively with the right
sub-tree of the root.

11/7/2024 41
Trees
Insertion in BST

struct node* insert(struct node* node, int key)


{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);

if (key < node->key)


node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}

11/7/2024 42
11/7/2024 43
Creation of Binary Search Tree
• Create a Binary Search Tree for following keys
14,10,17,12,10,11,20,12,18,25,20,8,22,11,23

11/7/2024 44
Creation of Binary Search Tree
• Create a Binary Search Tree for following keys
S,T,P,Q,M,N,O,R,K,V,A,B

11/7/2024 45
Trees

BST Insertion Operation


1. To insert element in degenerate tree, it requires order of n times.

2. To insert an element in Complete Binary search tree in the worst case, the no.
comparison required are bounded by O(log2n).

11/7/2024 46
Trees

BST Search Operation

struct node* search(struct node* root, int key)


{
// Base Cases: root is null or key is present at root
if (root == NULL || root->key == key)
return root;

// Key is greater than root's key


if (root->key < key)
return search(root->right, key);

// Key is smaller than root's key


return search(root->left, key);
}
11/7/2024 47
Trees

There are three ways which we use to traverse a tree

•In-order Traversal
•Pre-order Traversal
•Post-order Traversal

11/7/2024 48
Trees

In-order Traversal
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

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 inorder traversal of this tree will be −
D →B → E →A→ F→ C →G
11/7/2024 49
Trees
Pre-order Traversal
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

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
11/7/2024 50
Trees

Post-order Traversal

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

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
11/7/2024 51
Trees
Binary Search Tree Deletion
Case 1 : Deleting the leaf node from BST

Task 1 : Finding key takes either log2(n) or O(n) times.


Task 2 : Identify leaf node parent pointer which is pointing to it and
make it NULL.

11/7/2024 52
Trees
Binary Search Tree Deletion
Case 2 : Deleting the node which has only one child

Task 1 : Finding key takes either log2(n) or O(n) times.


Task 2 : Copy the child to the node and delete the child

11/7/2024 53
Trees
Binary Search Tree Deletion
Case 3 : Deleting the node which has left subtree or right subtree.
Task 1 : Finding key takes either log2(n) or O(n) times.
Task 2 : Replace the deleted node with the max of LST or min of RST
i.e the node which is to be deleted, is replaced with its in-order
successor

11/7/2024 54
Trees

Binary Search Tree Representation


Construct all possible Binary Search tree by inserting following keys in any order.

10, 20,30

11/7/2024 55
Trees

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.

BalanceFactor = height(left-subtree) − height(right-subtree)

11/7/2024 56
Trees

AVL Tree

Balance Factor = height(left-subtree) − height(right-subtree)

11/7/2024 57
Trees

11/7/2024 58
Trees

AVL Rotations

We perform rotation in AVL tree only in case if Balance Factor is other


than -1, 0, and 1. There are basically four types of rotations which are
as follows:

1.L L rotation: Inserted node is in the left subtree of left subtree of A


2.R R rotation : Inserted node is in the right subtree of right subtree of A
3.L R rotation : Inserted node is in the right subtree of left subtree of A
4.R L rotation : Inserted node is in the left subtree of right subtree of A
Where node A is the node whose balance Factor is other than -1, 0, 1.

The first two rotations LL and RR are single rotations and the next two
rotations LR and RL are double rotations.

11/7/2024 59
Trees
Left Rotation/RR Rotation
If a tree becomes unbalanced, when a node is inserted into the right
subtree of the right subtree, then we perform a single left rotation −

In our example, node A has become unbalanced as a node is


inserted in the right subtree of A's right subtree. We perform the left
rotation by making A the left-subtree of B.

11/7/2024 60
Trees

Right Rotation/LL Rotation


AVL tree may become unbalanced, if a node is inserted in the left
subtree of the left subtree. The tree then needs a right rotation.

As depicted, the unbalanced node becomes the right child of its left
child by performing a right rotation.

11/7/2024 61
Trees
Left-Right Rotation ()
State Action

A node has been inserted into the right subtree of the


left subtree. This makes C an unbalanced node.
These scenarios cause AVL tree to perform left-right
rotation.

We first perform the left rotation on the left subtree


of C. This makes A, the left subtree of B.

11/7/2024 62
Trees

Node C is still unbalanced,


however now, it is because of the
left-subtree of the left-subtree.

We shall now right-rotate the tree,


making B the new root node of this
subtree. C now becomes the right
subtree of its own left subtree.

The tree is now balanced.

11/7/2024 63
Trees

Right-Left Rotation
The second type of double rotation is Right-Left Rotation. It is a
combination of right rotation followed by left rotation.
State Action

A node has been inserted into the left subtree of the


right subtree. This makes A, an unbalanced node
with balance factor 2.

First, we perform the right rotation along C node,


making C the right subtree of its own left subtree B.
Now, B becomes the right subtree of A.

11/7/2024 64
Trees

Node A is still unbalanced because of the


right subtree of its right subtree and
requires a left rotation.

A left rotation is performed by


making B the new root node of the
subtree. A becomes the left subtree of its
right subtree B.

The tree is now balanced.

11/7/2024 65
Trees
Q: Construct an AVL tree having the following elements
H, I, J, B, A, E, C, F, D, G, K, L

1. Insert H, I, J

11/7/2024 66
Trees
On inserting the above 2. Insert B, A
elements, especially in the case
of H, the BST becomes
unbalanced as the Balance
Factor of H is -2. Since the BST
is right-skewed, we will perform
RR Rotation on node H.
The resultant balance tree is:

11/7/2024 67
Trees

On inserting the above elements, especially in case of A, the BST


becomes unbalanced as the Balance Factor of H and I is 2, we consider
the first node from the last inserted node i.e. H. Since the BST from H is
left-skewed, we will perform LL Rotation on node H.
The resultant balance tree is:

11/7/2024 68
Trees

3. Insert E On inserting E,

BST becomes unbalanced as the


Balance Factor of I is 2, since if we
travel from E to I we find that it is
inserted in the left subtree of right
subtree of I,

we will perform LR Rotation on node


I. LR = RR + LL rotation

11/7/2024 69
Trees
3 a) We first perform RR rotation on node B
The resultant tree after RR rotation is:

11/7/2024 70
Trees

3b) We first perform LL rotation on the node I


The resultant balanced tree after LL rotation is:

11/7/2024 71
Trees

4. Insert C, F, D

On inserting C, F, D, BST
becomes unbalanced as the
Balance Factor of B and H is -2,
since if we travel from D to B we
find that it is inserted in the right
subtree of left subtree of B,

we will perform RL Rotation on


node I. RL = LL + RR rotation.

11/7/2024 Megha Gupta KCS 301: DS Unit 3 72


Trees

4a) We first perform LL rotation on node E


The resultant tree after LL rotation is:

11/7/2024 73
Trees

4b) We then perform RR rotation on node B


The resultant balanced tree after RR rotation is:

11/7/2024 74
Trees

5. Insert G

On inserting G, BST become unbalanced as the Balance Factor of H is 2,


since if we travel from G to H, we find that it is inserted in the left subtree
of right subtree of H, we will perform LR Rotation on node I. LR = RR + LL
rotation.

11/7/2024 75
Trees

5 a) We first perform RR rotation on node C


The resultant tree after RR rotation is:

11/7/2024 76
Trees

5 b) We then perform LL rotation on node H


The resultant balanced tree after LL rotation is:

11/7/2024 77
Trees

6. Insert K

On inserting K, BST becomes unbalanced as the Balance Factor of I is -2.


Since the BST is right-skewed from I to K, hence we will perform RR
Rotation on the node I.

11/7/2024 78
Trees

The resultant balanced tree after RR rotation is:

11/7/2024 79
Trees

7. Insert L
On inserting the L tree is still balanced as the Balance Factor of each
node is now either, -1, 0, +1. Hence the tree is a Balanced AVL tree

11/7/2024 80
Trees

Deletion in AVL Tree


Deleting a node from an AVL tree is similar to that in a binary search
tree.
Case1: Deleting node is Leaf Node
1. Delete that node
2. Check balance factor and perform rotation if found any critical node
Case 2: Deleting node has one child
1. Replace the deleted node with its child
2. Check balance factor and perform rotation if found any critical node
Case 3: Deleting node has two children
1. Find the largest node (x) in left subtree or find the smallest node (y) in right subtree
and replace deleted node either by x or y.
2. Check balance factor and perform rotation if found any critical node

Rotations
There are two types of rotations L rotation (RR) and R rotation (LL). Here, we
will discuss R rotations. L rotations are the mirror images of them.

If the node which is to be deleted is present in the left sub-tree of the critical node,
then L rotation needs to be applied else if, the node which is to be deleted is present
in the right sub-tree of the critical node, the R rotation will be applied.

11/7/2024 81
Trees
Let us consider that, A is the critical node and B is the root node of its
left sub-tree. If node X, present in the right sub-tree of A, is to be
deleted, then there can be three different situations:

R0 rotation (Node B has balance factor 0 )

If the node B has 0 balance factor, and the balance factor of node A
disturbed upon deleting the node X, then the tree will be rebalanced
by rotating tree using R0 rotation.

The critical node A is moved to its right and the node B becomes the
root of the tree with T1 as its left sub-tree. The sub-trees T2 and T3
becomes the left and right sub-tree of the node A. the process
involved in R0 rotation is shown in the following image.

11/7/2024 82
Trees
Example:
Delete the node 30 from the AVL tree shown in the following image.
Solution
In this case, the node B has balance factor 0, therefore the tree will be
rotated by using R0 rotation as shown in the following image. The node
B(10) becomes the root, while the node A is moved to its right. The right
child of node B will now become the left child of node A.

11/7/2024 83
Trees
R1 Rotation (Node B has balance factor 1)

R1 Rotation is to be performed if the balance factor of Node B is 1. In


R1 rotation, the critical node A is moved to its right having sub-trees
T2 and T3 as its left and right child respectively. T1 is to be placed as
the left sub-tree of the node B.
The process involved in R1 rotation is shown in the following image.

11/7/2024 84
Trees

Example
Delete Node 55 from the AVL tree shown in the following image.

Deleting 55 from the AVL Tree disturbs the balance factor of the node
50 i.e. node A which becomes the critical node. This is the condition of
R1 rotation in which, the node A will be moved to its right (shown in the
image below). The right of B is now become the left of A (i.e. 45).
The process involved in the solution is shown in the following image.

11/7/2024 85
Trees
R-1 Rotation (Node B has balance factor -1)
R-1 rotation is to be performed if the node B has balance factor -1. This case
is treated in the same way as LR rotation. In this case, the node C, which is
the right child of node B, becomes the root node of the tree with B and A as
its left and right children respectively.
The sub-trees T1, T2 becomes the left and right sub-trees of B whereas, T3,
T4 become the left and right sub-trees of A.
The process involved in R-1 rotation is shown in the following image.

11/7/2024 86
Trees
Example
Delete the node 60 from the AVL tree shown in the following image.

Solution:
in this case, node B has balance factor -1. Deleting the node
60, disturbs the balance factor of the node 50 therefore, it
needs to be R-1 rotated. The node C i.e. 45 becomes the
root of the tree with the node B(40) and A(50) as its left and
right child.

11/7/2024 87
AVL Tree Deletion

BF(B)=0 BF(B)=+1 BF(B)=-1

BF(A)=+2 R0 (LL) Rotation R1 (LL) Rotation) R-1 (LR) Rotation

BF(A)=-2 L0 (RR) Rotation L1 (RR) Rotation) L-1 (RL) Rotation

11/7/2024 88
Trees
Motivation for B-Trees
• So far we have assumed that we can store an entire data
structure in main memory
• What if we have so much data that it won’t fit?
• We will have to use disk storage but when this happens our time
complexity fails

11/7/2024 89
Trees

Motivation (cont.)
• Assume that a disk spins at 3600 RPM
• In 1 minute it makes 3600 revolutions, hence one revolution
occurs in 1/60 of a second, or 16.7ms
• On average what we want is half way round this disk – it will take
8ms
• This sounds good until you realize that we get 120 disk accesses
a second – the same time as 25 million instructions
• In other words, one disk access takes about the same time as
200,000 instructions
• It is worth executing lots of instructions to avoid a disk access

11/7/2024 90
Trees

Motivation (cont.)
• Assume that we use an Binary tree to store all the details of
people in Canada (about 32 million records)
• We still end up with a very deep tree with lots of different disk
accesses; log2 20,000,000 is about 25, so this takes about
1.21 seconds (if there is only one user of the program)
• We know we can’t improve on the log n for a binary tree
• But, the solution is to use more branches and thus less height!
• As branching increases, depth decreases

11/7/2024 91
Trees

M-Way Search Tree


1. Generalized version of BST

2. The goal is to minimize the access time( Time required to load the data from the
memory) while retrieving a key value from the file.

A m-way search tree is an empty tree if tree is not empty then it satisfies the
following properties

(i) Each node has at most m-child nodes called order of the tree.

Each node can be represented as (k0A0,k1A1,k2A2…..Km-1,Am-1)

(ii) If m is the order of the tree, then max no. of child pointers are m and max
number of key stores m-1.

11/7/2024 92
Trees
2. If a node has k keys than, k<=m-1
than the keys are k1,k2,k3,….km
such that ki<ki+1 also each of the
keys in the node partition the sub
trees into k+1 subsets.

3. For any node all the keys in the


subtree pointed by Ai-1 are less than
the key ki and all the keys in the sub
tree pointed by Ai are greter than the
key ki.

4. Each subtree pointed by ai are


also m-way search tree.

11/7/2024 93
M-way Trees
An M-way(multi-way) tree is a tree that has the following properties:
• Each node in the tree can have at most m children.
• Nodes in the tree have at most (m-1) key fields and pointers(references) to
the children.

11/7/2024 94
M-way Search Trees
• An M-way search tree is a more constrained m-way tree, and these
constrain mainly apply to the key fields and the values in them. The
constraints on an M-way tree that makes it an M-way search tree
are:
• Each node in the tree can associate with m children and m-1 key
fields.
• The keys in any node of the tree are arranged in a sorted
order(ascending).
• The keys in the first K children are less than the Kth key of this
node.
• The keys in the last (m-K) children are higher than the Kth key.

11/7/2024 95
M-way Search Tree

11/7/2024 96
M-way Search Trees
• If we want to search for a value say X in an M-way search tree and
currently we are at a node that contains key values from Y1, Y2,
Y3,.....,Yk. Then in total 4 cases are possible to deal with this scenario,
these are:

• If X < Y1, then we need to recursively traverse the left subtree of Y1.
• If X > Yk, then we need to recursively traverse the right subtree of Yk.
• If X = Yi, for some i, then we are done, and can return.
• Last and only remaining case is that when for some i we have Yi < X <
Y(i+1), then in this case we need to recursively traverse the subtree that is
present in between Yi and Y(i+1).
• An m-Way search tree of n elements ranges from a minimum height
of log_m(n+1) to a maximum of n (It is not height balanced)

11/7/2024 97
Searching in M-way search tree

11/7/2024 98
B Trees Data Structure
A B tree is an extension of an M-way search tree. Besides having all the
properties of an M-way search tree, it has some properties of its own,
these mainly are:
• All the leaf nodes in a B tree are at the same level.
• The root node has at least two child nodes and most M child nodes
• All internal nodes except root node must have at least
⌈ M/2⌉ child nodes and at most M child nodes
• The number of keys in each internal node is one less than the
number of child nodes and these keys and these keys partition the
keys in the subtrees of the node in a manner similar to that of M-
way search tree.

11/7/2024 99
B-tree of degree 5

11/7/2024 100
Trees
Insertion in the B-Tree
Search the element in the B-Tree is the same manner as in the m-way search
tree.
If the element is not present then search tends to finish at some leaf node.

Two cases arise-

Case 1. When leaf element is not full then insert element into it.

Case 2. When leaf node is full


a) Insert the element into the node
b) Split the node at its median into two nodes at the same level and pushing
the median element up by one level into the parent node.
c) If parent node accommodating the median element is not full then exit
otherwise repeat the above steps. It may even call the rearrangement of keys
in the root nodes or the formation of new root itself.

11/7/2024 101
Trees

Constructing a B Tree

Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R.
Order = 5

1. Insert P

2. Insert P

11/7/2024 102
Trees
Constructing a B Tree

Example
key :- P,A,Q,S,B,X,C,L,Z,Y,T,G,J,I,D,H,R,V,E,U,F,R,O.
1. Insert Q

2. Insert S

3. Insert B

11/7/2024 103
Trees

4. Insert X

5. Insert C

6. Insert L

11/7/2024 104
Trees
7. Insert Z

8. Insert Y

9. Insert T

11/7/2024 105
Trees
10. Insert G

11 Insert J

12 Insert I

11/7/2024 106
Trees

13. Insert D

14. Insert H

11/7/2024 107
Trees
15. Insert R

16. Insert V

11/7/2024 108
Trees

17. Insert E

18. Insert U

11/7/2024 109
Trees

19. Insert F

20. Insert O

11/7/2024 110
Trees

Constructing a B Tree
Example
key :- 1,12,8,2,25,6,14,28,17,7,52,16,48,68,3,26,29,53,55,45,67.
Order = 5
Procedure for adding key in b-tree

Step1. Add first key as root node.

11/7/2024 111
Trees

Step2. Add next key at the appropriate place in sorted order.

Step3. Same process applied until root node full. if root node full then
splitting process applied.

11/7/2024 112
Trees

11/7/2024 113
Trees

11/7/2024 114
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

11/7/2024 115
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

11/7/2024 116
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

11/7/2024 117
Trees
Example
key :-J, R, D, G, T, E, M, H, P, A, F, Q
Order = 4

11/7/2024 118
Trees

Deleting from a B-tree

As usual, this is the hardest of the processes to apply. The deletion


process will basically be a reversal of the insertion process - rather
than splitting nodes, it's possible that nodes will be merged so that
B-tree properties, namely the requirement that a node must be at
least half full, can be maintained.
There are two main cases to be considered:
1.Deletion from a leaf
2.Deletion from a non-leaf

11/7/2024 119
Case 1: Deletion from a leaf
1a) If the leaf is at least half full after deleting the desired value, the remaining larger values are
moved to "fill the gap".

Deleting 6 from the following tree:

11/7/2024 120
1b) If the leaf is less than half full after deleting the desired value (known as underflow), two
things could happen:
Deleting 7 from the tree above results in

1b-1) If there is a left or right sibling with the number of keys exceeding the minimum
requirement, all of the keys from the leaf and sibling will be redistributed between them
by moving the separator key from the parent to the leaf and moving the middle key from
the node and the sibling combined to the parent.

11/7/2024 121
Now delete 8 from the tree:

1b-2) If the number of keys in the sibling does not exceed the minimum requirement, then the
leaf and sibling are merged by putting the keys from the leaf, the sibling, and the separator
from the parent into the leaf. The sibling node is discarded and the keys in the parent are
moved to "fill the gap". It's possible that this will cause the parent to underflow. If that is the
case, treat the parent as a leaf and continue repeating step 1b-2 until the minimum requirement
is met or the root of the tree is reached.

Special Case for 1b-2: When merging nodes, if the parent is the root with only one key, the
keys from the node, the sibling, and the only key of the root are placed into a node and this will
become the new root for the B-tree. Both the sibling and the old root will be discarded.

11/7/2024 122
11/7/2024 123
Case 2: Deletion from a non-leaf
This case can lead to problems with tree reorganization but it will be solved in a manner
similar to deletion from a binary search tree.

The key to be deleted will be replaced by its immediate predecessor (or successor) and then
the predecessor (or successor) will be deleted since it can only be found in a leaf node.
Deleting 16 from the tree above results in:

11/7/2024 124
The "gap" is filled in with the immediate predecessor:

11/7/2024 125
Huffman Coding
• Huffman Coding is a famous Greedy Algorithm.

• It is used for the lossless compression of data.

• It uses variable length encoding.

• It assigns variable length codes to all the characters depending


on how frequently they occur in the given text.
• The character which occurs most frequently gets the smallest
code.

• The character which occurs least frequently gets the largest


code.

• It is also known as Huffman Encoding.


11/7/2024 126
Huffman Coding
Prefix Rule-
• To prevent ambiguities while decoding, Huffman coding
implements a rule known as a prefix rule.
• It ensures that the code assigned to any character is not a prefix
of the code assigned to any other character.

Major Steps in Huffman Coding-

There are two major steps in Huffman coding-


1. Building a Huffman tree from the input characters.
2. Assigning codes to the characters by traversing the Huffman
tree.

11/7/2024 127
Huffman Coding

The steps involved in the construction of Huffman Tree


are as follows-

Step-01:
• Create a leaf node for all the given characters.
• Leaf node contains the occurring frequency of that character.

Step-02:

• Arrange all the nodes in increasing order of their frequency value.

11/7/2024 128
Huffman Coding

Considering the first two nodes having minimum frequency,


• Create a new internal node.
• The frequency of this node is the sum of the frequency of those
two nodes.
• Make the first node as a left child and the other node as a right
child of the newly created node.

Step-04:

• Keep repeating Step-02 and Step-03 until all the nodes form a
single tree.
• The tree finally obtained will be the desired Huffman Tree.

11/7/2024 129
Huffman Coding

Important Formulas-
Formula-01:

11/7/2024 130
Huffman Coding

Formula-02:
Total number of bits in Huffman encoded message
= Total number of characters in the message x Average code
length per character
= ∑ ( frequencyi x Code lengthi )

11/7/2024 131
Huffman Coding

PRACTICE PROBLEM BASED ON HUFFMAN CODING-

Problem-

A file contains the following characters with the frequencies as shown.


If Huffman coding is used for data compression, determine-
1. Huffman code for each character
2. Average code length
3. Length of Huffman encoded message (in bits)

11/7/2024 132
Huffman Coding

Characters Frequencies
a 10
e 15
i 12
o 3
u 4
s 13
t 1

11/7/2024 133
Huffman Coding
Solution-
First let us construct the Huffman Tree.
Huffman Tree is constructed in the
following steps-
Step-01:

Step-02:

11/7/2024 134
Huffman Coding

Step-03:

11/7/2024 135
Huffman Coding
Step-04:

11/7/2024 136
Huffman Coding
Step-05:

11/7/2024 137
Huffman Coding
Step-06:

11/7/2024 138
Huffman Coding
Step-07:

11/7/2024 139
Huffman Coding
Now,
• We assign weight to all the edges of the constructed Huffman
tree.
• Let us assign weight ‘0’ to the left edges and weight ‘1’ to the
right edges.
Rule
• If you assign weight ‘0’ to the left edges, then assign
weight ‘1’ to the right edges.
• If you assign weight ‘1’ to the left edges, then assign
weight ‘0’ to the right edges.
• But follow the same convention at the time of decoding
that was adopted at the time of encoding.

After assigning weight to all the edges, the modified Huffman Tree is-

11/7/2024 140
Huffman Coding

Now, let us answer each part of the problem one by one-

11/7/2024 141
Huffman Coding

1. Huffman Code For The Characters-


To write Huffman Code for a character, traverse the Huffman
tree from root node to all the leaf node of that character.
Following this rule, the Huffman Code for each character is-
• a = 111
• e = 10
• i = 00
• o = 11001
• u = 1101
• s = 01
• t = 11000

11/7/2024 142
Huffman Coding
From here, We can observe-
• Characters occurring less frequently in the text are assigned
the larger code.
• Characters occurring more frequently in the text are assigned
the smaller code.

2. Average Code Length-

Average code length


= ∑ ( frequencyi x code lengthi ) / ∑ ( frequencyi )
= { (10 x 3) + (15 x 2) + (12 x 2) + (3 x 5) + (4 x 4) + (13 x 2) + (1
x 5) } / (10
+ 15 + 12 + 3 + 4 + 13 + 1)
= 2.52

11/7/2024 143
Huffman Coding

3. Length of Huffman Encoded Message-

Total number of bits in Huffman encoded message


= Total number of characters in the message x Average code
length per character
= 58 x 2.52
= 146.16
≅147 bits

11/7/2024 144
Trees
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the link fields
contain NULL values which results in wastage of storage space.
• If a binary tree consists of n nodes then n+1 link fields contain NULL values. So in order
to effectively manage the space, a method was devised by Perlis and Thornton in which
the NULL links are replaced with special links known as threads.
• Such binary trees with threads are known as threaded binary trees. Each node in a
threaded binary tree either contains a link to its child node or thread to other nodes in
the tree.

11/7/2024 145
Trees

Types of Threaded Binary Tree


There are two types of Threaded Binary Tree
• One-way threaded Binary Tree
• Two-way threaded Binary Tree

11/7/2024 146
Trees

Threaded Binary Tree Two-Way

• In the two-way threading of T.


• A thread will also appear in the left field of a node and will point to the
preceding node in the in-order traversal of tree T.
• Furthermore, the left pointer of the first node and the right pointer of the
last node (in the in-order traversal of T) will contain the null value
when T does not have a header node.

11/7/2024 147
Trees

Threaded Binary Tree Two-Way

• In the two-way threading of T.


• A thread will also appear in the left field of a node and will point to the
preceding node in the in-order traversal of tree T.
• Furthermore, the left pointer of the first node and the right pointer of the
last node (in the in-order traversal of T) will contain the null value
when T does not have a header node.

11/7/2024 148
Trees
One Way Threaded Binary Tree
• 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 inorder 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.
• In one-way threaded binary trees, the right link field of last node and left link field of
first node contains a NULL.
• In order to distinguish threads from normal links they are represented by dotted lines.

11/7/2024 149
11/7/2024 150
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.

11/7/2024 151
In the above figure of two-way threaded Binary tree, we noticed that no left thread is
possible for the first node and no right thread is possible for the last node.
This is because they don't have any inorder predecessor and successor respectively. This
is indicated by threads pointing nowhere.
So in order to maintain the uniformity of threads, we maintain a special node called
the header node.

11/7/2024 152
11/7/2024 153
Trees

Threaded Binary Tree


Advantages of threaded binary tree:

• Threaded binary trees have numerous advantages over non-threaded


binary trees listed as below:

• The traversal operation is more faster than that of its unthreaded


version, because with threaded binary tree non-recursive
implementation is possible which can run faster and does not require
the botheration of stack management.

11/7/2024 154
Trees

Threaded Binary Tree


• Advantages of threaded binary tree:

• The second advantage is more understated with a threaded binary


tree, we can efficiently determine the predecessor and successor
nodes starting from any node.
In case of unthreaded binary tree, however, this task is more time
consuming and difficult.
For this case a stack is required to provide upward pointing
information in the tree whereas in a threaded binary tree, without
having to include the overhead of using a stack mechanism the same
can be carried out with the threads.

11/7/2024 155
Trees

Threaded Binary Tree


• Advantages of threaded binary tree:
Any node can be accessible from any other node.
Threads are usually more to upward whereas links are downward.
Thus in a threaded tree, one can move in their direction and nodes
are in fact circularly linked.
This is not possible in unthreaded counter part because there we can
move only in downward direction starting from root.
Insertion into and deletions from a threaded tree are although time
consuming operations but these are very easy to implement.

11/7/2024 156
Trees

Threaded Binary Tree


• Disadvantages of threaded binary tree:

• Insertion and deletion from a threaded tree are very time


consuming operation compare to non-threaded binary tree.

• This tree require additional bit to identify the threaded link.

11/7/2024 157
Faculty Video Links, Youtube & NPTEL
Video Links and Online Courses Details
• Self Made Video Link:

• YouTube/other Video Links


https://www.youtube.com/watch?v=tORLeHHtazM

https://www.youtube.com/watch?v=qH6yxkw0u78&t=96s

https://www.youtube.com/watch?v=YAdLFsTG70w

11/7/2024 158
Daily Quiz

Q1. A Binary Tree cannot have odd number of nodes?


A. True
B. False

Q2. Which of the following statement is false:


A. A tree with ‘n’ nodes contains ‘n-1’ edges.
B. Every tree is a bipartite graph.
C. A tree is a connected graph.
D. A tree contains a cycle.

11/7/2024 159
Daily Quiz

Q3. A full binary tree with ‘n’ leaves contains:


A. n nodes
B. n+1 nodes
C. 2n nodes
D. 2n+1 nodes

Q4. The worst case of AVL Tree with ‘n’ nodes is:
A. 2logn
B. n log(n+1)
C. 1.44 log (n+2)
D. 1.44 nlogn

11/7/2024 160
Daily Quiz

Q5. The number of nodes in a full binary tree of depth 4 is:


A. 15
B. 16
C. 14
D. 13

Q6. Traversing a binary tree first root node and then left sub-tree and right sub-
tree called __Pre-Order____ traversal.

Q7. A B.S.T. contains the value 1, 2,3,4,5,6,7,8. The tree is traversed in Pre-Order
and the values are printed out. Which of the following is a valid output?
A. 53124786
B. 53126487
C. 53241678
D. 53124768

11/7/2024 161
Daily Quiz
Q8. Which of the following statement is false?
A. A tree with ‘n’ nodes has (n-1) edges.
B. A labeled sorted binary tree can be uniquely constructed given the post-order and
in-order traversal result.
C. A complete binary tree with’ n’ internal nodes has (n+1) leaves.
D. The maximum number of nodes in a binary tree of height ‘h’ is (2n+1-1).

Q9. The following number is inserted into an empty B.S.T. in given order:
10,1,3,5,15,12,16. What is the height of B.S.T.?
A. 2
B. 3
C. 4
D. 6

11/7/2024 162
Weekly Assignment

1. Prove that number of nodes in a binary tree of height h is 2h+1-1.


2. Prove that minimum height of a binary tree with node n is log2 n.
3. Implement iterative functions for preorder, inorder and postorder
traversal.
4. Construct a binary tree from given tree traversals-
Preorder: G,B,Q,A,C,K,F,P,D,E,R,H
Inorder: Q,B,K,C,F,A,G,P,E,D,H,R
5. Construct a binary tree from given tree traversals-
Postorder: 4,5,2,6,7,3,1
Inorder: 4,2,5,1,6,3,7
6. Construct a binary tree for the following expressions-
I. (A<B)&&(B<C)&&(C-D)
II. A+(B+C*D+E)+F/G

11/7/2024 163
Weekly Assignment

7. Generate a binary search tree by inserting following integers


50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24.
8. Write a function to delete a node from binary search tree.
9. Write properties of B-Tree. Construct a B tree of order 5 by
inserting following data:
H,D,K,Z,B,P,Q,E,A,S,W,T,C,L,N,Y,M.
10. With the help of diagrams explain all the rotations of AVL tree.
Construct tree for the following list of elements:
3,5,11,8,4,1,12,7,2,6,10.
11. Given 5-way B-tree created by these data (last exercise):
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
Add these further keys: 2, 6,12
Delete these keys: 4, 5, 7, 3, 14

11/7/2024 164
MCQ s

1. How many children does a binary tree have?


a) 2
b) any number of children
c) 0 or 1 or 2
d) 0 or 1

2. What is/are the disadvantages of implementing tree using normal


arrays?
a) difficulty in knowing children nodes of a node
b) difficult in finding the parent of a node
c) have to know the maximum number of nodes possible before
creation of trees
d) difficult to implement

11/7/2024 165
MCQ s
3. What must be the ideal size of array if the height of tree is ‘l’?
a) 2l-1
b) l-1
c) l
d) 2l

4. What are the children for node ‘w’ of a complete-binary tree in an


array representation?
a) 2w and 2w+1
b) 2+w and 2-w
c) w+1/2 and w/2
d) w-1/2 and w+1/2

11/7/2024 166
MCQ s
5. For the tree below, write the 6. For the tree below, write the
pre-order traversal. post-order traversal.

a) 2, 7, 2, 6, 5, 11, 5, 9, 4
a) 2, 7, 2, 6, 5, 11, 5, 9, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
b) 2, 7, 5, 2, 6, 9, 5, 11, 4
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
c) 2, 5, 11, 6, 7, 4, 9, 5, 2
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
d) 2, 7, 5, 6, 11, 2, 5, 4, 9
11/7/2024 167
MCQ s

7. What is the maximum number of children that a binary tree node can
have?
a) 0
b) 1
c) 2
d) 3

8. The following given tree is an example for?


a) Binary tree
b) Binary search tree
c) Fibonacci tree
d) AVL tree

11/7/2024 168
MCQ s
9. A binary tree is a rooted tree but not an ordered tree.
a) True
b) False
10. What is the traversal strategy used in the binary tree?
a) depth-first traversal
b) breadth-first traversal
c) random traversal
d) Priority traversal
11. How many types of insertion are performed in a binary tree?
a) 1
b) 2
c) 3
d) 4
11/7/2024 169
Expected Questions for University Exam

1. Define Binary search tree. How it is different from binary tree?


2. Suppose the following list of letters is inserted in order into empty binary
search tree: J, R, D, G, T, E, M, H, P, A, F, Q
(i) Construct the binary Search Tree
(ii) Find the in-order, Pre-order and Post-order Traversal of the BST created.
3. Discuss the advantages of using B-Tree. Insert the following Information 86,
23, 91, 4, 67, 18, 32, 54, 46, 96, 45 into an empty B-Tree with order 4.
4. Construct the binary tree using following in-order and post-order traversal.
In-order : DBMINEAFCJGK
Post-order : ABDEIMNCFGJK
5. What is a height balanced Tree? Why height balancing of Tree is required?
Create an AVL Tree for the following elements: a, z, b, y, c, x, d, w, e, v, f

11/7/2024 170
Expected Questions for University Exam

6. What is binary tree? Explain.

7. What is binary search tree? Write the important applications of binary search
tree. Write algorithm to delete a node from a binary search tree.

8. Write short notes on following


i.) B-Tree
ii.) Minimum cost spanning tree

9. Insert the following keys to a 5-way B-tree:


3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

11/7/2024 171
Old Question Papers

https://drive.google.com/open?id=15fAkaWQ5c
cZRZPzwlP4PBh1LxcPp4VAd

11/7/2024 172
Summary

• Unlike Array and Linked List, which are linear data structures,
tree is hierarchical (or non-linear) data structure.

• One reason to use trees might be because you want to store


information that naturally forms a hierarchy. For example, the file
system on a computer file system

• Binary Search Tree is a tree that allows fast search, insert, delete
on a sorted data. It also allows finding closest item

11/7/2024 173
References

[1] Aaron M. Tenenbaum, Yedidyah Langsam and Moshe J. Augenstein,


“Data Structures Using C and C++”, PHI Learning Private Limited,
Delhi India
[2] Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia
Publications Pvt Ltd Delhi India.
[3] Lipschutz, “Data Structures” Schaum’s Outline Series, Tata
McGraw-hill Education (India) Pvt. Ltd.
[4] Thareja, “Data Structure Using C” Oxford Higher Education.
[5] AK Sharma, “Data Structure Using C”, Pearson Education India.

11/7/2024 174
Thank you

11/7/2024 175

You might also like