DS Unit-3
DS Unit-3
Dr.R.Kanniga Devi
AssoCiate Professor
CSE/KARE
Outline
Introduction
Tree
Representation of Trees
Basic Tree Terminologies
Binary Trees
Binary Tree Traversal
Threaded Binary Trees
Binary Search Trees
AVL tree
Applications of Binary Tree
BTree
B+ Tree
Algorithm Analysis
Real Tree vs. DS Tree
8 9 10 11 13 14
Tree
ADT
Non-Linear data structure
Faster than linear data structures
More natural fit for Hierarchical data
Tree
Node
2
Edge
6
8
9 10 11 13 14
Represent organization
Represent computer file systems ( to store
information that naturally forms a hierarchy
Networks to find best path in the Internet
.Encoding
Decision tree
Chemical formulas representation (in Non-CS
field too)
Tree vs. Graph
(
X
This graph is not a Tree This graph is a Tree
Tree terminology
Child
Parent (siblings
Edge (Degree
Root Internal
Node
Tree Terminology
(Leaf Node
Forest
Subtree Level
Depth Height
Tree terminology
Root
The first node from where the tree
originates is called as a root node.
In any tree, there must be only one root
node.
Root node
Tree terminology
Edge
The connecting link
between any two nodes is
called as an edge.
A - Edges
Tree terminology
Parent
The node which has a branch from it to any other node is
called as a parent node.
In other words, the node which has one or more children
is called as a parent node.
In a tree, a parent node can have any number of child
nodes.
Parent of B and C
Here,
Node A is the parent of nodes B and C
Node B is the parent of nodes D, E and F
Here,
Nodes B and C are the children of node A
Nodes D, E and F are the children of node
B
Nodes G and H are the children of node C
Nodes I and J are the children of node E
Node K is the child of node GG
Tree terminology
SiblingsS
Nodes which belong to the same
parent are called as siblings.
In other words, nodes with the
same parent are sibling nodes.
Here,
Nodes Band C are siblings
Nodes D, E and F are siblings Siblings
Degree of node A = 2
Degree of node B =F3
Degree of node C = 2
Degree(B) =3
Degree(C) =2
Degree of node D = 0
Degree of node E =2
Degree of node F = 0
Degree of node G =1
Degree of node H =0
Degree of node = 00
I
Degree of node J =0
Degree of node K =F0
Tree terminology
InternalNode-(intermediate
nodes
an internal node.
Internal nodes are also called
as non-terminal nodes.
Every non-leaf node is an
internal node.
Here, nodes A, B, C, E and G
are internal nodes.
Tree terminology
Leaf Node
The node which does not
have any child is called as
a leaf node.
Leaf nodes are also called Leaf Node
--
Level11
-
Level 2
-
Level 3
Tree terminology
Path
-
the edges of a tree
A,B,D forms a path Root node
Tree terminology
Height
Total number of edges that lies on the longest path
from any leaf node to a particular node is called
as height of that node.
Height of a tree is the height of root node.
(Bottom to Top)
Height of node F = 00
Height of node G =F1
Height of node H = 0
Height of node I = 0
Height of node J = 0
Height of node K 00
Tree terminology
Depth
Depth of node J = 3
Depth of node K = 3 Depth(H) 2
Tree terminology
Subtree
In a tree, each child from a node forms
a subtree recursively.
Every child node forms a subtree on its
parent node.
Sub trees
Forest-
A
Tree terminology
Forest
-
Tree Terminology-Exercise
27
14 35
10 19 31 42
Representation of Trees
a
a
left right
b C
b
left right left right
d e f
d e f left rightleft right left right
struct node
int data;
struct node* left;
struct node* right;
Tree Applications
Binary Tree
Binary Search Tree
AVL Tree
B-Tree
.B+ Tree
Binary Trees
2 3
4 5
Binary Tree Representation
A node of a binary tree is
represented by a structure
containing a data part and two
pointers to other structures of
the same type.
struct node Data
{int data; Left Right
24
oe 3 s
Binary Tree Representation- Array
Min.no of nodes=1
Eg: height h=1
Min.no of nodes=h+1=>1+1=2
Binary Tree Representation-Array
w.bte ss.co
O O
Binary Tree Representation-Linked List
rress
Left Child
Address Data e
Right Child
LAddress
Binary Tree Representation
Example:
w.bte ss.co
rootNode
AN
B
DN F NULL
H
NULL NULL
Types of Binary Trees
Inorder -
left-root- right
.Preorder -
root-left -right
.Postorder -
left-right-root
Binary Tree Traversal
w.bte ss.co
-
1- D-J-B F-A-G-K-C- H
Binary Tree Traversal
Example: Preorder
w.bte ss.co
A- B-D-1 -J-F-C-G-K-H
Binary Tree Traversal
Example: Postorder
w.bte ss.co
B C
B C
2 (200)
10 (30 300)
Answer
Inorder 10,20, 30, 100, 150, 200,300
Preorder :
100, 20, 10, 30, 200, 150, 300
.Post order : 10,30, 20, 150, 300, 200, 100
Exercise- Binary Tree Traversall
D F
(Cn)
H
Answer
Inorder :BDA GEC HFI
Preorder ABD CEG FHI
:
.Post order: DB GE HI FC A
Binary Tree Traversal
Example:
12 9
Binary Tree creation
Creation of Binary Tree Using Recursion
A binary tree can be created recursively. The program will work as follow:
Reada data in x.
memory
Allocate for a new node and store the address in pointer p.
Store the data x in the node p.
Recursively create the left subtree of p and make it the left child of p.
Recursively create the right subtree of p and make it the right child of p.
Binary Tree Creation
struct Node
{
int data;
struct Node *left;
struct Node *right;
Binary Tree Creation
struct node *
create()
#include<stdio.h>
#include<malloc.h>
int x;
struct node struct node *newnode;
newnode=(struct node *) malloc(sizeof(struct node));
int data; printf("enter data (-1 for no node)");
struct node *left; scanf("%d",&x);
struct node *right; if(x==-1)
return 0;
newnode->data=X;
printf("\n enter left child of %d", x);
newnode->left=create();
printf("\n enter right child of %d", x);
newnode->right=create();
return newnode;
Binary Tree Creation
void preorder(struct node *root) void main()
{
if(root != NULL)
display(root->left);
printf("%d", root->data);
display(root->right);
Binary Tree display (preorder)
if(root != NULL)
printf("%d", root->data);
display(root->left);
display(root->right);
}
Binary Tree display (postorder)
if(root != NULL)
display(root->left);
display(root->right);
printf("%d",root->data);
Binary Tree -Time Complexity
A binary tree has the following time complexities
-
Search Operation - O(n)
-
Insertion Operation O(n)
-
Deletion Operation - O(n)
Searching:
For searching element 1, we have to
traverse all elements (in order 3, 2, 1).
Therefore, searching in binary search
tree has worst case complexity of O(n).
In general, time complexity is Oh)
where h is height of BST.
3
2
1
Binary Tree -Time Complexity
Insertion:
- For inserting element 0, it must be
inserted as left child of 1.
-
Therefore, we need to traverse all
elements (in order 3, 2, 1) to insert0
which has worst case complexity of
O(n).
- In general, time complexity is O(h).
3
(2
Binary Tree -Time Complexity
Deletion:
-
For deletion of element 1
we need to traverse all elements (in
order 3, 2, 1) to find 1 which has
Worst case complexity of O(n).
- In general, time complexity is O(h).
3
(2
Threaded Binary Tree
Threaded Binary Tree makes use of NULL
pointers to improve its traversal process.
In a threaded binary tree, NULL pointers are
replaced by references of other nodes in the
tree.
These extra reterences are called as threads.
TypesSingle threaded and double threaded
Threaded Binary Tree
Threaded Binary Tree is also a binary tree
in which
- all left child pointers that are NULL (in Linked
list representation) points to its in-order
predecessor
- all right child pointers that are NULL (in Linked
list representation) points to its in-order
SuccesSor.
Threaded Binary Tree
It there isno in-order predecessor or in-order
successor, then it points to the root node.
Threaded Binary Tree
www mar
G
(H) O O
To convert the example binary tree into a threaded binary tree, first find the
in-order traversal of that tree...
In-ordertraversal ofabove binary tree..H D-1-B-
- E-A-F-J -C-G
Threaded Binary Tree
In-ordertraversal of abovebinary tree...H D-1-B-E-A-F-J-C-G
-
O
O
When we represent the example binary tree using linked list
representation, nodes H, 1, E, F, J and G left child pointers are
NULL.
This NULL is replaced by address of its in-order predecessor
respectively (I to D, E to B, F to A, J to F and G to C), but here
the node H does not have its in-order predecessor, so it
points to the root node A.
Nodes H, 1, E, J and G right child pointers are NULL.
These NULL pointers are replaced by address of its in-order
successor respectively (H to D, I to B, E to A, and J to C), but
here the node G does not have its in-order successor, so it
points to the root node A.
Threaded Binary Tree
Example binary tree is converted into threaded binary tree as
follows.
In the figure, threads are indicated with dotted links.
L
A)
Ot oO
Thread
Use of Threaded Binary Tree
Inorder traversal of binary search tree is either be
done using recursion or with an auxiliary stack.
The idea of the threaded binary trees is to make
inorder traversal faster and do it without stack
and without recursion
Without recursion, it will not take more memory
and computational time
It facilitates forward and backward traversal from
a node
Binary Search Tree (BST)
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 is a binary tree in which every node
contains only smaller values in its left subtree and only
larger values in its right subtree.
Binary Search Tree
Node
with yalue
Left /Right
Subtree Subtree
Contains only Contains only \
/smaller values\ / larger values\
All values <= K All values> K
Binary Search Tree
Example
In this tree, left subtree of every node contains nodes with
smaller values and right subtree of every node contains larger
values.
40)
28 48
4 50
Every binary search tree is a binary tree but every binary tree need not to be binary
search tree.
Binary Search Tree-Operations
The following operations are performed on a binary search
tree
- Search
Insertion
-
Deletion
Traversal (Inorder displays elements in increasing order)
Binary Search Tree-Construction
Example
Construct a Binary Search Tree by inserting the following
sequence of numbers...
10,12,5,4,20,8,7,15 and 13
Binary Search Tree-Construction
insert (10) insert (12) insert (5)
10 10 10
4 8
Binary Search Tree-Construction
10
12
20
15
13
Binary Search Tree-Advantages
Searching become very efficient in a binary search tree since,
we get a hint at each step, about which sub-tree contains the
desired element.
The binary search tree is considered as efficient data
structure as compared to arrays and linked lists.
In searching process, it removes halfsub-tree at every step.
Searching for an element in a binary search tree takes
o(log,n) time.
It also speed up the insertion and deletion operations as
compared to that in array and linked list.
Binary Search Tree-insert
Algorithm:
If node == NULL
return createNode(data
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
Binary Search Tree-Delete- 3 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
Binary Search Tree-Delete- case-1
Case 1: Deleting a leaf node (Simply remove from the tree)
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.
Binary Search Tree-Delete- case-1
Case
Inthe first case, the node to be deleted is the leaf node. In such a case,
simply delete the node from the tree.
I0
3 10
14
14
Binary Search Tree-Delete- case-2
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
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 free function and terminate
the function.
Binary Search Tree-Delete- case-2
Case II (Copy the child to the node and delete the child)
In the second case, the node to be deleted lies has a single child node.
In such a case follow the steps below:
Replace that node with its child node.
Remove the child node from its original position.
Binary Search Tree-Delete- case-2
8
3 10
3 10
14
4
3 10
14
Binary Search Tree-Delete- case-3
Case Ill (Find inorder successor of the node. Copy contents of
the inorder successor to the node and delete the inorder
successor. Note that inorder predecessor can also be used))
In the third case, the node to be deleted has two children. In such a
case follow the steps below:
Get the inorder successor of that node.
Replace the node with the inorder successor. (smallest element on the
right subtree) or Replace the node with the inorder predecessor.
(greatest element on the left subtree)
-
Remove the inorder successor from its original position.
Binary Search Tree-Delete- case-3
3 10 10
6 14 6 14
8
10
14
Binary Search Tree-Delete- case-1
50 50
12 30 60 85 12 30 60 85 X
deleted node
52 70 52 70
Binary Search Tree-Delete- case-2
50 50
60 6 30 60
12 30
52 70 52 70
6 12 X
Deleted node
Binary Search Tree-Delete- case-3
50 52
60 30 60
6 30
52 70 50 70
Deleted Node
return root;
Binary Search Tree-Delete- case-3
//function to delete a node
struct node* delete(struct node *root, int x)
I/No Children
if(root->left_child==NULL && root->right_child==NULL)
free(root);
return NULL;
Binary Search Tree-Delete- case-3
//One Child
else if(root->left_child==NULL || root->right_child==NULL)
{
return root;
Binary Search Tree-Search
Algorithm:
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
Binary Search Tree-Time Complexity
Insert O(log n)
Delete O(log n)
Traverse O(n)
Create O(n)
Binary Search Tree-Exercise
Dr.R.Kanniga Devi
Associate Professor
CSE/KARE
8 18 sT
Because-
18
The difference between height of
left subtree and right subtree of root
node =
4-2 2.
This difference is greatenthan one
2
Not an AVL Tree
33
Right Subtree Height of Left Subtree) 1 1
9 53
The self balancing property of an AVL 8 21 61
tree is maintained by the balance factor.
11
The value of balance factor should
always be
0or +1,
1-2-1 1-1 0
50 150
1-1 0
0
25 75 125 175
65 85
AVL Tree
In AVL tree
Balance factor is defined for every node.
Balance factor of a node =
Height of its left subtree Height of its right
Subtree
-
Balance factor of every node is either-0er 1
or -1.
Search Operation
Insertion Operation
Deletion Operation
Case-01:
After the operation, the boalance factor of each node is either 0 or 1 or
-1
Case-02:
After the operation, the balance factor of at least one node is not 0 or
1or-1.
Inthis case, the AVL tree is considered to be imbalanced.
Rotations are then performed to balance the tree.
balance factor of each node is checked.
Case-01:
After the insertion, the balance factor of each node is either 0 or 1 or -1.
In this case, the tree is considered to be balanced.
Conclude the operation.
Insert the next element if any.
Case-02:
After the insertion, the balance factor of at least one node is not 0 or 1 or -1.
In this case, the tree is considered to be imbalanced.
Perform the suitable rotation to balance the tree.
After the tree is balanced, insert the next element if any.
S
(
( A)
-
(20)
Et ah wes 6
AVL Tree insertion- Practice Problem-
Solution
(50) 50
20 60
Tree is Balanced (20
Tree is Balanced
Tree is Balanced
50 5
(60)
(60
10
(60)
* RR Rotation
(60
Tree is Imbalance
O
-.
60
LR Rotation
- 5
Tree is Balanced
Tree is Balanced
(46)
(11
-------- (6
-O
Tree is imbalanced
(60)
Tree is Imbalanced
LL Rotation
= 2H*1-1
Height 3
D)
14 L (3
(3
2
LL Rotation
(Rotate Anticlockwise)
3
Tree is Balanced
RR Rotation
(Rotate clockwise)
Tree is Balanced
3
LR Rotation
Tree is Balanced
Insertion Order: 3, 1,2
Tree is Imbalanced
RR+LL Rotation
Tree is Balanced
LR 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.
C
Node C is still unbalanced, however now, it is because of the left
B subtree of the left-subtree.
C
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.
- C
D
First, we perform
subtree
of A.
subtree
subtree.
and requires
A left rotation
the
because
a left rotation.
is performed
A becomes
by making
the left subtree
node,
B. Now, B becomes
of the right
making
subtree
of its right
of the
30 BF=+1 30 BF +2 BF=0
20
BF 0 10 BF
Case: 1B 10 BF=-1 O 30 BF 0
20
BF-0 20 BF 0
BF=0
Case: 1CC BF= 0 BF= 0 BF 1 30 BF=1
BF 0 BF= 0 20 BF 0
20 BF-00 20
BF 0
Deleting 40 Tree becomes imbalance Tree balanced.
performing LL rotation.
BF 0 20 40 BF0
40 BF-00 400 BF-00
310
BF 0
SBF 0
Deleting 10 Tree becomes imbalance, Tree balanced.
performing RL rotation.
.
2
Node A Critical Node Node B
20 20 10
Performing RO
0
Deleting Node 30 rotation
Node B
Node A
10 30 10 5 20
Node to be deleted
(X)
5 15 5 15 15
Performing R1
1 Node A
Deleting Node55 rotation
Node B
Node B
40 60 40 60 30 50 0
0
30 45 55 30 45 10 45 60
Node to be deleted
10 10
Performing R-1
-1
Deleting Node 60 rotation
Node B
Node B
40 60 40 40 50
45 45
Node C Node C
int data;
struct AVLNode *left, *right;
int balfactor;
14
11 17
53
14
17
4 11
53
13
14
17
4 11
53
13
12
14
17
4 11
53
12
13
14
17
4 12 53
11 13
14
17
4 12 53
11
13
8
14
17
4 11
53
8 12
13
14
11 17
12 53
4 8 13
14
11 17
12 53
4 8 13
14
11 17
12
4 8 13
11
14
4 8 12 17
13
14
12 17
13
4 14
12 17
13
4 12
14
13 17
12
14
4
13 17
20 20
13 24 15 24
10 15 13
10
Edit with WPS OTice
15, 20, 24, 10, 13, 7, 30, 36, 25
20
13
13 24 10 20
10 15
7 15 24
30
13 36
10 20
15 30
24 36
Edit with WPS Office
15, 20, 24, 10, 13, 7, 30, 36, 25
13 13
10 20 10 20
15 30 7 15 24
24 36 30
25 13 25 36
10 24
7 20 30
15 25 36
Edit with WPS Office
Remove 24 and 20 from the AVL tree.
13 13
10 24 10 20
7 20 30 7 15 30
15 2536 25 36
13 13
10 30 10 15
15 36 7 30
25 Edit with WPS Office 25 36
B-Tree
B Tree is a Balancing Tree, not a Binary Tree
Balanced m-waytree
m-means order of a tree (For eg: create a B
tree of order m)
B-Tree was named Height Balanced m-way
Search Tree. Later it was named as B-Tree.
8070
25 40 50
becomes newone
root node for the tree and the height of the tree is
increased by
insert(2)
Element 2'is added to existing leaf node. Here, we have only one node and that node acts as root and also leaf. This leaf
node has an empty position. So, newelement (2) can be inserted at that empty position.
12
insert(3)
Element 3'is added to existing leaf node. Here, we have only one node and that node acts as root and a lso leaf. This leaf
node doesn't has an empty position. So, we split that node by sending middle value (2) to its parent node. But here, this
node doesn't has parent. So, this middle value becomes a new root node for the tree.
After split
12 3
34
insert(5)
Element '5' is larger than root node 2'and it is not a leaf node. So, we move to the right of'2! We reach to a leaf node and
it is already full. So, we split that node by sending middle value (4) to its parent node (2). There is an empty position in its
parent node. So, value '4'is added to node with value'2'and new element'5' added as new leaf node.
After split 2 4
24
insert(7)
Element 7' is larger than root node 2'&'4'and it is not a leaf node. So, we move to the right of '4. We reach to a leaf node
and it is already full. So, we split that node by sending middle value (6) to its parent node (284). But the parent (28-4) is
also full. So, again we split the node (2&4) by sending middle value'4'to its parent but this node doesn't have parent.
So, the element'4' becomes new root node for the tree.
4
After split
2
Edit with WPS Office
insert(8)
B-Tree Construction
Element '8'is larger than root node'4' and it is not a leaf node. So, we move to the right of 4. We reach to a node with
value '6.'8 is larger than'6' and it is also not a leaf node. So, we move to the right of'6. We reach to a leaf node (7) and it
5 an empty position. So, new element (8) can be inserted at that empty position.
2 6
insert(9)
Element '9' is larger than root node'4' and it is not a leaf node. So, we move to the right of'4. We reach to a node with
value'6. 9 is larger than'6' and it is also not a leaf node. So, we move to the right of'6. We reach to a leaf node (7 &8).
This leaf node is already full. So, we split this node by sending middle value (8) to its parent node. The parent node (6)
has an empty position. So, '8'is added at that position. And new element is added as a new leaf node.
2 6 8
B-Tree Construction
insert(10)
Element '10' is largerthan root node 4' and it is not a leaf node. So, we move to the right of 4. We reach to a node with
values'6& 8:'10'is larger than '6 & 8'and it is also not a leaf node. So, we move to the right of'8. We reach to a leafnode
(9).This leaf node has an empty position. So, new element '10' is added at that empty position.
4
2 G 8
10
2
1 2
3
T UdIL
1123
S
2,
2
ma
51
dlla elarma
2
3
21/D
shl Vist9,
B- P
2
Edit with WPS Office
|
all
(l2 RG
TV
P 7
T
3
element with next key value in the same node and repedt steps 3,
4, 5 and 6 until we find the exact match or until the search
element is compared with last key value in the leaf node.
Step 7- If the last key value in the leaf node is also not matched
then display "Element is not found" and terminate the function.
78
40 56 89 101
10 23 45 49 60 69 85 90 98 103 110
Delete o(log n)
Index P
SN x Y Z
T
1 123
1
2 311
Index P
3
3 444
1
Index P 4 654
5
5 5 111
Example
Insert the value 195 into the B+ tree of
order 5 shown in the following figure.
48 83 88 10 119 195 20
60 78 120 190
2
3
3
2 34-
Siz+
O e nuJlle ame
Edit with WPS Office
3
21| 2
L3
P-
Edit with WPS Office
3
s//T
s/4
3
25
15 35 45
25
15 35 45
5 15 2025 30 35 4045 55
Edit with WPS Office
B+ Tree Searching
25
15 35 45
k is found
25
15 35 45
https://www.cs.ustca.edu/~galles/visualization/
BPlusTree.html
Delete o(log n)