0% found this document useful (0 votes)
58 views235 pages

DS Unit-3

This document discusses trees as a non-linear data structure that is faster than linear data structures and provides a more natural fit for hierarchical data. It defines key tree terminology like nodes, edges, root, internal nodes, leaf nodes, height, depth, etc. It also outlines examples of tree representations and applications of trees in areas like storing file systems, networks, encoding, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views235 pages

DS Unit-3

This document discusses trees as a non-linear data structure that is faster than linear data structures and provides a more natural fit for hierarchical data. It defines key tree terminology like nodes, edges, root, internal nodes, leaf nodes, height, depth, etc. It also outlines examples of tree representations and applications of trees in areas like storing file systems, networks, encoding, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 235

Study Buddy

A loyal study partner.


Tree

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

Tree consists of collection of nodes and Edges


T=(N,E)
Node is represented in circle with data/key
Edge is represented by a link between two nodes
Example Tree
root
Sami's Home Page

Teaching Research Activities

CS101 CS211 Papers (Presentations

A web page has been modeled as a Tree data structure


Why Tree Data Structure?

Other data structures such as arrays, linked list,


stack, and queue are linear data structures that
store data sequentially.
In order to perform any operation in a linear data
structure, the time complexity increases with the
increase in the data size.
But, it is not acceptable in today's computational
world.
Different tree data structures allow quicker and
easier access to the data as it is a non-linear data
structure.
Tree Applications

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

Node C is the parent of nodes G and H


Node E is the parent of nodes I and J
Node G is the parent of node K
O O
Tree terminology
Child-
The node which is a descendant of some
node is called as a child node. Child of A

All the nodes except root node are child


nodes.
Child of B

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

Nodes G and H are siblings


NodesI and J are siblings
Siblings
Tree terminology
Degree
Degree of a node is the total number of children of that
node.
Degree of a tree is the highest degree of a node among all
the nodes in the tree.

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

The node which has at least


one child is called as Internal Node
Internal Node

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

as external - Leaf Node

nodes or terminal nodes.


Here, nodes D, 1, J, F, K and
H are leaf nodes.
Tree terminology
Level
In a tree, each step from
top to bottom is called
as level of a tree.
The level count starts with
0 and increments by 1 at
each level or step. -
Level 0

--
Level11

-
Level 2

-
Level 3
Tree terminology
Path

Sequence of nodes along

-
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 all leaf nodes 0


Height of node A = 3
Height of node B = 2 Height(B) =2
Height of node C =2
Height of node D =0 Height(H)0
Height of node E = 11

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

Total number of edges from root node to a particular node


is called as depth of that node.
Depth of a tree is the total number of edges from root
node to a leaf node in the longest path.
(Top to bottom)

Depth of the root node = 0


Depth of node A = 00
Depth of node B = 1
Depth of node C= 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H 2
Depth(B)= 1
Depth of node
I
= 3

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 is a set of disjoint


trees.
6
(B)

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 Search Trees(BSTs) are used to quickly


Binary
check whether an element is present in a set or
not.
Heap is a kind of tree that is used for heap sort.
A modified version of a tree called Tries is used in
modern routers to store routing information.
Most popular databases use B-Trees and T-Trees,
which are variants of the tree structure we
learned above to store their data
Compilers use a syntax tree to validate the syntax
of every program you write.
Types of Tree

Binary Tree
Binary Search Tree
AVL Tree

B-Tree
.B+ Tree
Binary Trees

A binary treetree data structure in which


is a
each parent node can have at most two
children.

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

struct node *left;


struct node *right; Data Data

Left Right Left Right

NULL NULL NULL NULL


Binary Tree Representation

Array Representation of Binary Tree


(sequential representation)
Linked List Representation of Binary Tree
(Dynamic node representation)
Binary Tree Representation- Array

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.
Binary Tree Representation- Array

For eg: If the array size is a [100|


Root at a[O]

If a node occupies a[il


- Left child is in a(2i+1]
- Right child is in a[2i+2]
-
Parent is in al(i-1)/2]
In a binary tree, if level of root node is 0, and the level of
left and right child of root is 1, the maximum number of
nodes present in a level i of a binary tree is 2
Binary Tree Representation- Array

The maximum number of nodes present in a


level of a binary tree is 2
i

24

oe 3 s
Binary Tree Representation- Array

The maximum number of nodes present ina tree


of heighth of a binary tree is 2h*1-1
Eg: height h
= 20+2+22+.2h

Eg: height h=2 24


22+1-1
R
3 s(
23-1
1
Binary Tree Representation- Array
The mimimum number of nodes present in a tree of height h
of a binary tree is h+1
Eg: height h
= h+1
Eg: height h=0

Min.no of nodes=1
Eg: height h=1

Min.no of nodes=h+1=>1+1=2
Binary Tree Representation-Array

Array Representation of Binary Tree

w.bte ss.co

O O
Binary Tree Representation-Linked List

Linked List Representation of Binary Tree


We use a doubly linked list to representa
binary tree.
In a doubly 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.
Binary Tree Representation-Linked List

In this linked list representation, a node has


the following structure...

rress
Left Child
Address Data e
Right Child
LAddress
Binary Tree Representation

Example:

w.bte ss.co

In a binary tree with n number of nodes, there


are exactly (n-1) number of edges.
Binary Tree Representation- Linked List

rootNode

AN
B
DN F NULL

H
NULL NULL
Types of Binary Trees

Full Binary Tree


Complete Binary Tree
.Perfect Binary Tree
Types of Binary Trees

Full Binary Tree


A full Binary tree is a special
type of binary tree in which
every parent node/internal 2 3
node has either two or no
children. (ie.,no single child) 5
Every node other than leaf has 6
a
two children
Types of Binary Trees
Complete Binary Tree
A complete binary tree is just like a
full binary tree, but with two major 1
differences
Every level (except the last )must be 2 3
completely filled
elements must lean
All the leaf
towards the left.
4 6
The last leaf element might not
have a right sibling i.e. a complete
binary tree doesn't have to be a full
binary tree.
Types of Binary Trees

Perfect Binary Tree


A perfect binary tree is a type
of binary tree in which every
2 3
internal node has exactly two
child nodes and all the leaf 4
nodes are at the same level.
Full+complete binary tree
Binary Tree Applications

For easy and quick access to data


In router algorithms
To implement heap data structure
Syntax tree
Binary Tree Traversal
Traversing a tree means visiting every node in
the tree.
Eg: you want to add all the values in the tree or
find the largest one.
For all these operations, you will need to visit
each node of the tree.
Linear data structures ike
arrays, stacks, queues, and linked list have only
one way to read the data.
But a hierarchical data structure like a tree can
be traversed in different ways.
Binary Tree Traversal Types

Depending on the order in which we do this,


there can be three types of traversal.
Inorder
Preordeer
.Postorder
Binary Tree Traversal Types

Inorder Traverse the lett sub-tree first, annd


then traverse the root and the right
Sub-tree respectively.
Preorder Traverse the root first then
traverse into the left sub-tree and right
sub-tree respectively.
Postorder Traverse the left sub-tree and
then traverse the right sub-tree and root
respectively.
Binary Tree Traversal Types

Inorder -
left-root- right
.Preorder -
root-left -right
.Postorder -
left-right-root
Binary Tree Traversal

Example: Inorder (always displays in increasing


order in case of BSTa

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

1-J-D- F -B -K-G H-C-A


Binary Tree Traversal (Inorder
trick
In' means 'down of the node. So, put the linedown of each
node. And, traverse it from root to end.

In-order tree traversal:


Root node

In-OrderTraversal Sequence: B ADCE E


Binary Tree Traversal-Preorder
trick
Pre' means 'before i.e. left' of the node. So, put the line
on the left side of each node. And, traverse it from root to
end.

Pre-order tree traversal:


Root node

B C

Pre-Order Traversal Sequence:A B CDE E


Binary Tree Traversal-post order trick
Post' means 'after i.e. right' of the node. So, put the line
on the right side of each node. And, traverse it from root
to end.

Post-order tree traversal:


Root node

B C

Post-Order TraversalSequence: BDECA D E


Exercise- Binary Tree Traversal

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==0) struct node *root;


root-0;
return; root=create();
printf("\n the preorder traversal is");
printf("%d ",root->data); preorder(root);
preorder(root->left);
preorder(root->right);
Binary Tree display (inorder)
void display(struct Node *root)

if(root != NULL)

display(root->left);
printf("%d", root->data);
display(root->right);
Binary Tree display (preorder)

void display(struct Node *root)


{

if(root != NULL)

printf("%d", root->data);
display(root->left);
display(root->right);
}
Binary Tree display (postorder)

void display(struct Node *root)

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)

Ingeneral, time complexity of all the operations is O(h)


where h is height of BST.
Binary Tree -Time Complexity

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

insert (4) insert (20) insert (8)


10 10 10

4 8
Binary Search Tree-Construction

insert (7) insert (15) insert (13)

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

Assign node to NULL


25 75 25 75
and free the node
delete node 85

12 30 60 85 12 30 60 85 X
deleted node
52 70 52 70
Binary Search Tree-Delete- case-2
50 50

Replace 12 with 6 and


25 75 25 75
delete 12
delete node 12

60 6 30 60
12 30

52 70 52 70
6 12 X
Deleted node
Binary Search Tree-Delete- case-3

50 52

Replace 50 with its


25 75 25 75
in-order successor
delete node 50

60 30 60
6 30

52 70 50 70

Deleted Node

Inorder successor is always either a leaf node or a node with


empty left child
Binary Search Tree-Delete- case-3
struct node* deleteNode(struct node* root, int key)
// same as root's key, then This
if key is is the node
I/ to be deleted
base case
else
if (root == NULL) return root;

node with only one child or no child


// If the key to be deleted is smaller than the root's
if (root->left == NULL)
key,
// then it lies in left subtree
if (key < root->key) struct node *temp = root->right;
root->left = deleteNode(root->left, key); free(root);
return temp;
/If the key to be deleted is greater than the root's
key, else if (root->right == NULL)
{
/then it lies in right subtree
=
else if (key> root->key) struct node *temp root->left;
root->right = deleteNode(root->right, key); free(root);
return temp;
Binary Search Tree-Delete- case-3
else if (root->right == NULL)

struct node *temp = root->left;


freefroot);
return temp;

//node with two children: Get the inorder successor (smallest


/in the right subtree)
struct node* temp = minValueNode(root->right);

//Copy the inorder successor's content to this node


root->key = temp->key;

//Delete the inorder successor


=
root->right deleteNode(root->right, temp->key);

return root;
Binary Search Tree-Delete- case-3
//function to delete a node
struct node* delete(struct node *root, int x)

//searching for the item to be deleted


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

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)
{

struct node *temp;


if(root->left_child==NULL)
temp = root->right_child;
else
temp root->left_child;
free(root);
return temp;
Binary Search Tree-Delete- case-3
//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;
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

Operation Time Complexity


Search O(log n)

Insert O(log n)

Delete O(log n)

Traverse O(n)
Create O(n)
Binary Search Tree-Exercise

Create the binary search tree using the following data


elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
AVL Treee

Dr.R.Kanniga Devi
Associate Professor
CSE/KARE

Edit with WPS Office


AVL Trees
Named after Adelson-Velskii and Landis

Edit with WPS Office


O-- AVL Tree
AVL trees are special kind of binary search trees.
In AVL trees, height of left subtree and right
subtree of every node differs by at most one.
AVL trees are also called as self-balancing binary
search trees.
The advantage of balancing a Binary Search
Tree is, it decreases the search time complexity
Balance is definedby comparing the height of
the twe sub-trees
2
C
w WpsOHCe
WEdit
AVL Tree Example
F 2-2-

8 18 sT

AVL Tree Example

This tree is an AVL tree because-


It is a binary search tree.
The difference between height of left subtree and right subtree of
every node is at most one.

Edit with WPS Office


Not an example of AVL Tree
2

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

Edit with WPS Office


Balance Factor
Balance Factor = (Height of Left Subtree
Height of Right Subtree) or (Height of
-

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,

Edit with WPS Office


Balance Factor
3-2 1
100

1-2-1 1-1 0

50 150

1-1 0
0
25 75 125 175

65 85

AVL Tree

Edit with WPS Office


Balance Factor

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.

Edit with WPS Office


AVL Tree Operations

Search Operation
Insertion Operation
Deletion Operation

Edit with WPS Office


AVL Tree Operations
After performing any operation on AVL tree the balance factor of each
node is checked.
There are following two cases possible-

Case-01:
After the operation, the boalance factor of each node is either 0 or 1 or
-1

this case, the AVL tree


In is considered to be balanced.
-The operation is over.

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.

Edit with WPS Office


AVL Tree insertion
To insert an element in the AVL tree,
follow the following steps
-
Insert the element in the AvL tree in the
same way the insertion is performed in BST.
-
After insertion, check the balance factor of
each node of the resulting tree.

Edit with WPS Office


AVL Tree insertion
Now, following two cases are possible-

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.

Edit with WPS Office


AVL Tree insertion- Practice
Problem
Construct AVL Tree for the folowing
sequence oft numberS
50,20,60,10, 8,15,32,46,11,48
(so

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

Tree is Balanced Tree is Imbalanced


Edit with WPS Office
AVL Tree insertion- Practice Problem-
Solution

(60)
* RR Rotation
(60

Tree is Imbalanced Tree is Balanced

Edit with WPS Office


AVL Tree insertion- Practice Problem-
Solution
60)

Tree is Imbalance

O
-.
60
LR Rotation
- 5

Tree is Imbalanced Tree is Balanced

Edit with WPS Office


AVL Tree insertion- Practice Problem-
Solution
20
50)

Tree is Balanced

Tree is Balanced

(46)

Wreeis Balenceds Office


AVL Tree insertion- Practice Problem-
Solution

(11
-------- (6

-O
Tree is imbalanced
(60)
Tree is Imbalanced

LL Rotation

Edit with WPS Office Tree is Balanced


Edit with WPS Office
Edit with WPS Office
Edit with WPS Office
Edit with WPS Office
AVL Tree Property
Maximum poSsible number of nodes in
AVL tree of height H

= 2H*1-1

Edit with WPS Office


AVL Tree Property
Example
Maximum possible number of nodes in
AVL tree of height-3
= 23+1-1
= 16-1
= 15
Thus, in AVL tree of height-3, maximum
number of nodes that can be inserted = 15.

Edit with WPS Office


AVL Tree Property

Height 3

D)

Edit with WPS Office


AVL Tree Rotations

Rotation is the process of moving the


nodes to make tree balanced.
Of--2 (12
12
RL

14 L (3
(3
2

Edit with WPS Office


Kinds of Rotations

There are 2 major classification of


rotation
-I I-.-
A A
AVL L.L
Tree Rotations VL

Single Rotations Double Rotations

Left Rotation Right Rotation Lft-Right Rotaion Right-Left Rotation


(LL Rotation) (RR Rotation) (LR Rotatiory (RL Rotation)

Edit with WPS Office


Kinds of Rotations

Left Rotation (LL Rotation)

Right Rotation (RR Rotation)


Left-Right Rotation (LR Rotation)
Right-Left Rotation (RL Rotation)

Edit with WPS Office


Cases of Imbalance and their Balancing using
Rotation Operations
Case-01:
This node becomes imbalanced
after inserting node-3

LL Rotation
(Rotate Anticlockwise)

3
Tree is Balanced

Insertion Order:1, 2,3


Tree is Imbalanced

Edit with WPS Office


Cases of Imbalance and their Balancing using
Rotation Operations
Case-02:
This node becomes imbalanced
after inserting node-1

RR Rotation

(Rotate clockwise)

Tree is Balanced

Insertion Order : 3, 2,1


Tree is Imbalanced

Edit with WPS Office


Cases of Imbalance and their Balancing using
Rotation Operations
Case-03:
This node becomes imbalanced
after inserting node-2

3
LR Rotation

(LL +RR Rotation)

Tree is Balanced
Insertion Order: 3, 1,2
Tree is Imbalanced

Edit with WPS Office


Cases of Imbalance and their Balancing using
Rotation Operations
Case-04: C

This node becomes imbalanced


after inserting node-2
O 2
3
RL Rotation

RR+LL Rotation

Tree is Balanced

Insertion Order : 1,3,2


Tree is Imbalanced

Edit with WPS Office


State Action

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.

The tree is now balanced.


A

Edit with WPS Office


State Action
2
RL
A node has been inserted into the left subtree of the right subtree.
This makes A, an unbalanced node with balance factor 2.

- C
D
First, we perform
subtree
of A.

subtree

subtree.
and requires

A left rotation
the

Node A is still unbalanced


right rotation alongC
of its own left subtree

because
a left rotation.

is performed
A becomes
by making
the left subtree
node,
B. Now, B becomes

of the right
making

subtree

B the new root node


of its right subtree B.
C the right
the right subtree

of its right

of the

The tree is now balanced.


A

Edit with WPS Office


AVL Tree Visualization
https://www.cs.usfca.edu/~galles
visualization/AVLtree.html

Edit with WPS Office


AVL Tree -Deletion

Case 1: Deleting from the right subtree.


1A. If BFnode) = +2 and BF(node -> left-child) +1, perform LL
rotation.
1B. If BF(node) = +2 and BF(node -> left-child) = -1, perform LR rotation.
1C. If BF(node) = +2 and BF(node -> left-child) = 0, perform LL rotation.

Edit with WPS Office


Deletion: Case 1
(deleting from right sub tree)
BF=+1
30 30 BF=+2
20 BF=0
Case: 1A BF=0 20 40 BF= 0
20
BF
BF+1 010 BF 0
BF=0 10
BF=0 10

Deleting 40 Tree becomes imbalance Tree balanced.


performing LL rotation.

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

Deleting 40 Tree becomes imbalance Tree balanced.


performing LR rotation.

BFm+1 30 BF=+2 BF=-1


30 10

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.

Edit with WPS Office


AVL Tree -Deletion

Case 2: Deleting from left subtree.


2A. If BF(node) = -2 and BF(node-> right-child) = -1, perform RR
rotation.
2B. If BF(node) = -2 and BF(node -> right-child) = +1, perform RL
rotation.
2C. If BF(node) = -2 and BF(node -> right-child) = 0, perform RR
rotation.

Edit with WPS Office


Deletion: Case 2
(deleting from left sub tree)
BF=-1
20
20 BF-2
30 BF=0
BF= 0 10 30 BF=-1
BF -1
Case: 2A B0

BF 0 20 40 BF0
40 BF-00 400 BF-00

Deleting 10 Tree becomes imbalance, Tree balanced.


performing RR rotation.

20 BF-1 20 BF=-2 30 BF=0

Case: 2B BF-0 10 40 BF-+1 40 BF-+1 BF-0 20 3F 0

310
BF 0
SBF 0
Deleting 10 Tree becomes imbalance, Tree balanced.
performing RL rotation.

20 BF--1 20 BF- 30 BF=+1

Case: 2C BF-O10 30 BF-0 BF=0 BF -1 20 40 BF-0

BF-0 40 BF-0 BF-0 25 BF-0 25


BF-0

Deleting10 Tree becomes imbalance, Tree balanced.


W Edit with WPS OffeEorming RR rotation.
AVL Tree -Deletion (Another Eg)

.
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

AVL Tree Non AVL Tree RO Rotated Tree

Edit with WPS Office


AVL Tree -Deletion (Another Eg)
2
Node A Critical Node Node A Node B
50 50 40

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

AVL Tree Non AVL Tree RI Rotated Tree

Edit with WPS Office


AVL Tree -Deletion (Another Eg)
2
Node A Critical Node Node A Node C
50 50 45

Performing R-1
-1
Deleting Node 60 rotation
Node B
Node B
40 60 40 40 50

Node to be deleted Node B Node A

45 45
Node C Node C

AVL Tree Non AVL Tree R-1 Rotated Tree

Edit with WPS Office


AVL Tree Representation
Struct AVLNode

int data;
struct AVLNode *left, *right;
int balfactor;

Edit with WPS Office


AVL Tree -Time Complexity

Operation Time Complexity


(worst case)
Search O(log n)
Insert O(log n)
Delete O(log n)
Traversal O(n)

Edit with WPS Office


AVL Tree Example
Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL treee

14
11 17

53

Edit with WPS Office


AVL Tree Example
Insert 14, 17, 11, 7, 53, 4, 13 into an empty AVL tree

14
17

4 11
53

13

Edit with WPS Office


AVL Tree Example:
Now insert 12

14
17

4 11
53

13
12

Edit with WPS Office


AVL Tree Example
Now insert 12

14
17

4 11
53

12

13

Edit with WPS Office


AVL Tree Example
.Now the AVL tree is balanced.

14
17

4 12 53

11 13

Edit with WPS Office


AVL Tree Example:
.Now insert 8

14
17
4 12 53
11
13
8

Edit with WPS Office


AVL Tree Example
.Now insert 8

14

17
4 11
53

8 12
13

Edit with WPS Office


AVL Tree Example
.Now the AVL tree is balanced.

14
11 17

12 53

4 8 13

Edit with WPS Office


AVL Tree Example
.Now remove 53

14
11 17

12 53

4 8 13

Edit with WPS Office


AVL Tree Example
.Now remove 53, unbalanced

14
11 17

12
4 8 13

Edit with WPS Office


AVL Tree Example
Balanced! Remove 11

11

14

4 8 12 17

13

Edit with WPS Office


AVL Tree Example
Remove 11, replace it with the largest in its left branch

14

12 17

13

Edit with WPS Office


AVL Tree Example
Remove 8, unbalanced

4 14

12 17

13

Edit with WPS Office


AVL Tree Example
Remove 8, unbalanced

4 12

14

13 17

Edit with WPS Office


AVL Tree Example
Balanced!!

12

14

4
13 17

Edit with WPS Office


Exercise
Build an AVL tree with the following values:
15, 20, 24, 10, 13, 7, 30, 36, 25

Edit with WPS Office


15, 20, 24, 10, 13, 7, 30, 36, 25
20
15
15 24
20
10
24
13

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.

Edit with WPS Office


B-Tree
In search trees, like binary search tree and
AVL Tree, every node contains only one
value (key) and a maximum of two children.
In B-Tree a node contains more than one
value (key) and more than two children
which keeps the height of the tree relatively
small.
It allowS operations like Insertion, searching,
and deletion in less time.
widely used for disk access
Edit with WPS Office
Edit with WPS Office
B-Tree
The number of keys in a node and
number of children for a node depends
on the order of B-Tree (m).
The value of "m" depends upon the block
size on the disk on which data is primarilyy
located

Edit with WPS Office


B-Tree
All leaves will be at the same level.
The left subtree of the node will have lesser
values than the right side of the subtree.
Generalization of BST)
Nodes maintain sorted data (in ascending
order from left to right)
Data/key can be present in all the nodes

Edit with WPS Office


Rules for B-Tree
For a B-Tree of order m Minimum:
The minimum children a node can
Maximum: have is half of the order, which is
-
The maximum m/2 (the ceiling value is taken).
number of children Minimum children
for any node: mh
leaf -> 0
:

The maximum :root-> 2


number of keys m-1 internal node->m/2
:

(the ceiling value)


-
For example: Every node, except root, must contain
m =4 minimum
max children = 4 keys of [m/21-1
max keys 4 1 Root contains minimum key of 1
3
For example:
m 3=
min keys (3/2)-1 1.5-1 2-1=1
Edit with WPS Office
Edit with WPS Office
Example- B-Treee
For eXample, B-Tree of Order 4 contains a
maximum of 3 key values in a node and
maximum of 4 children fora node.
B-Tree of Order 4

8070

25 40 50

DB7 5 21 2 26 2 53 246 67 5 785O997

Edit with WPS Office


Edit with WPS Office
Why use B-Tree
Data is stored on the disk in blocks, this data, when brought
into main memory (or RAM) is called data structure.
In-case of huge data, searching a record in the disk requires
reading the entire disk;
This increases time and main memory consumption due to
high disk access frequency and data size.
To overcome this, index tables are created that saves the
record reference of the records based on the blocks they
reside in. This drastically reduces the time and memory
Consumption.
Since we have huge data, we can create multi-level index
tables.
Multi-level index can be designed by using B Tree for
keeping the data sorted in a self-balancing fashion.

Edit with WPS Office


Why use B-Tree

Reduces the number of reads made on


the disk
It is a useful algorithm for databases oand
file systems.

Edit with WPS Office


B-Tree -Insertion
In a B-Tree, a new element must be added only at the leaf node. That
means, the new keyValue is always attached to the leaf node only.

Step 1-Check whether tree is Empty.


Step 2-Iftree 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 ddded using Binary Search Iree logic.
Step 4-Ifthat leaf node hgs empty position, gdd 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 spilting is performed at root node then the middle value
-

becomes newone
root node for the tree and the height of the tree is
increased by

Edit with WPS Office


B-Tree Construction
Construct a B-Tree of rder 3 (m=3) by
inserting numbers from 1 to 10.

Edit with WPS Office


B-Tree Construction
insert(1)
Since'1'is the first element into the tree that is inserted into a new node. It acts as the root node.

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

Edit with WPS Office


B-Tree Construction
insert(4)
Element 4'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 with
value 3and it has an empty position. So, new element (4) can be inserted at that empty position.

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

Edit with WPS Office


B-Tree Construction
insert(6)
Element '6'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
with value'5' and it has an empty position. So, new element (6) can be inserted at that empty position.

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

Edit with WPS Office


Edit with WPS Office
Cors m (3-13 ca crde m 2
23, s 6, 7, 1,
an 3

2
1 2
3
T UdIL
1123
S
2,
2
ma
51
dlla elarma

Edit with WPS Office


2

2
3

21/D

Edit with WPS Office


7
D 3 B-(a
Pn
P*

shl Vist9,
B- P

2
Edit with WPS Office
|

all

(l2 RG
TV
P 7

Edit with WPS Office


L

T
3

Edit with WPS Office


Edit with WPS Office
Edit with WPS Office
Edit with WPS Office
B-Tree-Search Operation
The search operation in B-Tree is similar to
the search operation in Binary Search Tree.
In a Binary search tree, the search process
starts from the root node and we make a
2-Way decision every time (we go tO either
left subtree or right subtree).
In B-Tree also search process startS trom
the root node but here we make an m-way
decision every time, where m is the total
number of children the node has.
Edit with WPS Office
B-Tree-Search Operation
Step 1- Read the search element from the user.
Step 2-Compare the search element with first key value of root
node in the tree.
Step 3 If both are matched, then display "Given node is found!"
-

and terminate the function


Step 4- If both are not matched, then check whether search
element is smaller or larger than that key value.
Step 5 If search element is smaller, then continue the search
-

process in left subtree.


Step 6 If search element is larger, then compare the search
-

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.

Edit with WPS Office


B-Tree-Search Operation- Example

78

40 56 89 101

10 23 45 49 60 69 85 90 98 103 110

Edit with WPS Office


B-Tree-Search Operation- Example
For example, if we search for an item 49 in
the B Tree.
The process will be:
-
Compare item 49 with root node 78. since 49
<78 hence, moveto its left sub-tree.
Since, 40<49<56, traverse right Sub-tree of 40.
-
49>45, move to right. Compare 49.
match found, return.
Searching in aB tree depends upon the
height of the tree.
Edit with WPS Office
B-Tree Visualization
https://www.cs.usfca.edu/~galles/
visualization/BTree.html

Edit with WPS Office


B-Tree Time Complexity

Operation Time Complexity


Search Ollog n)
Insert O(log n)

Delete o(log n)

Edit with WPS Office


B+ Tree
A B+ Tree is a balanced tree
Same height for paths from root to leaf
Given a search key K, nearly same access timee
for different K values
Key values are maintained in sorted order
Same rules as that of B-Tree
Compared to B- Tree, the B+ Tree stores the
data only at the leaf nodes of the Tree, which
makes search process more acCurate and faster

Edit with WPS Office


B+ Tree
A B+ Tree is primarily used for implementing
multi-level indexing.
Compared to B- Tree, the B+ Tree stores the
data only at the leaf nodes of the Tree, which
makes search process more aCCurate and
faster.

Edit with WPS Office


B+ Tree
AB+ tree is an advanced form of self- a
balancing tree in which all the values(keys/
data) are present in the leaf level
An important concept to be understood
before learning B+ tree is multilevel
indexing.
In multilevel indexing, the index of indices
is created as in figure. It makes accessing
the data easier and faster.

Edit with WPS Office


B+ Tree

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

root node internal nodes leaf nodes


Edit with WPS Office
Rules of B+ Tree
Leaves are used to store data records.
The left subtree of the node will have
lesser values than the right side of the
subtree. (Generalization of BST)

Edit with WPS Office


Comparison between a B-tree and a B+
Tree

The data are present only at the leaf nodes


on a B+ tree wnereas the data are present
in the internal, leaf or root nodes on a B-
tree
The leaves are not connected with each
other on a B-tree whereas they are
Connected on a B+ tree.
Operations on a B+ tree are faster than on
a B-tree.

Edit with WPS Office


B+ Tree Insertion

Step 1: Insert the new node as a leaf node


Step 2: If the leaf doesn't have required
space, split the node and copy the middle
node to the next index node.
Step 3: If the index node doesn't have
required space, split the node and copy the
middle element to the next index page.

Edit with WPS Office


B+ Tree Insertion

Example
Insert the value 195 into the B+ tree of
order 5 shown in the following figure.

4 3 57 83 110 119 129 190 200

Edit with WPS Office


B+ Tree Insertion

195 will be inserted in the right sub-tree of


120 after 190. Insert it at the desired
Dosition.
60 78 108 120

48 83 10 119 124 190 195 200

Edit with WPS Office


B+ Tree Insertion

The node contains greater than the


maximum number of elements i.e. 4,
therefore split it and place the median
node un to the narent
60
78108 120 190

48 83 88 10 119 195 20

Edit with WPS Office


B+ Tree Insertion

Now, the index node Contains 6 children


and 5 keys which violates the B+ tree
properties, therefore we need to split it,
shown as folloWs.
108

60 78 120 190

48 53 57 69 14 83 88 110 119 129 154 195 200

w Edit With WPSUTCE


CTec +-
2, 3f,5 ,9, S, 7, C

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

Edit with WPS Office


s/ 1/

s/4
3

Edit with WPS Office


Sh vito u
Pr
2

Edit with WPS Office


Edit with WPS Office
Edit with WPS Office
Edit with WPS Office
B+ Tree Searchinng

Searching Example ona B+ Tree


Let us search k = 45 on the following B+
tree.
25
15 35 45
515 2025 30 35 4045 55
Edit with WPS Office
B+ Tree Searching

Compare k with the root node, k is not


found at the root
25
15 35 45

515 2025 30-35 4045 55

Edit with WPS Office


B+ Tree Searching

Since k> 25, go to the right child.


Go to right of the root

25
15 35 45

515 2025 30 3540 45 55


Edit with WPS Office
B+ Tree Searching

Compare k with 35. Since k > 30, compare k


with 45.
k not found

25
15 35 45
5 15 2025 30 35 4045 55
Edit with WPS Office
B+ Tree Searching

Since k 2 45, so go to the right child.


go to the right

25
15 35 45

515 2025 30-35 45 55


Edit with WPS Office
B+ Tree Searching

k is found

25
15 35 45

515 20-25 30-35 40-45 55

Edit with WPS Office


B+ Tree Visualization

https://www.cs.ustca.edu/~galles/visualization/
BPlusTree.html

Edit with WPS Office


B-Tree Vs.B+ Tree
B
B+Tree Tree
Search keys can be Search keys cannot be
repeated. redundant.
Data is only saved on the Both leaf nodes and
leaf nodes. internal nodes can store
data
Data stored on the leaf Searching is slow due to
node makes the search data stored on Leaf and
more accurate and faster. internal nodes.
Linked leaf nodes make the You cannot link leaf nodes.
search efficient and quick.

Edit with WPS Office


B+Tree -Time Complexity

Operation Time Complexity


Search Ollog n)
Insert O(log n)

Delete o(log n)

Edit with WPS Office

You might also like