KEMBAR78
Binary and B+tree in data structure.pptx
DATA STRUCTURES – Binary
and B+ Tree
Trees - Introduction
• A tree is a nonlinear hierarchical data structure that
consists of nodes connected by edges.
• Different tree data structures allow quicker and
easier access to the data as it is a non-linear data
structure.
Properties of Trees
• There is one and only one path between every
pair of vertices in a tree.
• A tree with n vertices has n-1 edges.
• A graph is a tree if and if only if it is minimally
connected.
• Any connected graph with n vertices and n-1
edges is a tree.
Tree terminologies
• Node - A node is an entity that contains a key or value and pointers to its
child nodes.
– The last nodes of each path are called leaf nodes or external
nodes that do not contain a link/pointer to child nodes.
– The node having at least a child node is called an internal node.
• Edge -It is the link between any two nodes.
• Root - It is the topmost node of a tree.
Tree terminologies
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.
• We can never have multiple root nodes in a tree data structure.
Edge
• The connecting link between any two nodes is called as
an edge.
• In a tree with n number of nodes, there are exactly (n-1)
number of edges.
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.
Child
• The node which is a descendant of some node is called as
a child node.
• All the nodes except root node are child nodes.
Siblings
• Nodes which belong to the same parent are called as siblings.
• In other words, nodes with the same parent are sibling nodes.
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.
Internal node
• The node which has at least one child is called as an internal
node.
• Internal nodes are also called as non-terminal nodes.
• Every non-leaf node is an internal node.
Leaf node
• The node which does not have any child is called as a leaf
node.
• Leaf nodes are also called as external nodes or terminal
nodes.
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.
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.
• Height of all leaf nodes = 0
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.
• Depth of the root node = 0
• The terms “level” and “depth” are used interchangeably.
Subtree
• In a tree, each child from a node forms a subtree recursively.
• Every child node forms a subtree on its parent node.
Forest
• A forest is a set of disjoint trees.
Binary Tree
• Binary tree is a special tree data structure in which
each node can have at most 2 children.
• Thus, in a binary tree, Each node has either 0 child or
1 child or 2 children.
Unlabeled Binary Tree
• A binary tree is unlabeled if its nodes are not
assigned any label.
Example
• Consider we want to draw all the binary trees possible
• Number of binary trees possible with 3 unlabeled nodes
• = 2 x 3C3 / (3 + 1)
• = 6C3 / 4
• = 5
Labeled Binary Tree
• A binary tree is labelled if all its nodes are
assigned a label.
Example
• Consider we want to draw all the binary trees possible with 3 labeled
nodes.
• Number of binary trees possible with 3 labeled nodes
• = { 2 x 3C3 / (3 + 1) } x 3!
• = { 6C3 / 4 } x 6
• = 5 x 6
• = 30
Types of Binary Trees
Rooted Binary Tree
• A rooted binary tree is a binary tree that
satisfies the following 2 properties:
– It has a root node.
– Each node has at most 2 children.
Full/Strictly Binary Tree
• A binary tree in which every node has either 0 or 2
children is called as a Full binary tree.
• Full binary tree is also called as Strictly binary
tree.
Complete /Perfect Binary Tree
• A complete binary tree is a binary tree that satisfies
the following 2 properties:
– Every internal node has exactly 2 children.
– All the leaf nodes are at the same level.
Almost Complete Binary Tree
• An almost complete binary tree is a binary tree that satisfies
the following 2 properties-
– All the levels are completely filled except possibly the last level.
– The last level must be strictly filled from left to right.
Skewed Binary Tree
• A skewed binary tree is a binary tree that satisfies the following 2 properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child.
OR
• A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Tree Traversal
• In order to perform any operation on a tree,
you need to reach to the specific node. The
tree traversal algorithm helps in visiting a
required node in the tree.
• Tree Traversal refers to the process of visiting
each node in a tree data structure exactly
once.
Tree traversal techniques
Tree Traversal
Depth First Traversal Breadth First Traversal
Preorder Traversal
Inorder Traversal
Postorder Traversal
Depth First Traversal
• Following three traversal techniques fall under
Depth First Traversal-
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
Preorder Traversal
• Algorithm-
– Visit the root
– Traverse the left sub tree i.e. call Preorder (left sub tree)
– Traverse the right sub tree i.e. call Preorder (right sub tree)
Inorder Traversal
• Algorithm-
– Traverse the left sub tree i.e. call Inorder (left sub tree)
– Visit the root
– Traverse the right sub tree i.e. call Inorder (right sub tree)
Postorder Traversal
• Algorithm-
– Traverse the left sub tree i.e. call Postorder (left sub tree)
– Traverse the right sub tree i.e. call Postorder (right sub
tree)
– Visit the root
Breadth First Search
• Breadth First Traversal of a tree prints all the
nodes of a tree level by level.
• Breadth First Traversal is also called as Level
Order Traversal.
From a single traversal it is not possible to construct
unique binary tree. However any of the two traversals
are given then the corresponding tree can be drawn
uniquely:
• Inorder and preorder
• Inorder and postorder
• Inorder and level order
Building Binary Tree
Building Binary Tree
Example 1: Construct a binary tree from a given
preorder and inorder sequence:
Preorder: A B D G C E H I F
Inorder: D G B A H E I C F
Solution:
Building Binary Tree
Implementation of Binary Tree
struct tree
{
struct tree* lchild;
char data[10];
struct tree* rchild;
};
Implementation of Binary Tree
node* getnode()
{
node *temp ;
temp = (node*) malloc(sizeof(node));
printf("n Enter Data: ");
scanf("%s",temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
B+ Tree
B+ Tree
A B+ tree is a self-balancing tree data structure
commonly used in databases and file systems for
efficient storage and retrieval of large amounts of
data. It is an extension of the B-tree, optimized for
disk-based storage systems.
Components of B+ Tree
• Leaf nodes store all the key values and pointers to
the actual data.
• Internal nodes store only the keys that guide
searches.
• All leaf nodes are linked together, supporting
efficient sequential and range queries.
Structure of B+ Tree
B+ Tree Features
Balanced: Auto-adjusts when data is added or
removed, keeping search time efficient.
Multi-level: Has a root, internal nodes, and leaf nodes
(which store the data).
Ordered: Maintains sorted keys, making range queries
easy.
High Fan-out: Each node has many children, keeping
the tree short and fast.
Cache-friendly: Works well with memory caches for
better speed.
Disk-efficient: Ideal for disk storage due to fast data
access.
Searching record in B+ Tree
• Start at the root node
• Navigate through internal nodes based on key
comparisons
• Reach the appropriate leaf node
• If the key exists, return the data; otherwise,
report "record not found"
Searching record in B+ Tree
Example: In the image below, in order to search for
58, traverse the root → internal node → reach the
correct leaf node that contains 58 (if it exists).
Insertion in B+ Tree
Insertion in B+ Trees is done in three steps:
1. Navigate to the correct leaf node
2. Insert the new key in sorted order
3. If overflow occurs:
• Split the leaf node
• Push the middle key to the parent
• This process may propagate up to the root
Deletion in B+ Tree
1. Finding and removing the key from the
appropriate leaf node
2. Rebalancing the tree to maintain B+ Tree
properties:
• Borrowing keys from siblings or
• Merging nodes if necessary
Advantages of B+ Tree
• Data stored in a B+ tree can be accessed both
sequentially and directly.
• It takes an equal number of disk accesses to fetch
records.
• B+ trees have redundant search keys, and storing
search keys repeatedly is not possible.
• A B+ tree with 'l' levels can store more entries in
its internal nodes compared to a B-tree having the
same 'l' levels.
Disadvantages of B+ Tree
Slower exact match: Data is always in leaf nodes,
increasing search path.
More disk access: Requires reaching the leaf even
for a single record.
Complex updates: Insertion and deletion are more
complex.
Extra space: Leaf node links need additional
pointers.
Thank you

Binary and B+tree in data structure.pptx

  • 1.
    DATA STRUCTURES –Binary and B+ Tree
  • 2.
    Trees - Introduction •A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges. • Different tree data structures allow quicker and easier access to the data as it is a non-linear data structure.
  • 3.
    Properties of Trees •There is one and only one path between every pair of vertices in a tree. • A tree with n vertices has n-1 edges. • A graph is a tree if and if only if it is minimally connected. • Any connected graph with n vertices and n-1 edges is a tree.
  • 4.
    Tree terminologies • Node- A node is an entity that contains a key or value and pointers to its child nodes. – The last nodes of each path are called leaf nodes or external nodes that do not contain a link/pointer to child nodes. – The node having at least a child node is called an internal node. • Edge -It is the link between any two nodes. • Root - It is the topmost node of a tree.
  • 5.
  • 6.
    Root • The firstnode from where the tree originates is called as a root node. • In any tree, there must be only one root node. • We can never have multiple root nodes in a tree data structure.
  • 7.
    Edge • The connectinglink between any two nodes is called as an edge. • In a tree with n number of nodes, there are exactly (n-1) number of edges.
  • 8.
    Parent • The nodewhich 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.
  • 9.
    Child • The nodewhich is a descendant of some node is called as a child node. • All the nodes except root node are child nodes.
  • 10.
    Siblings • Nodes whichbelong to the same parent are called as siblings. • In other words, nodes with the same parent are sibling nodes.
  • 11.
    Degree • Degree ofa 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.
  • 12.
    Internal node • Thenode which has at least one child is called as an internal node. • Internal nodes are also called as non-terminal nodes. • Every non-leaf node is an internal node.
  • 13.
    Leaf node • Thenode which does not have any child is called as a leaf node. • Leaf nodes are also called as external nodes or terminal nodes.
  • 14.
    Level • In atree, 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.
  • 15.
    Height • Total numberof 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. • Height of all leaf nodes = 0
  • 16.
    Depth • Total numberof 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. • Depth of the root node = 0 • The terms “level” and “depth” are used interchangeably.
  • 17.
    Subtree • In atree, each child from a node forms a subtree recursively. • Every child node forms a subtree on its parent node.
  • 18.
    Forest • A forestis a set of disjoint trees.
  • 19.
    Binary Tree • Binarytree is a special tree data structure in which each node can have at most 2 children. • Thus, in a binary tree, Each node has either 0 child or 1 child or 2 children.
  • 20.
    Unlabeled Binary Tree •A binary tree is unlabeled if its nodes are not assigned any label.
  • 21.
    Example • Consider wewant to draw all the binary trees possible • Number of binary trees possible with 3 unlabeled nodes • = 2 x 3C3 / (3 + 1) • = 6C3 / 4 • = 5
  • 22.
    Labeled Binary Tree •A binary tree is labelled if all its nodes are assigned a label.
  • 23.
    Example • Consider wewant to draw all the binary trees possible with 3 labeled nodes. • Number of binary trees possible with 3 labeled nodes • = { 2 x 3C3 / (3 + 1) } x 3! • = { 6C3 / 4 } x 6 • = 5 x 6 • = 30
  • 24.
  • 25.
    Rooted Binary Tree •A rooted binary tree is a binary tree that satisfies the following 2 properties: – It has a root node. – Each node has at most 2 children.
  • 26.
    Full/Strictly Binary Tree •A binary tree in which every node has either 0 or 2 children is called as a Full binary tree. • Full binary tree is also called as Strictly binary tree.
  • 27.
    Complete /Perfect BinaryTree • A complete binary tree is a binary tree that satisfies the following 2 properties: – Every internal node has exactly 2 children. – All the leaf nodes are at the same level.
  • 28.
    Almost Complete BinaryTree • An almost complete binary tree is a binary tree that satisfies the following 2 properties- – All the levels are completely filled except possibly the last level. – The last level must be strictly filled from left to right.
  • 29.
    Skewed Binary Tree •A skewed binary tree is a binary tree that satisfies the following 2 properties- • All the nodes except one node has one and only one child. • The remaining node has no child. OR • A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
  • 30.
    Tree Traversal • Inorder to perform any operation on a tree, you need to reach to the specific node. The tree traversal algorithm helps in visiting a required node in the tree. • Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.
  • 31.
    Tree traversal techniques TreeTraversal Depth First Traversal Breadth First Traversal Preorder Traversal Inorder Traversal Postorder Traversal
  • 32.
    Depth First Traversal •Following three traversal techniques fall under Depth First Traversal- 1. Preorder Traversal 2. Inorder Traversal 3. Postorder Traversal
  • 33.
    Preorder Traversal • Algorithm- –Visit the root – Traverse the left sub tree i.e. call Preorder (left sub tree) – Traverse the right sub tree i.e. call Preorder (right sub tree)
  • 35.
    Inorder Traversal • Algorithm- –Traverse the left sub tree i.e. call Inorder (left sub tree) – Visit the root – Traverse the right sub tree i.e. call Inorder (right sub tree)
  • 37.
    Postorder Traversal • Algorithm- –Traverse the left sub tree i.e. call Postorder (left sub tree) – Traverse the right sub tree i.e. call Postorder (right sub tree) – Visit the root
  • 39.
    Breadth First Search •Breadth First Traversal of a tree prints all the nodes of a tree level by level. • Breadth First Traversal is also called as Level Order Traversal.
  • 40.
    From a singletraversal it is not possible to construct unique binary tree. However any of the two traversals are given then the corresponding tree can be drawn uniquely: • Inorder and preorder • Inorder and postorder • Inorder and level order Building Binary Tree
  • 41.
    Building Binary Tree Example1: Construct a binary tree from a given preorder and inorder sequence: Preorder: A B D G C E H I F Inorder: D G B A H E I C F Solution:
  • 42.
  • 43.
    Implementation of BinaryTree struct tree { struct tree* lchild; char data[10]; struct tree* rchild; };
  • 44.
    Implementation of BinaryTree node* getnode() { node *temp ; temp = (node*) malloc(sizeof(node)); printf("n Enter Data: "); scanf("%s",temp->data); temp->lchild = NULL; temp->rchild = NULL; return temp; }
  • 45.
  • 46.
    B+ Tree A B+tree is a self-balancing tree data structure commonly used in databases and file systems for efficient storage and retrieval of large amounts of data. It is an extension of the B-tree, optimized for disk-based storage systems. Components of B+ Tree • Leaf nodes store all the key values and pointers to the actual data. • Internal nodes store only the keys that guide searches. • All leaf nodes are linked together, supporting efficient sequential and range queries.
  • 47.
  • 48.
    B+ Tree Features Balanced:Auto-adjusts when data is added or removed, keeping search time efficient. Multi-level: Has a root, internal nodes, and leaf nodes (which store the data). Ordered: Maintains sorted keys, making range queries easy. High Fan-out: Each node has many children, keeping the tree short and fast. Cache-friendly: Works well with memory caches for better speed. Disk-efficient: Ideal for disk storage due to fast data access.
  • 49.
    Searching record inB+ Tree • Start at the root node • Navigate through internal nodes based on key comparisons • Reach the appropriate leaf node • If the key exists, return the data; otherwise, report "record not found"
  • 50.
    Searching record inB+ Tree Example: In the image below, in order to search for 58, traverse the root → internal node → reach the correct leaf node that contains 58 (if it exists).
  • 51.
    Insertion in B+Tree Insertion in B+ Trees is done in three steps: 1. Navigate to the correct leaf node 2. Insert the new key in sorted order 3. If overflow occurs: • Split the leaf node • Push the middle key to the parent • This process may propagate up to the root
  • 52.
    Deletion in B+Tree 1. Finding and removing the key from the appropriate leaf node 2. Rebalancing the tree to maintain B+ Tree properties: • Borrowing keys from siblings or • Merging nodes if necessary
  • 53.
    Advantages of B+Tree • Data stored in a B+ tree can be accessed both sequentially and directly. • It takes an equal number of disk accesses to fetch records. • B+ trees have redundant search keys, and storing search keys repeatedly is not possible. • A B+ tree with 'l' levels can store more entries in its internal nodes compared to a B-tree having the same 'l' levels.
  • 54.
    Disadvantages of B+Tree Slower exact match: Data is always in leaf nodes, increasing search path. More disk access: Requires reaching the leaf even for a single record. Complex updates: Insertion and deletion are more complex. Extra space: Leaf node links need additional pointers.
  • 55.