SlideShare a Scribd company logo
1 of 20
Unit - 5
Advanced Tree
Prepared By:
Ms. Purvi Tandel
Topics of Unit 2
• Binary search trees
• Tree traversal (Inorder, postorder, preorder)
• Height Balanced tree
• AVL tree
 Splay tree
• B-Tree of order m
• Heap tree
• Binomial Heap
• Red-black tree
B-tree of order m
• In search trees like binary search tree, AVL Tree, Red-Black
tree, etc., every node contains only one value (key) and a
maximum of two children.
• But there is a special type of search tree called B-Tree in which
a node contains more than one value (key) and more than two
children.
• The number of keys in a node and number of children for a node
depends on the order of B-Tree.
• Every B-Tree has an order.
B-Tree is a self-balanced search tree in which every node contains multiple
keys and has more than two children.
B-tree of order m
B-Tree of Order m has the following properties...
Property #1 - All leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1 keys
and maximum of m-1 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2 children.
Property #4 - If the root node is a non leaf node, then it must
have atleast 2 children.
Property #5 - A non leaf node with n-1 keys must have n number of
children.
Property #6 - All the key values in a node must be in Ascending
Order.
Example: B-tree of order 4
B-Tree of Order 4 has the following properties...
Property #1 - All leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1=[4/2]-
1=1 keys and maximum of m-1 = 4-1= 3 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes)
must have at least m/2=4/2=2 children.
Property #4 - If the root node is a non leaf node, then it must
have atleast 2 children.
Property #5 - A non leaf node with n-1=4-1=3 keys must
have n=4 number of children.
Property #6 - All the key values in a node must be in Ascending
Order.
Example: B-tree of order 4
Operations on a B-Tree:
1.Search
2.Insertion
3.Deletion
B-tree of order m –Search operation
In a B-Tree, the search operation is performed with O(log n) time complexity.
The search operation is performed as follows...
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 repeat 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 and follow the right subtree if element larger, repeat the steps 3, 4, 5
and 6.
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.
B-tree of order m – Insert operation
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.
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(ceil(m/2)) 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.
B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Deleting item I:
1. Locate node n, which contains item I
2. If node n is not a leaf, swap I with in-order successor; deletion always begins at a
leaf
3. If leaf node n contains another item, just delete item I (ceil(m/2)-1)
else try to redistribute nodes from siblings
if not possible, merge node
B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Case – I:
Redistribution:
A sibling has 2 items:
redistribute item between siblings and parent
Merging:
No sibling has 2 items:
merge node
move item from parent to sibling
B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Case – II:
Redistribution:
Internal node n has no item left
redistribute
Merging:
Redistribution not possible:
merge node
move item from parent to sibling
adopt child of empty node.
B-tree of order m – Delete operation
There are three cases of deletion:
Case 1: A leaf node has to be deleted.
Case 2: An internal node has to be deleted.
Case 3: A root node to be deleted.
Case – III:
If merging process reaches the root and root is without item
delete root
B-tree of order m – Delete operation
Deletion Operation:
1. If search key K
2. If key K found at non-leaf node,
1. Swap with its inorder predecessor or successor element
3.If key K found at leaf node L,
i.Delete key K from node L
ii.If remaining keys at L or P >= ⌈m/2⌉-1
A. Do nothing
iii.Else
A.Check total keys at left sibling / right sibling node J
B.If total keys at J > ⌈m/2⌉ - 1
a.Perform Redistribution Operation
C.Else
a.Perform Merging Operation
D.After redistribution or merging operation cont. with step-ii for parent
node P.
B-tree of order m – Delete operation
Example: Construct B-tree of order 4 using given data.
11, 45, 34, 67, 78, 89, 90, 75, 110, 95
Delete: 90, 110, 75, 89
Heap Tree
• Heap data structure is a specialized binary tree-based data structure.
• Heap is a binary tree with special characteristics.
• In a heap data structure, nodes are arranged based on their values.
• A heap data structure some times also called as Binary Heap.
There are two types of heap data structures:
1.Max Heap
2.Min Heap
Heap Tree
Every heap data structure has the following properties:
Property #1 (Ordering): Nodes must be arranged in an order according to their
values based on Max heap or Min heap.
Property #2 (Structural): All levels in a heap must be full except the last level and
all nodes must be filled from left to right strictly.
Structural as well as max heap
Heap Tree
Max-Heap: In a Max-Heap the key present at the root node must be greatest among
the keys present at all of it’s children. The same property must be recursively true for
all sub-trees in that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among
the keys present at all of it’s children. The same property must be recursively true for
all sub-trees in that Binary Tree.
Max Heap sequence: 100, 40, 50, 10,
15, 50, 40
Min Heap sequence: 10, 15, 30, 40,
50, 100, 40
Heapify method rearranges the
elements of an array where the left and
right sub-tree of ith element obeys the
heap property.
Heap Tree
Properties:
Parent node location of child node k = floor(k/2)
Left child node location for parent node k = 2*k
Right child node location for parent node k = 2*k + 1
Max Heap sequence: 100, 40, 50, 10,
15, 50, 40
Min Heap sequence: 10, 15, 30, 40,
50, 100, 40
Heap Tree – Max Heap
Insertion:
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Deletion:
Deletion in Max Heap always happens at the root to remove the Maximum value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Finding Maximum Value Operation in Max Heap:
In a max heap, the root node has the maximum value than all other nodes. So,
directly we can display root node value as the maximum value in max heap.
Heap Tree – Min Heap
Insertion:
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is greater than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Deletion:
Deletion in Min Heap always happens at the root to remove the Minimum value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is greater than child, then swap them. (heapify)
Step 5 − Repeat step 3 & 4 until Heap property holds.
Finding Minimum Value Operation in Min Heap:
In a min heap, the root node has the minimum value than all other nodes. So, directly
we can display root node value as the minimum value in min heap.

