SlideShare a Scribd company logo
DATA STRUCTURE
TREES
Trees:
• Definition of Tree
• Properties of Tree
• Binary Tree
• Representation of Tree using array and linked lists
• Operations on binary tree
• Binary tree traversal (recursive)
• Binary Search Tree (BST)
• B-Tree
• B+ Tree
• AVL Tree
• Threaded Binary Tree
Tree:
It is non-linear data structure.
It represents a hierarchical data structure.
It represents a parent child relationship between various pieces of data and thus allow us to arrange our
records of data and files in a hierarchical fashion.
e.g. Family tree
It is depicted upside down with root at the top and leaves at bottom.
The root is a node having no parent, It can have only a child nodes.
Leaves have no children.
A single node by itself is a tree. Root node
Root node
Edge
Leaf nodes or external nodes
Parent node
Subtree
Siblings
Each node is reachable from root, by having a path from root to leaf.
Tree is a collection of elements called nodes. So node is the main component of tree. It stores he actual data
along with link to other nodes.
Binary Tree:
A binary tree is a tree in which each node has at most two children i.e the left child and right child.
Data
Left child Right child
Binary Tree
A
B C
D E F
Properties of Binary Tree:
1. The number of external nodes is one more than the number of internal nodes.
2. The number of external nodes is at least h+1, h is the height of tree and at most 2h
.
3. The number of internal nodes is at least h, and at most 2h
-1.
4. The total number of nodes in binary tree is at least h+1 and at most 2h+1
-1
5. A binary tree with n nodes has exactly (n-1) edges
A
B C
D E F
Representation of binary tree:
1. Using array (linear)
2. Using linked list (link)
Array representation:
In this we use one dimensional array to represent a binary tree. The elements of tree are stored level by level
starting from the root node (root node is at level 0).
• The root of the tree will be in position 1 of the array (i.e. at position 0)
• The left child of a node at position n is at position 2n. (n=1, 2*1=2)
• The right child of a node at position n is at position 2n+1 (n=1, 2*1+1=3)
• The parent of a node at position n is at position n/2
B C
D F
A
E
Root node at level 0
A B C D E F
0
1
1 2 3 4 5 6
2 3 4 5 6 7
Linked list representation of binary tree:
In this we use double linked list to represent a binary tree. In double linked list the first field for storing left child
address, second field for storing data and third for storing right child address.
Data
Left child Right child
1008 1210
A
1004 NULL
B 1300 1018
C
NULL NULL
D NULL NULL
E NULL NULL
F
1000 root node
Operations on binary tree:
1. Insertion
2. Deletion
3. Traversal
Binary tree traversal:
It is the process of accessing(visiting) every node of the tree exactly once. A tree is defined in a recursive in a
recursive manner. Binary tree traversal are also defined recursively.
During creation we use three parameters Node(N), left subtree (L), right subtree (R).
There are three techniques for traversal:
4. Preorder traversal (NLR)
5. Inorder traversal (LNR)
6. Postorder traversal(LRN)
Preorder(NLR): -Visit the Node (N)
-Traverse the left subtree(L)
-Traverse the right subtree(R)
Inorder(LNR): -Traverse the left subtree(L)
-Visit the Node (N)
-Traverse the right subtree(R)
Postorder(LRN): -Traverse the left subtree(L)
-Traverse the right
subtree(R)
B C
D F
A
E
G
e.g. Binary tree
Preorder(NLR): A B D E G C F
B C
D F
A
E
G
Inorder(LNR): D B E G A F C
1
2
3
4
5
6
7
B C
D F
A
E
G
Postorder(LRN): D G E B F C A
1
2
3
4
5
6
7
There are two different ways of creating binary tree using traversals:
1. Preorder and inorder traversals
2. Postorder and inorder traversals
e.g. draw binary tree using following traversals:
Preorder(NLR): A B D H E C F G
Inorder (LNR): D H B E A F C G
Using preorder we determine which is the root node because root is the first node to be traversed so A is the
root node.
Using Inorder we determine the nodes to the left of root and the nodes to the right of root (i.e. LNR)
Preorder(NLR): A B D H E C F G
Inorder (LNR): D H B E A F C G
A
D H B E F C G
A
F C G
B
E
D H
Preorder(NLR): A B D H E C F G
Inorder (LNR): D H B E A F C G
A
F C G
B
H
D E
A
B
H
D E
C
F G
a). Preorder : F A E C K D H G B
Inorder : E A C K F H D B G
b). Postorder : H D I E B J F K L G C A
Inorder : H D B I E A F J C K G L
c). Inorder : B C A E G D H F I J
Preorder : A B C D E G F H I J
Binary Search Tree(BST):
Binary Search Tree is a binary tree data structure in which the nodes are arranged in a specific order i.e. value at
a node is greater than every value to the left of that node and less than every value to the right of that node.
BST properties:
• No two elements have same key
• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• The left and right subtree each must also be a binary search tree.
BST operations: -Searching : Finding the location of some specific element in a binary search tree.
-Insertion : Adding a new element to the binary search tree at the appropriate location so
that the property of BST do not violate.
-Deletion : Deleting some specific node from a binary search tree. However, there can be
various cases in deletion depending upon the number of children, the node
have.
20
>20
<20
A
B C
D E F
Create the binary search tree using the following data elements:
45, 21, 85, 99, 10, 64, 19
1.Insert 45 into the tree as the root of the tree.
2.Read the next element, if it is lesser than the root node element, insert it as the root of the left sub-tree.
3.Otherwise, insert it as the root of the right of the right sub-tree.
Step-1
45 45
Step-2
21
45
21
Step-3
85
45
21
Step-4
85
99
45
21
Step-5
85
99
10
45
21
Step-6
85
99
10 64
45
21
Step-7
85
99
10 64
19
typedef struct bst
{
int info;
struct bst *left;
struct bst *right;
}node;
//create function
node *create()
{
node *temp;
printf("nEnter data:");
temp=(node *)malloc(sizeof(node));
scanf("%d",&temp->info);
temp->left=temp->right=NULL;
return temp;
}
Searching:
It is used to search for a key stored in the tree.
Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key
value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow
the same algorithm for each node.
BST_SEARCH(x,k) : searches the tree root at x for a node whose key value equals to k. it returns a pointer to
node if it exists otherwise NULL.
BST_SEARCH(x,k)
1. If x=NULL or k=key[x]
then return x
2. if k<key[x]
then BST_SEARCH(left[x],k)
else
BST_SEARCH(right[x],k)
20 60
5 55
50
30
35
x
K=30, x!=NULL , key[x]=50 i.e. 30<50
K=30, x!=NULL , key[x]=20 i.e. 30>20
K=30, x!=NULL , key[x]=30 i.e. 30=30
x
x
/*BST search function*/
void search(node *root,int item)
{
if(root==NULL)
printf("nSearch unsuccessful");
else
{
if(root->info==item)
{
printf("nSearch successful:");
}
else
{
if(item<root->info)
search(root->left,item);
else
if(item>root->info)
search(root->right,item);
}
}
}
Insertion:
Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then
if the data is less than the key value, search for the empty location in the left subtree and insert the data.
Otherwise, search for the empty location in the right subtree and insert the data.
15 65
50
Insert 105
15 65
50
105
1. Create a new BST node and assign values to it.
2. insert(node, key)
i) If root == NULL,
return the new node to the calling function.
ii) if root->data < key
call the insert function with root->right and assign the return value in root->right.
root->right = insert(root->right,key)
iii) if root->data > key
call the insert function with root->left and assign the return value in root->left.
root->left = insert(root->left,key)
3. Finally, return the original root pointer to the calling function.
//BST insert function
void insert(node *root,node *temp)
{
if(temp->info<root->info)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(temp->info>root->info)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
Insert 105
1008 1210
50
NULL NULL
15 NULL NULL
65
1000 root
NULL NULL
105
1026 newnode
1008 1210
50
NULL NULL
15 NULL 1026
65
1000 root
NULL NULL
105
temp
Deletion:
To delete a node from BST, there are three cases:
1. Node having no children (leaf node)
2. Node having one children (right child or left child)
3. Node having two children
Case 1: Leaf node
If the node is leaf (both left and right will be NULL), remove the node directly and free its memory.
45
21 85
99
10 64
delete 64
45
21 85
99
10
Case 2: Node having one children (right child or left child)
If the node has only right child (left will be NULL), make the node points to the right node and free the
node.
If the node has only left child (right will be NULL), make the node points to the left node and free the node.
45
21 85
99
10 64
115
delete 99
45
21 85
10 64 115
Case 3: If the node has both left and right child
1. Find the smallest node in the right subtree. say min
2. Make node->data = min
3. Delete the min node.
45
21 85
10 64 115
delete 45
64
21 85
10 115
struct node* delete(struct node *root, int x)
{
if(root==NULL)
return NULL;
if (x>root->data)
root->right_child = delete(root->right_child, x);
else if(x<root->data)
root->left_child = delete(root->left_child, x);
else
{
//No Children
if(root->left_child==NULL && root->right_child==NULL)
{
free(root);
return NULL;
}
//One Child
else if(root->left_child==NULL || root->right_child==NULL)
{
struct node *temp;
if(root->left_child==NULL)
temp = root->right_child;
else
temp = root->left_child;
free(root);
return temp;
}
//Two Children
else
{
struct node *temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, temp->data);
}
}
return root;
}
AVL Tree:
It is named after their inventor Adelson, Velski & Landis. It is height balanced tree. It is a binary search tree
that has an additional balance condition. AVL tree checks the height of the left and the right sub-trees and
assures that the difference is not more than 1. This difference is called the Balance Factor.
For an AVL tree, the value of balance factor of any node is -1, 0 or 1. If it is other than these three values then
the tree is not balanced or it is not AVL tree
Balance Factor = height(left-sutree) − height(right-sutree)
In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the difference is 2. In
the third tree, the right subtree of A has height 2 and the left is missing, so it is 0, and the difference is 2 again.
AVL tree permits difference (balance factor) to be only 1.
B
A C
C
B
A
A
B
C
0
0 0
Balanced Not balanced Not balanced
0
1
2
0
-1
-2
If the difference in the height of left and right sub-trees is more than 1, the tree is balanced using some rotation
techniques.
AVL Rotations
To balance itself, an AVL tree may perform the following four kinds of rotations −
•Single Left rotation(LL rotation)
•Single Right rotation(RR rotation)
•Left-Right rotation
•Right-Left rotation
The first two rotations are single rotations and the next two rotations are double rotations.
Left 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 −
A
B
C
0
-1
-2 A
B
C
B
A C
0
0 0
Right unbalanced Left rotation Balanced
Right 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.
The unbalanced node becomes the right child of its left child by performing a right rotation.
A
B
C
0
1
2
B
A C
0
0 0
Left unbalanced Right rotation
Balanced
A
B
C
Left-Right Rotation:
Double rotations are slightly complex. In this unbalance occurred due to the insertion of node in the right subtree
of the left subtree of node. It involves two rotations-left rotation followed by right rotation.
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.
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.
C
A
B
2
-1
0
C
A
B
C
B
A
2
1
0
C
B
A
B
A C
0
0 0
Right-Left Rotation
The second type of double rotation is Right-Left Rotation. In this unbalance occurred due to the insertion of
node in the left subtree of the right subtree of node. It is a combination of right rotation followed by left
rotation.
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.
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.
A
C
B
-2
1
0
A
C
B
A
B
C
0
-1
-2 A
B
C
B
A C
0
0 0
Construct the AVL tree for the following set of data:
50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48
Step-01: Insert 50 Step-02: Insert 20
•As 20 < 50, so insert 20 in 50’s left sub tree.
Step-03: Insert 60
•As 60 > 50, so insert 60 in 50’s right sub tree.
Step-04: Insert 10
•As 10 < 50 , so insert 10 in 50’s left sub tree.
•As 10 < 20, so insert 10 in 20’s left sub tree.
0
0
1
0
0 0
0
0
1
1
50 50
20
Tree is balanced
Tree is balanced
50
20 60
Tree is balanced
50
20 60
10
Tree is balanced
Step-05: Insert 8
•As 8 < 50, so insert 8 in 50’s left sub tree.
•As 8 < 20, so insert 8 in 20’s left sub tree.
•As 8 < 10, so insert 8 in 10’s left sub tree.
0
0
1
2
2
To balance the tree,
•Find the first imbalanced node on the path from the newly inserted node (node 8) to the root node.
•The first imbalanced node is node 20.
•Now, count three nodes from node 20 in the direction of leaf node.
•Then, use AVL tree rotation to balance the tree.
0
0
0
0
1
50
20 60
10
8
Tree is imbalanced
50
20 60
10
8
50
10 60
8 20
Tree is imbalanced Tree is balanced
RR rotation
Step-06: Insert 15
•As 15 < 50, so insert 15 in 50’s left sub tree.
•As 15 > 10, so insert 15 in 10’s right sub tree.
•As 15 < 20, so insert 15 in 20’s left sub tree.
0
0
0
1
-1
2
To balance the tree,
•Find the first imbalanced node on the path from the newly inserted node (node 15) to the root node.
•The first imbalanced node is node 50.
•Now, count three nodes from node 50 in the direction of leaf node.
•Then, use AVL tree rotation to balance the tree. 0
0
-1
0
0
0
50
10 60
8 20
15
Tree is imbalanced
50
10 60
8 20
15
20
10 50
60
15
8
LR rotation
Tree is imbalanced Tree is balanced
Step-07: Insert 32
•As 32 > 20, so insert 32 in 20’s right sub tree.
•As 32 < 50, so insert 32 in 50’s left sub tree.
Step-08: Insert 46
•As 46 > 20, so insert 46 in 20’s right sub tree.
•As 46 < 50, so insert 46 in 50’s left sub tree.
•As 46 > 32, so insert 46 in 32’s right sub tree.
0
0
0 0
0
0 0
20
10 50
60
15
8 32
Tree is balanced
0
0
0
20
10 50
60
15
8 32
0
46
0
0
-1
1
Tree is balanced
Step-09: Insert 11
•As 11 < 20, so insert 11 in 20’s left sub tree.
•As 11 > 10, so insert 11 in 10’s right sub tree.
•As 11 < 15, so insert 11 in 15’s left sub tree.
Step-10: Insert 48
•As 48 > 20, so insert 48 in 20’s right sub tree.
•As 48 < 50, so insert 48 in 50’s left sub tree.
•As 48 > 32, so insert 48 in 32’s right sub tree.
•As 48 > 46, so insert 48 in 46’s right sub tree.
0
0
0
0
0
1
0
0
0
1
-1
-1
-2
2
-1
-1
20
10 50
60
15
8 32
46
Tree is balanced
11
0
1
-1
20
10 50
60
15
8 32
46
11
0
48
Tree is imbalanced
To balance the tree,
•Find the first imbalanced node on the path from the newly inserted node (node 48) to the root node.
•The first imbalanced node is node 32.
•Now, count three nodes from node 32 in the direction of leaf node.
•Then, use AVL tree rotation to balance the tree.
This is the final balanced AVL tree after inserting all the given elements.
0
0
0
0
0
0
1
-1
20
10 50
60
15
8 32
46
32
48
20
10 50
60
15
8
32
46
11
48
0
1
1
Tree is balanced
LL rotation
Deletion Operation(AVL tree):
The deletion of a node in AVL is exactly as the deletion of a node from BST.
• Search for the node to be deleted
• After deleti0n check the balance factor of each node
• If the tree is unbalance after deletion, to balance again AVL rotations rotations are used
Delete the node 30 from the AVL tree 20
10 30
15
8
0 0
0 0
1
20
30
15
8
10
Node to be deleted
0 0
0 0
1
Deleting node 30
20
15
8
10
0 0
0
2
Performing R0
rotation
Critical node
20
8
10
15
0
0 1
-1
B-tree:
It is a self-balanced search tree in which every node contains multiple keys and has more than
two children.
Insertion of node increases the height of tree. Access time of a tree totally dependent on the
level of the tree. So we minimize the access time through balance tree only.
There is a need to take all the leaf nodes at the same level and non-leaf nodes not contain
empty subtree.
A B-tree is also known as balanced M-way tree. It is used in external sorting.
For order n , maximum number of children be n and each node contain k keys, where k<=n-1
For balancing each node contains n/2 keys (except root)
20 30 20 55 80 20
Three way
n=3, k=n-1=2
Four way
n=4, k=n-1=3
Three way
n=2, k=n-1=1
B-tree is a special type of search tree in which a node contains more than one value and
more than two children
B-Tree of Order n has the following properties...
• All leaf nodes must be at same level.
• All nodes except root must have at least [n/2]-1 keys and maximum of n-1 keys.
• All non leaf nodes except root (i.e. all internal nodes) must have at least n/2 children.
• If the root node is a non leaf node, then it must have atleast 2 children.
• A non leaf node with n-1 keys must have n number of children.
• All the key values in a node must be in Ascending Order.
Application of B-tree:
The main application of B-tree is the organization of huge collection of records into a file
structure. In this insertion, deletion and modification can be carried out perfectly and
efficiently.
20
30 40
5 10
4 6 8 12 14 25 38 42 68
All leaf nodes are at same level.
All non-leaf nodes have no empty subtree and they have keys 1 less than the number of
children
.
Operations on a B-Tree
The following operations are performed on a B-Tree...
1.Insertion
2.Deletion
3.Search
Insertion Operation in B-Tree
In a B-Tree, a new element must be added only at the leaf node. That means, the new key Value is always
attached to the leaf node only.
The insertion operation is performed as follows:
Step 1 - Check whether tree is Empty.
Step 2 - If tree is Empty, then create a new node with new key value and insert it into the tree as a root node.
Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is added using Binary
Search Tree logic.
Step 4 - If that leaf node has empty position, add the new key value to that leaf node in ascending order of key
value within the node.
Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its parent node. Repeat
the same until the sending value is fixed into a node.
Step 6 - If the splitting is performed at root node then the middle value becomes new root node for the tree and
the height of the tree is increased by one.
Construct a B-tree of order 3 using following
data:
1, 2, 3, 4, 5, 6, 7, 8, 9
(each node contain 2 data values)
Insert 1:
Insert 2:
Insert 3:
Insert 4:
1
1 2
2
1 3
2
1 3 4
Insert 5:
Insert 6:
Insert 7:
2 4
1 5
3
2 4
1 5 6
3
1 2 3
2
1 3 4 5
2 4
1 5 6 7
3
Insert 8:
4
2 6
1 5 7
3
4
2 6
1 5 7 8
3
Insert 9: 4
2 6
1 5 7 8 9
3
4
2 6 8
1 5 9
3 7
e.g.
Construct a B-tree of order 5 with following data:
D, H, Z, K, B, P, Q, E, A, S, W, T, C
B+Tree:
It is an extension of B-tree which allows efficient insertion, deletion and search operations.
In B-tree keys are stored in internal as well as external(leaf) nodes, whereas in B+ tree data(key) can be stored
on the leaf nodes.
A B+Tree is called a balanced tree because all leaves are on the same level and every path from a root to leaf
is of same length.
The main drawback of B-tree is the difficulty of traversing the keys sequentially. B+Tree retains the rapid
access property of the B-Tree, allowing rapid sequential access.
In B+Tree all keys are maintained in leaves and keys are replicated in non-leaf nodes to define the path for
locating individual records. The leaves are linked together to provide a sequential path for traversing the keys
in the tree.
B+Tree are used to store large amount of data which cannot be stored in main memory (size of main memory
is limited). The internal nodes(key to access records) whereas leaf nodes are stored in secondary memory.
The internal nodes of B+Tree are called index nodes.
B+Tree:
It is a structure of nodes linked by pointers.
There is a unique path to each leaf and all paths are of equal length.
Stores keys only at leaves and stores reference values in other internal nodes.
Less disk access time due to fever levels in the tree.
It provides faster sequential access of data.
50
35 70
25 30 40 45 59 65 80 90
B+ Tree
Advantages Of B+ Trees
•Data can be fetched in an equal number of disk accesses.
•Compared to the B tree, the height of the B+ tree is less and remains balanced.
•We use keys for indexing.
•Data in the B+ tree can be accessed sequentially or directly as the leaf nodes are arranged in a linked list.
•Search is faster as data is stored in leaf nodes only and as a linked list.
B-Tree B+ Tree
Data is stored in leaf nodes as well as internal nodes. Data is stored only in leaf nodes.
Searching is a bit slower as data is stored in internal as well
as leaf nodes.
Searching is faster as the data is stored only in the leaf nodes.
No redundant search keys are present. Redundant search keys may be present.
Deletion operation is complex. Deletion operation is easy as data can be directly deleted
from the leaf nodes.
Leaf nodes cannot be linked together. Leaf nodes are linked together to form a linked list.
Difference Between B-Tree And B+ Tree
Insertion in B+Tree:
1. Find the leaf node in which key value of the node has to be inserted
2. If key value already exist, no more insertion
Else if the key value does not exist , insert key value in leaf node in an ordered fashion
3. When a node is split, the middle key is retained in the left half as well as promoted to the parent node
B+ Tree of order 4:
Insert 18:
20 40 60
10 12 20 25 30 40 45 60 65 70 80
40
12 20 60
10 12 18 20 25 30 40 45 60 65 70 80
Deletion in B+ Tree:
1. Delete the key and data from the leaves.
2. If the leaf node contains less than minimum number of elements, merge down the node with its sibling and
delete the key in between them.
3. If the index node contains less than minimum number of elements, merge the node with the sibling and move
down the key in between them.
Consider B+ Tree of order 5:
Delete 210
210 is present in the right sub-tree of 190, after 195. delete it.
Merge the two nodes by using 195, 190, 138 and 125.
108
60 78 120 190
40 50 54 65 70 83 95 110 115 125 138 195 210
Merge the two nodes by using 195, 190, 138 and 125.
Now, element 120 is the single element present in the node which is violating the B+ Tree properties.
Therefore, we need to merge it by using 60, 78, 108 and 120.
Now, the height of B+ tree will be decreased by 1.
108
60 78 120
40 50 54 65 70 83 95 110 115 125 138 190 195
40 50 54 65 70 83 95 110 115 125 138 190 195
60 78 108 120
Threaded Binary Tree:
A binary tree can be represented by using an array or linked list. When binary tree is
represented using linked , if any node I is not having a child we use a NULL pointer. At least
half of the entries in Left and Right pointer will contain NULL entries.
These NULL pointer does not play any role except indicating there is no child (Link).
This space may be more efficiently used by replacing NULL pointer entries by special pointer,
called Threads, which points to the node higher in the tree. Such trees are called Threaded
Binary Tree.
Threaded Binary Tree is also a binary tree in which left child pointers that are NULL (in linked
list representation) points to its in-order predecessor and right child pointers that are NULL
points to its in-order successor.
If there is no predecessor or successor then points to the root node.
There are three way to thread a binary tree:
1. In an in-order traversal when the right pointer in NULL then it can be replaced by a
thread to the successor of that node and it is called Right Threaded Tree.
2. In an in-order traversal when the left pointer is NULL then it is replaced by a thread to
the predecessor of that node and it is Left Threaded Tree.
3. In an in-order traversal when both left and right pointers are NULL then they are
replaced by threads points to predecessor and successor of that node respectively. It is
called Fully Threaded Tree.
When one thread is used it is called one way threaded tree and when both the threads are
used it is called two way threaded tree.
The idea threaded binary tree is to make in-order traversal faster and do it witout stack and
without recursion.
A
B C
D F
E
Right Threaded Tree:
A
B C
D E F
No successor
Inorder Traversal: D B E A F C
A
B C
D F
E
Left Threaded Tree:
A
B C
D E F
G
G
Inorder Traversal: D B G E A F C
A
B C
D F
E
Fully Threaded Tree:
A
B C
D E F
G
G
Inorder Traversal: D B G E A F C
Preorder : F A E C K D H G B
Inorder : E A C K F H D B G
F
E A C K H D B G
F
A H D B G
E C K
F
A H D B G
E C
K
F
A H D B G
E C
K
Preorder : F A E C K D H G B
Inorder : E A C K F H D B G
F
A D
E C
K
H B G
F
A D
E C
K
H G
B
Postorder : H D I E B J F K L G C A
Inorder : H D B I E A F J C K G L
A
H D B I E F J C K G L
A
B F J C K G L
H D I E
A
B F J C K G L
D I E
H
A
B F J C K G L
D I E
H
A
B F J C K G L
D I E
H
Postorder : H D I E B J F K L G C A
Inorder : H D B I E A F J C K G L
A
B F J C K G L
D E
H I
A
B
D E
H I
F J K G L
C
A
B
D E
H I
G
C
F
J K L
Inorder : B C A E G D H F I J
Preorder : A B C D E G F H I J
A
B C E G D H F I J
A
B E G D H F I J
C
A
B D
C E G H F I J
A
B D
C E H F I J
G
A
B D
C E F
G H I J
A
B D
C E F
G H I
J

More Related Content

PPTX
PDF
Unit iv data structure-converted
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
PPTX
Lecture_10 - Revised.pptx
PPTX
learn tree, linked list, queue, stack, and other algo
PDF
PPTX
datastructurestreeand type of trees.pptx
PPTX
Binary search tree
Unit iv data structure-converted
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
Lecture_10 - Revised.pptx
learn tree, linked list, queue, stack, and other algo
datastructurestreeand type of trees.pptx
Binary search tree

Similar to UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE (20)

DOCX
Biary search Tree.docx
PPTX
BST.pptx this is Good for data structure
PPTX
BST.pptx this isp used for learning binary search trees
PPTX
Binary Search Tree
PDF
Lecture notes data structures tree
PDF
Trees, Binary Search Tree, AVL Tree in Data Structures
PPT
Algorithm and Data Structure - Binary Trees
PPTX
Binary Search Tree.pptx
PPTX
PPTX
Binary tree
PDF
Binary search tree
PPT
Trees
PPT
BINARY SEARCH TREE
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPT
Lecture 7-BinarySearchTrees.ppt
PPTX
Tree structure and its definitions with an example
PDF
bst-copy-171118141913.pdf
PPTX
Binary Search Tree
PDF
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
PPTX
Week 8 (trees)
Biary search Tree.docx
BST.pptx this is Good for data structure
BST.pptx this isp used for learning binary search trees
Binary Search Tree
Lecture notes data structures tree
Trees, Binary Search Tree, AVL Tree in Data Structures
Algorithm and Data Structure - Binary Trees
Binary Search Tree.pptx
Binary tree
Binary search tree
Trees
BINARY SEARCH TREE
UNIT III Non Linear Data Structures - Trees.pptx
Lecture 7-BinarySearchTrees.ppt
Tree structure and its definitions with an example
bst-copy-171118141913.pdf
Binary Search Tree
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Week 8 (trees)
Ad