More Related Content

Similar to Unit-5 Advanced tree zxcppt

Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxMalligaarjunanN
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)M Sajid R
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxLesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxCrisannJasmin
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxasimshahzad8611
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingBindiya syed
 

Similar to Unit-5 Advanced tree zxcppt (20)

Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptxLesson-7-Priority-Queues-and-Heap-Trees-1.pptx
Lesson-7-Priority-Queues-and-Heap-Trees-1.pptx
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Tree
TreeTree
Tree
 
Heap sort
Heap sortHeap sort
Heap sort
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 

Recently uploaded

9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...Delhi Escorts
 
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girlsMumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girlsPooja Nehwal
 
Horizon Net Zero Dawn – keynote slides by Ben Abraham
Horizon Net Zero Dawn – keynote slides by Ben AbrahamHorizon Net Zero Dawn – keynote slides by Ben Abraham
Horizon Net Zero Dawn – keynote slides by Ben Abrahamssuserbb03ff
 
Low Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service Bikaner
Low Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service BikanerLow Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service Bikaner
Low Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service BikanerSuhani Kapoor
 
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...Suhani Kapoor
 
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur EscortsCall Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...Suhani Kapoor
 
(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...ranjana rawat
 
VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130
VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130
VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130Suhani Kapoor
 
Freegle User Survey as visual display - BH
Freegle User Survey as visual display - BHFreegle User Survey as visual display - BH
Freegle User Survey as visual display - BHbill846304
 
Spiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnidsSpiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnidsprasan26
 
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...Cluster TWEED
 
Sustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesSustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesDr. Salem Baidas
 

Recently uploaded (20)

9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...9873940964 High Profile  Call Girls  Delhi |Defence Colony ( MAYA CHOPRA ) DE...
9873940964 High Profile Call Girls Delhi |Defence Colony ( MAYA CHOPRA ) DE...
 
9953056974 ,Low Rate Call Girls In Adarsh Nagar Delhi 24hrs Available
9953056974 ,Low Rate Call Girls In Adarsh Nagar  Delhi 24hrs Available9953056974 ,Low Rate Call Girls In Adarsh Nagar  Delhi 24hrs Available
9953056974 ,Low Rate Call Girls In Adarsh Nagar Delhi 24hrs Available
 
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girlsMumbai Call Girls, 💞  Prity 9892124323, Navi Mumbai Call girls
Mumbai Call Girls, 💞 Prity 9892124323, Navi Mumbai Call girls
 
Horizon Net Zero Dawn – keynote slides by Ben Abraham
Horizon Net Zero Dawn – keynote slides by Ben AbrahamHorizon Net Zero Dawn – keynote slides by Ben Abraham
Horizon Net Zero Dawn – keynote slides by Ben Abraham
 
Low Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service Bikaner
Low Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service BikanerLow Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service Bikaner
Low Rate Call Girls Bikaner Anika 8250192130 Independent Escort Service Bikaner
 
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service NashikLow Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
Low Rate Call Girls Nashik Lavanya 7001305949 Independent Escort Service Nashik
 
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
Call Girls In { Delhi } South Extension Whatsup 9873940964 Enjoy Unlimited Pl...
 
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
VIP Call Girls Saharanpur Aaradhya 8250192130 Independent Escort Service Saha...
 
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur EscortsCall Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
Call Girl Nagpur Roshni Call 7001035870 Meet With Nagpur Escorts
 
Call Girls In Delhi 9953056974 (Low Price) Escort Service Pushp Vihar
Call Girls In Delhi 9953056974 (Low Price) Escort Service Pushp ViharCall Girls In Delhi 9953056974 (Low Price) Escort Service Pushp Vihar
Call Girls In Delhi 9953056974 (Low Price) Escort Service Pushp Vihar
 
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  kashmiri gate (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In kashmiri gate (Delhi) Call Us 9953056974
 
VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
VIP Call Girls Ramanthapur ( Hyderabad ) Phone 8250192130 | ₹5k To 25k With R...
 
(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
(NANDITA) Hadapsar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune ...
 
VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130
VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130
VIP Call Girls Service Bandlaguda Hyderabad Call +91-8250192130
 
Freegle User Survey as visual display - BH
Freegle User Survey as visual display - BHFreegle User Survey as visual display - BH
Freegle User Survey as visual display - BH
 
Spiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnidsSpiders by Slidesgo - an introduction to arachnids
Spiders by Slidesgo - an introduction to arachnids
 
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
webinaire-green-mirror-episode-2-Smart contracts and virtual purchase agreeme...
 
Sustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and ChallengesSustainable Clothing Strategies and Challenges
Sustainable Clothing Strategies and Challenges
 
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls ServicesGandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
Gandhi Nagar (Delhi) 9953330565 Escorts, Call Girls Services
 
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCREscort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
Escort Service Call Girls In Shakti Nagar, 99530°56974 Delhi NCR
 

Unit-5 Advanced tree zxcppt

  • 1. Unit - 5 Advanced Tree Prepared By: Ms. Purvi Tandel
  • 2. Topics of Unit 2 • Binary search trees • Tree traversal (Inorder, postorder, preorder) • Height Balanced tree • AVL tree  Splay tree • B-Tree of order m • Heap tree • Binomial Heap • Red-black tree
  • 3. B-tree of order m • In search trees like binary search tree, AVL Tree, Red-Black tree, etc., every node contains only one value (key) and a maximum of two children. • But there is a special type of search tree called B-Tree in which a node contains more than one value (key) and more than two children. • The number of keys in a node and number of children for a node depends on the order of B-Tree. • Every B-Tree has an order. B-Tree is a self-balanced search tree in which every node contains multiple keys and has more than two children.
  • 4. B-tree of order m B-Tree of Order m has the following properties... Property #1 - All leaf nodes must be at same level. Property #2 - All nodes except root must have at least [m/2]-1 keys and maximum of m-1 keys. Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at least m/2 children. Property #4 - If the root node is a non leaf node, then it must have atleast 2 children. Property #5 - A non leaf node with n-1 keys must have n number of children. Property #6 - All the key values in a node must be in Ascending Order.
  • 5. Example: B-tree of order 4 B-Tree of Order 4 has the following properties... Property #1 - All leaf nodes must be at same level. Property #2 - All nodes except root must have at least [m/2]-1=[4/2]- 1=1 keys and maximum of m-1 = 4-1= 3 keys. Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at least m/2=4/2=2 children. Property #4 - If the root node is a non leaf node, then it must have atleast 2 children. Property #5 - A non leaf node with n-1=4-1=3 keys must have n=4 number of children. Property #6 - All the key values in a node must be in Ascending Order.
  • 6. Example: B-tree of order 4 Operations on a B-Tree: 1.Search 2.Insertion 3.Deletion
  • 7. B-tree of order m –Search operation In a B-Tree, the search operation is performed with O(log n) time complexity. The search operation is performed as follows... 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 repeat 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 and follow the right subtree if element larger, repeat the steps 3, 4, 5 and 6. 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.
  • 8. B-tree of order m – Insert operation 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. 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(ceil(m/2)) 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.
  • 9. B-tree of order m – Delete operation There are three cases of deletion: Case 1: A leaf node has to be deleted. Case 2: An internal node has to be deleted. Case 3: A root node to be deleted. Deleting item I: 1. Locate node n, which contains item I 2. If node n is not a leaf, swap I with in-order successor; deletion always begins at a leaf 3. If leaf node n contains another item, just delete item I (ceil(m/2)-1) else try to redistribute nodes from siblings if not possible, merge node
  • 10. B-tree of order m – Delete operation There are three cases of deletion: Case 1: A leaf node has to be deleted. Case 2: An internal node has to be deleted. Case 3: A root node to be deleted. Case – I: Redistribution: A sibling has 2 items: redistribute item between siblings and parent Merging: No sibling has 2 items: merge node move item from parent to sibling
  • 11. B-tree of order m – Delete operation There are three cases of deletion: Case 1: A leaf node has to be deleted. Case 2: An internal node has to be deleted. Case 3: A root node to be deleted. Case – II: Redistribution: Internal node n has no item left redistribute Merging: Redistribution not possible: merge node move item from parent to sibling adopt child of empty node.
  • 12. B-tree of order m – Delete operation There are three cases of deletion: Case 1: A leaf node has to be deleted. Case 2: An internal node has to be deleted. Case 3: A root node to be deleted. Case – III: If merging process reaches the root and root is without item delete root
  • 13. B-tree of order m – Delete operation Deletion Operation: 1. If search key K 2. If key K found at non-leaf node, 1. Swap with its inorder predecessor or successor element 3.If key K found at leaf node L, i.Delete key K from node L ii.If remaining keys at L or P >= ⌈m/2⌉-1 A. Do nothing iii.Else A.Check total keys at left sibling / right sibling node J B.If total keys at J > ⌈m/2⌉ - 1 a.Perform Redistribution Operation C.Else a.Perform Merging Operation D.After redistribution or merging operation cont. with step-ii for parent node P.
  • 14. B-tree of order m – Delete operation Example: Construct B-tree of order 4 using given data. 11, 45, 34, 67, 78, 89, 90, 75, 110, 95 Delete: 90, 110, 75, 89
  • 15. Heap Tree • Heap data structure is a specialized binary tree-based data structure. • Heap is a binary tree with special characteristics. • In a heap data structure, nodes are arranged based on their values. • A heap data structure some times also called as Binary Heap. There are two types of heap data structures: 1.Max Heap 2.Min Heap
  • 16. Heap Tree Every heap data structure has the following properties: Property #1 (Ordering): Nodes must be arranged in an order according to their values based on Max heap or Min heap. Property #2 (Structural): All levels in a heap must be full except the last level and all nodes must be filled from left to right strictly. Structural as well as max heap
  • 17. Heap Tree Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. Min-Heap: In a Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. Max Heap sequence: 100, 40, 50, 10, 15, 50, 40 Min Heap sequence: 10, 15, 30, 40, 50, 100, 40 Heapify method rearranges the elements of an array where the left and right sub-tree of ith element obeys the heap property.
  • 18. Heap Tree Properties: Parent node location of child node k = floor(k/2) Left child node location for parent node k = 2*k Right child node location for parent node k = 2*k + 1 Max Heap sequence: 100, 40, 50, 10, 15, 50, 40 Min Heap sequence: 10, 15, 30, 40, 50, 100, 40
  • 19. Heap Tree – Max Heap Insertion: Step 1 − Create a new node at the end of heap. Step 2 − Assign new value to the node. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. (heapify) Step 5 − Repeat step 3 & 4 until Heap property holds. Deletion: Deletion in Max Heap always happens at the root to remove the Maximum value. Step 1 − Remove root node. Step 2 − Move the last element of last level to root. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. (heapify) Step 5 − Repeat step 3 & 4 until Heap property holds. Finding Maximum Value Operation in Max Heap: In a max heap, the root node has the maximum value than all other nodes. So, directly we can display root node value as the maximum value in max heap.
  • 20. Heap Tree – Min Heap Insertion: Step 1 − Create a new node at the end of heap. Step 2 − Assign new value to the node. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is greater than child, then swap them. (heapify) Step 5 − Repeat step 3 & 4 until Heap property holds. Deletion: Deletion in Min Heap always happens at the root to remove the Minimum value. Step 1 − Remove root node. Step 2 − Move the last element of last level to root. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is greater than child, then swap them. (heapify) Step 5 − Repeat step 3 & 4 until Heap property holds. Finding Minimum Value Operation in Min Heap: In a min heap, the root node has the minimum value than all other nodes. So, directly we can display root node value as the minimum value in min heap.