Recently uploaded (20)

PPTX
web development for engineering and engineering
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Glazing at Facade, functions, types of glazing
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
PPTX
anatomy of limbus and anterior chamber .pptx
PDF
ETO & MEO Certificate of Competency Questions and Answers
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PDF
International Journal of Information Technology Convergence and Services (IJI...
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
web development for engineering and engineering
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Lesson 3_Tessellation.pptx finite Mathematics
Glazing at Facade, functions, types of glazing
Model Code of Practice - Construction Work - 21102022 .pdf
July 2025: Top 10 Read Articles Advanced Information Technology
anatomy of limbus and anterior chamber .pptx
ETO & MEO Certificate of Competency Questions and Answers
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
International Journal of Information Technology Convergence and Services (IJI...
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Road Safety tips for School Kids by a k maurya.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Monitoring Global Terrestrial Surface Water Height using Remote Sensing - ARS...
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Ad

UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE

  • 2. Trees: • Definition of Tree • Properties of Tree • Binary Tree • Representation of Tree using array and linked lists • Operations on binary tree • Binary tree traversal (recursive) • Binary Search Tree (BST) • B-Tree • B+ Tree • AVL Tree • Threaded Binary Tree
  • 3. Tree: It is non-linear data structure. It represents a hierarchical data structure. It represents a parent child relationship between various pieces of data and thus allow us to arrange our records of data and files in a hierarchical fashion. e.g. Family tree It is depicted upside down with root at the top and leaves at bottom. The root is a node having no parent, It can have only a child nodes. Leaves have no children. A single node by itself is a tree. Root node Root node Edge Leaf nodes or external nodes Parent node Subtree Siblings
  • 4. Each node is reachable from root, by having a path from root to leaf. Tree is a collection of elements called nodes. So node is the main component of tree. It stores he actual data along with link to other nodes. Binary Tree: A binary tree is a tree in which each node has at most two children i.e the left child and right child. Data Left child Right child Binary Tree A B C D E F
  • 5. Properties of Binary Tree: 1. The number of external nodes is one more than the number of internal nodes. 2. The number of external nodes is at least h+1, h is the height of tree and at most 2h . 3. The number of internal nodes is at least h, and at most 2h -1. 4. The total number of nodes in binary tree is at least h+1 and at most 2h+1 -1 5. A binary tree with n nodes has exactly (n-1) edges A B C D E F
  • 6. Representation of binary tree: 1. Using array (linear) 2. Using linked list (link) Array representation: In this we use one dimensional array to represent a binary tree. The elements of tree are stored level by level starting from the root node (root node is at level 0). • The root of the tree will be in position 1 of the array (i.e. at position 0) • The left child of a node at position n is at position 2n. (n=1, 2*1=2) • The right child of a node at position n is at position 2n+1 (n=1, 2*1+1=3) • The parent of a node at position n is at position n/2 B C D F A E Root node at level 0 A B C D E F 0 1 1 2 3 4 5 6 2 3 4 5 6 7
  • 7. Linked list representation of binary tree: In this we use double linked list to represent a binary tree. In double linked list the first field for storing left child address, second field for storing data and third for storing right child address. Data Left child Right child 1008 1210 A 1004 NULL B 1300 1018 C NULL NULL D NULL NULL E NULL NULL F 1000 root node
  • 8. Operations on binary tree: 1. Insertion 2. Deletion 3. Traversal Binary tree traversal: It is the process of accessing(visiting) every node of the tree exactly once. A tree is defined in a recursive in a recursive manner. Binary tree traversal are also defined recursively. During creation we use three parameters Node(N), left subtree (L), right subtree (R). There are three techniques for traversal: 4. Preorder traversal (NLR) 5. Inorder traversal (LNR) 6. Postorder traversal(LRN) Preorder(NLR): -Visit the Node (N) -Traverse the left subtree(L) -Traverse the right subtree(R) Inorder(LNR): -Traverse the left subtree(L) -Visit the Node (N) -Traverse the right subtree(R) Postorder(LRN): -Traverse the left subtree(L) -Traverse the right subtree(R)
  • 9. B C D F A E G e.g. Binary tree Preorder(NLR): A B D E G C F B C D F A E G Inorder(LNR): D B E G A F C 1 2 3 4 5 6 7 B C D F A E G Postorder(LRN): D G E B F C A 1 2 3 4 5 6 7
  • 10. There are two different ways of creating binary tree using traversals: 1. Preorder and inorder traversals 2. Postorder and inorder traversals e.g. draw binary tree using following traversals: Preorder(NLR): A B D H E C F G Inorder (LNR): D H B E A F C G Using preorder we determine which is the root node because root is the first node to be traversed so A is the root node. Using Inorder we determine the nodes to the left of root and the nodes to the right of root (i.e. LNR) Preorder(NLR): A B D H E C F G Inorder (LNR): D H B E A F C G A D H B E F C G
  • 11. A F C G B E D H Preorder(NLR): A B D H E C F G Inorder (LNR): D H B E A F C G A F C G B H D E A B H D E C F G
  • 12. a). Preorder : F A E C K D H G B Inorder : E A C K F H D B G b). Postorder : H D I E B J F K L G C A Inorder : H D B I E A F J C K G L c). Inorder : B C A E G D H F I J Preorder : A B C D E G F H I J
  • 13. Binary Search Tree(BST): Binary Search Tree is a binary tree data structure in which the nodes are arranged in a specific order i.e. value at a node is greater than every value to the left of that node and less than every value to the right of that node. BST properties: • No two elements have same key • The left subtree of a node contains only nodes with keys lesser than the node’s key. • The right subtree of a node contains only nodes with keys greater than the node’s key. • The left and right subtree each must also be a binary search tree. BST operations: -Searching : Finding the location of some specific element in a binary search tree. -Insertion : Adding a new element to the binary search tree at the appropriate location so that the property of BST do not violate. -Deletion : Deleting some specific node from a binary search tree. However, there can be various cases in deletion depending upon the number of children, the node have. 20 >20 <20 A B C D E F
  • 14. Create the binary search tree using the following data elements: 45, 21, 85, 99, 10, 64, 19 1.Insert 45 into the tree as the root of the tree. 2.Read the next element, if it is lesser than the root node element, insert it as the root of the left sub-tree. 3.Otherwise, insert it as the root of the right of the right sub-tree. Step-1 45 45 Step-2 21 45 21 Step-3 85 45 21 Step-4 85 99 45 21 Step-5 85 99 10 45 21 Step-6 85 99 10 64 45 21 Step-7 85 99 10 64 19
  • 15. typedef struct bst { int info; struct bst *left; struct bst *right; }node; //create function node *create() { node *temp; printf("nEnter data:"); temp=(node *)malloc(sizeof(node)); scanf("%d",&temp->info); temp->left=temp->right=NULL; return temp; }
  • 16. Searching: It is used to search for a key stored in the tree. Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node. BST_SEARCH(x,k) : searches the tree root at x for a node whose key value equals to k. it returns a pointer to node if it exists otherwise NULL. BST_SEARCH(x,k) 1. If x=NULL or k=key[x] then return x 2. if k<key[x] then BST_SEARCH(left[x],k) else BST_SEARCH(right[x],k) 20 60 5 55 50 30 35 x K=30, x!=NULL , key[x]=50 i.e. 30<50 K=30, x!=NULL , key[x]=20 i.e. 30>20 K=30, x!=NULL , key[x]=30 i.e. 30=30 x x
  • 17. /*BST search function*/ void search(node *root,int item) { if(root==NULL) printf("nSearch unsuccessful"); else { if(root->info==item) { printf("nSearch successful:"); } else { if(item<root->info) search(root->left,item); else if(item>root->info) search(root->right,item); } } }
  • 18. Insertion: Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right subtree and insert the data. 15 65 50 Insert 105 15 65 50 105 1. Create a new BST node and assign values to it. 2. insert(node, key) i) If root == NULL, return the new node to the calling function. ii) if root->data < key call the insert function with root->right and assign the return value in root->right. root->right = insert(root->right,key) iii) if root->data > key call the insert function with root->left and assign the return value in root->left. root->left = insert(root->left,key) 3. Finally, return the original root pointer to the calling function.
  • 19. //BST insert function void insert(node *root,node *temp) { if(temp->info<root->info) { if(root->left!=NULL) insert(root->left,temp); else root->left=temp; } if(temp->info>root->info) { if(root->right!=NULL) insert(root->right,temp); else root->right=temp; } } Insert 105 1008 1210 50 NULL NULL 15 NULL NULL 65 1000 root NULL NULL 105 1026 newnode 1008 1210 50 NULL NULL 15 NULL 1026 65 1000 root NULL NULL 105 temp
  • 20. Deletion: To delete a node from BST, there are three cases: 1. Node having no children (leaf node) 2. Node having one children (right child or left child) 3. Node having two children Case 1: Leaf node If the node is leaf (both left and right will be NULL), remove the node directly and free its memory. 45 21 85 99 10 64 delete 64 45 21 85 99 10
  • 21. Case 2: Node having one children (right child or left child) If the node has only right child (left will be NULL), make the node points to the right node and free the node. If the node has only left child (right will be NULL), make the node points to the left node and free the node. 45 21 85 99 10 64 115 delete 99 45 21 85 10 64 115
  • 22. Case 3: If the node has both left and right child 1. Find the smallest node in the right subtree. say min 2. Make node->data = min 3. Delete the min node. 45 21 85 10 64 115 delete 45 64 21 85 10 115
  • 23. struct node* delete(struct node *root, int x) { if(root==NULL) return NULL; if (x>root->data) root->right_child = delete(root->right_child, x); else if(x<root->data) root->left_child = delete(root->left_child, x); else { //No Children if(root->left_child==NULL && root->right_child==NULL) { free(root); return NULL; } //One Child else if(root->left_child==NULL || root->right_child==NULL) { struct node *temp; if(root->left_child==NULL) temp = root->right_child; else temp = root->left_child; free(root); return temp; } //Two Children else { struct node *temp = find_minimum(root->right_child); root->data = temp->data; root->right_child = delete(root->right_child, temp->data); } } return root; }
  • 24. AVL Tree: It is named after their inventor Adelson, Velski & Landis. It is height balanced tree. It is a binary search tree that has an additional balance condition. AVL tree checks the height of the left and the right sub-trees and assures that the difference is not more than 1. This difference is called the Balance Factor. For an AVL tree, the value of balance factor of any node is -1, 0 or 1. If it is other than these three values then the tree is not balanced or it is not AVL tree Balance Factor = height(left-sutree) − height(right-sutree) In the second tree, the left subtree of C has height 2 and the right subtree has height 0, so the difference is 2. In the third tree, the right subtree of A has height 2 and the left is missing, so it is 0, and the difference is 2 again. AVL tree permits difference (balance factor) to be only 1. B A C C B A A B C 0 0 0 Balanced Not balanced Not balanced 0 1 2 0 -1 -2
  • 25. If the difference in the height of left and right sub-trees is more than 1, the tree is balanced using some rotation techniques. AVL Rotations To balance itself, an AVL tree may perform the following four kinds of rotations − •Single Left rotation(LL rotation) •Single Right rotation(RR rotation) •Left-Right rotation •Right-Left rotation The first two rotations are single rotations and the next two rotations are double rotations. Left 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 − A B C 0 -1 -2 A B C B A C 0 0 0 Right unbalanced Left rotation Balanced
  • 26. Right 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. The unbalanced node becomes the right child of its left child by performing a right rotation. A B C 0 1 2 B A C 0 0 0 Left unbalanced Right rotation Balanced A B C
  • 27. Left-Right Rotation: Double rotations are slightly complex. In this unbalance occurred due to the insertion of node in the right subtree of the left subtree of node. It involves two rotations-left rotation followed by right rotation. 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. 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. C A B 2 -1 0 C A B C B A 2 1 0 C B A B A C 0 0 0
  • 28. Right-Left Rotation The second type of double rotation is Right-Left Rotation. In this unbalance occurred due to the insertion of node in the left subtree of the right subtree of node. It is a combination of right rotation followed by left rotation. 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. 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. A C B -2 1 0 A C B A B C 0 -1 -2 A B C B A C 0 0 0
  • 29. Construct the AVL tree for the following set of data: 50 , 20 , 60 , 10 , 8 , 15 , 32 , 46 , 11 , 48 Step-01: Insert 50 Step-02: Insert 20 •As 20 < 50, so insert 20 in 50’s left sub tree. Step-03: Insert 60 •As 60 > 50, so insert 60 in 50’s right sub tree. Step-04: Insert 10 •As 10 < 50 , so insert 10 in 50’s left sub tree. •As 10 < 20, so insert 10 in 20’s left sub tree. 0 0 1 0 0 0 0 0 1 1 50 50 20 Tree is balanced Tree is balanced 50 20 60 Tree is balanced 50 20 60 10 Tree is balanced
  • 30. Step-05: Insert 8 •As 8 < 50, so insert 8 in 50’s left sub tree. •As 8 < 20, so insert 8 in 20’s left sub tree. •As 8 < 10, so insert 8 in 10’s left sub tree. 0 0 1 2 2 To balance the tree, •Find the first imbalanced node on the path from the newly inserted node (node 8) to the root node. •The first imbalanced node is node 20. •Now, count three nodes from node 20 in the direction of leaf node. •Then, use AVL tree rotation to balance the tree. 0 0 0 0 1 50 20 60 10 8 Tree is imbalanced 50 20 60 10 8 50 10 60 8 20 Tree is imbalanced Tree is balanced RR rotation
  • 31. Step-06: Insert 15 •As 15 < 50, so insert 15 in 50’s left sub tree. •As 15 > 10, so insert 15 in 10’s right sub tree. •As 15 < 20, so insert 15 in 20’s left sub tree. 0 0 0 1 -1 2 To balance the tree, •Find the first imbalanced node on the path from the newly inserted node (node 15) to the root node. •The first imbalanced node is node 50. •Now, count three nodes from node 50 in the direction of leaf node. •Then, use AVL tree rotation to balance the tree. 0 0 -1 0 0 0 50 10 60 8 20 15 Tree is imbalanced 50 10 60 8 20 15 20 10 50 60 15 8 LR rotation Tree is imbalanced Tree is balanced
  • 32. Step-07: Insert 32 •As 32 > 20, so insert 32 in 20’s right sub tree. •As 32 < 50, so insert 32 in 50’s left sub tree. Step-08: Insert 46 •As 46 > 20, so insert 46 in 20’s right sub tree. •As 46 < 50, so insert 46 in 50’s left sub tree. •As 46 > 32, so insert 46 in 32’s right sub tree. 0 0 0 0 0 0 0 20 10 50 60 15 8 32 Tree is balanced 0 0 0 20 10 50 60 15 8 32 0 46 0 0 -1 1 Tree is balanced
  • 33. Step-09: Insert 11 •As 11 < 20, so insert 11 in 20’s left sub tree. •As 11 > 10, so insert 11 in 10’s right sub tree. •As 11 < 15, so insert 11 in 15’s left sub tree. Step-10: Insert 48 •As 48 > 20, so insert 48 in 20’s right sub tree. •As 48 < 50, so insert 48 in 50’s left sub tree. •As 48 > 32, so insert 48 in 32’s right sub tree. •As 48 > 46, so insert 48 in 46’s right sub tree. 0 0 0 0 0 1 0 0 0 1 -1 -1 -2 2 -1 -1 20 10 50 60 15 8 32 46 Tree is balanced 11 0 1 -1 20 10 50 60 15 8 32 46 11 0 48 Tree is imbalanced
  • 34. To balance the tree, •Find the first imbalanced node on the path from the newly inserted node (node 48) to the root node. •The first imbalanced node is node 32. •Now, count three nodes from node 32 in the direction of leaf node. •Then, use AVL tree rotation to balance the tree. This is the final balanced AVL tree after inserting all the given elements. 0 0 0 0 0 0 1 -1 20 10 50 60 15 8 32 46 32 48 20 10 50 60 15 8 32 46 11 48 0 1 1 Tree is balanced LL rotation
  • 35. Deletion Operation(AVL tree): The deletion of a node in AVL is exactly as the deletion of a node from BST. • Search for the node to be deleted • After deleti0n check the balance factor of each node • If the tree is unbalance after deletion, to balance again AVL rotations rotations are used
  • 36. Delete the node 30 from the AVL tree 20 10 30 15 8 0 0 0 0 1 20 30 15 8 10 Node to be deleted 0 0 0 0 1 Deleting node 30 20 15 8 10 0 0 0 2 Performing R0 rotation Critical node 20 8 10 15 0 0 1 -1
  • 37. B-tree: It is a self-balanced search tree in which every node contains multiple keys and has more than two children. Insertion of node increases the height of tree. Access time of a tree totally dependent on the level of the tree. So we minimize the access time through balance tree only. There is a need to take all the leaf nodes at the same level and non-leaf nodes not contain empty subtree. A B-tree is also known as balanced M-way tree. It is used in external sorting. For order n , maximum number of children be n and each node contain k keys, where k<=n-1 For balancing each node contains n/2 keys (except root) 20 30 20 55 80 20 Three way n=3, k=n-1=2 Four way n=4, k=n-1=3 Three way n=2, k=n-1=1
  • 38. B-tree is a special type of search tree in which a node contains more than one value and more than two children B-Tree of Order n has the following properties... • All leaf nodes must be at same level. • All nodes except root must have at least [n/2]-1 keys and maximum of n-1 keys. • All non leaf nodes except root (i.e. all internal nodes) must have at least n/2 children. • If the root node is a non leaf node, then it must have atleast 2 children. • A non leaf node with n-1 keys must have n number of children. • All the key values in a node must be in Ascending Order. Application of B-tree: The main application of B-tree is the organization of huge collection of records into a file structure. In this insertion, deletion and modification can be carried out perfectly and efficiently.
  • 39. 20 30 40 5 10 4 6 8 12 14 25 38 42 68 All leaf nodes are at same level. All non-leaf nodes have no empty subtree and they have keys 1 less than the number of children .
  • 40. Operations on a B-Tree The following operations are performed on a B-Tree... 1.Insertion 2.Deletion 3.Search Insertion Operation in B-Tree In a B-Tree, a new element must be added only at the leaf node. That means, the new key Value is always attached to the leaf node only. The insertion operation is performed as follows: Step 1 - Check whether tree is Empty. Step 2 - If tree is Empty, then create a new node with new key value and insert it into the tree as a root node. Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is added using Binary Search Tree logic. Step 4 - If that leaf node has empty position, add the new key value to that leaf node in ascending order of key value within the node. Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its parent node. Repeat the same until the sending value is fixed into a node. Step 6 - If the splitting is performed at root node then the middle value becomes new root node for the tree and the height of the tree is increased by one.
  • 41. Construct a B-tree of order 3 using following data: 1, 2, 3, 4, 5, 6, 7, 8, 9 (each node contain 2 data values) Insert 1: Insert 2: Insert 3: Insert 4: 1 1 2 2 1 3 2 1 3 4 Insert 5: Insert 6: Insert 7: 2 4 1 5 3 2 4 1 5 6 3 1 2 3 2 1 3 4 5 2 4 1 5 6 7 3
  • 42. Insert 8: 4 2 6 1 5 7 3 4 2 6 1 5 7 8 3 Insert 9: 4 2 6 1 5 7 8 9 3 4 2 6 8 1 5 9 3 7
  • 43. e.g. Construct a B-tree of order 5 with following data: D, H, Z, K, B, P, Q, E, A, S, W, T, C
  • 44. B+Tree: It is an extension of B-tree which allows efficient insertion, deletion and search operations. In B-tree keys are stored in internal as well as external(leaf) nodes, whereas in B+ tree data(key) can be stored on the leaf nodes. A B+Tree is called a balanced tree because all leaves are on the same level and every path from a root to leaf is of same length. The main drawback of B-tree is the difficulty of traversing the keys sequentially. B+Tree retains the rapid access property of the B-Tree, allowing rapid sequential access. In B+Tree all keys are maintained in leaves and keys are replicated in non-leaf nodes to define the path for locating individual records. The leaves are linked together to provide a sequential path for traversing the keys in the tree. B+Tree are used to store large amount of data which cannot be stored in main memory (size of main memory is limited). The internal nodes(key to access records) whereas leaf nodes are stored in secondary memory. The internal nodes of B+Tree are called index nodes.
  • 45. B+Tree: It is a structure of nodes linked by pointers. There is a unique path to each leaf and all paths are of equal length. Stores keys only at leaves and stores reference values in other internal nodes. Less disk access time due to fever levels in the tree. It provides faster sequential access of data. 50 35 70 25 30 40 45 59 65 80 90 B+ Tree
  • 46. Advantages Of B+ Trees •Data can be fetched in an equal number of disk accesses. •Compared to the B tree, the height of the B+ tree is less and remains balanced. •We use keys for indexing. •Data in the B+ tree can be accessed sequentially or directly as the leaf nodes are arranged in a linked list. •Search is faster as data is stored in leaf nodes only and as a linked list. B-Tree B+ Tree Data is stored in leaf nodes as well as internal nodes. Data is stored only in leaf nodes. Searching is a bit slower as data is stored in internal as well as leaf nodes. Searching is faster as the data is stored only in the leaf nodes. No redundant search keys are present. Redundant search keys may be present. Deletion operation is complex. Deletion operation is easy as data can be directly deleted from the leaf nodes. Leaf nodes cannot be linked together. Leaf nodes are linked together to form a linked list. Difference Between B-Tree And B+ Tree
  • 47. Insertion in B+Tree: 1. Find the leaf node in which key value of the node has to be inserted 2. If key value already exist, no more insertion Else if the key value does not exist , insert key value in leaf node in an ordered fashion 3. When a node is split, the middle key is retained in the left half as well as promoted to the parent node B+ Tree of order 4: Insert 18: 20 40 60 10 12 20 25 30 40 45 60 65 70 80 40 12 20 60 10 12 18 20 25 30 40 45 60 65 70 80
  • 48. Deletion in B+ Tree: 1. Delete the key and data from the leaves. 2. If the leaf node contains less than minimum number of elements, merge down the node with its sibling and delete the key in between them. 3. If the index node contains less than minimum number of elements, merge the node with the sibling and move down the key in between them. Consider B+ Tree of order 5: Delete 210 210 is present in the right sub-tree of 190, after 195. delete it. Merge the two nodes by using 195, 190, 138 and 125. 108 60 78 120 190 40 50 54 65 70 83 95 110 115 125 138 195 210
  • 49. Merge the two nodes by using 195, 190, 138 and 125. Now, element 120 is the single element present in the node which is violating the B+ Tree properties. Therefore, we need to merge it by using 60, 78, 108 and 120. Now, the height of B+ tree will be decreased by 1. 108 60 78 120 40 50 54 65 70 83 95 110 115 125 138 190 195 40 50 54 65 70 83 95 110 115 125 138 190 195 60 78 108 120
  • 50. Threaded Binary Tree: A binary tree can be represented by using an array or linked list. When binary tree is represented using linked , if any node I is not having a child we use a NULL pointer. At least half of the entries in Left and Right pointer will contain NULL entries. These NULL pointer does not play any role except indicating there is no child (Link). This space may be more efficiently used by replacing NULL pointer entries by special pointer, called Threads, which points to the node higher in the tree. Such trees are called Threaded Binary Tree. Threaded Binary Tree is also a binary tree in which left child pointers that are NULL (in linked list representation) points to its in-order predecessor and right child pointers that are NULL points to its in-order successor. If there is no predecessor or successor then points to the root node.
  • 51. There are three way to thread a binary tree: 1. In an in-order traversal when the right pointer in NULL then it can be replaced by a thread to the successor of that node and it is called Right Threaded Tree. 2. In an in-order traversal when the left pointer is NULL then it is replaced by a thread to the predecessor of that node and it is Left Threaded Tree. 3. In an in-order traversal when both left and right pointers are NULL then they are replaced by threads points to predecessor and successor of that node respectively. It is called Fully Threaded Tree. When one thread is used it is called one way threaded tree and when both the threads are used it is called two way threaded tree. The idea threaded binary tree is to make in-order traversal faster and do it witout stack and without recursion.
  • 52. A B C D F E Right Threaded Tree: A B C D E F No successor Inorder Traversal: D B E A F C
  • 53. A B C D F E Left Threaded Tree: A B C D E F G G Inorder Traversal: D B G E A F C
  • 54. A B C D F E Fully Threaded Tree: A B C D E F G G Inorder Traversal: D B G E A F C
  • 55. Preorder : F A E C K D H G B Inorder : E A C K F H D B G F E A C K H D B G F A H D B G E C K F A H D B G E C K
  • 56. F A H D B G E C K Preorder : F A E C K D H G B Inorder : E A C K F H D B G F A D E C K H B G F A D E C K H G B
  • 57. Postorder : H D I E B J F K L G C A Inorder : H D B I E A F J C K G L A H D B I E F J C K G L A B F J C K G L H D I E A B F J C K G L D I E H A B F J C K G L D I E H
  • 58. A B F J C K G L D I E H Postorder : H D I E B J F K L G C A Inorder : H D B I E A F J C K G L A B F J C K G L D E H I A B D E H I F J K G L C A B D E H I G C F J K L
  • 59. Inorder : B C A E G D H F I J Preorder : A B C D E G F H I J A B C E G D H F I J A B E G D H F I J C A B D C E G H F I J A B D C E H F I J G A B D C E F G H I J A B D C E F G H I J