Professional Documents
Culture Documents
07 Trees
07 Trees
Algorithms (DSA)
Dr Suryaprakash Kompalli
Agenda
• Introduction to the Tree Data Structure
• Binary Search Trees
• Red-Black Trees
• Adelson-Velskii and Landis (AVL) Trees
• Other structures:
– Trie, Min/Max heap, B-Trees
Introduction to Tree Data Structure
D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
– Height of a tree is same as height of root node 1 4 21
Example tree: 9 is the parent of nodes 3 and 19. 3 is the
parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
children of 9, and so on.
– Pointers
class TreeNode{
3 D: 1, H: 1
19 D: 1, H: 1
protected:
int value; D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
TreeNode* ptrToLeftChild; 1 4 21
TreeNode* ptrToRightChild;
};
Example tree: 9 is the parent of nodes 3 and 19. 3 is the
Data structure where a parent can parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
have at max 2 children children of 9, and so on.
class TreeNode{ PS: Depth and Height are shown in the gray box.
protected:
int value;
TreeNode** ptrToChildren;
};
Data structure where a parent can
have more than 2 children
ptrToLeftChild->deleteRightChild();
– Pointers ptrToLeftChild = NULL;
} 3
D: 1, H: 1
19 D: 1, H: 1
};
void TreeNode::deleteRightChild(){ D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
if (ptrToRightChild != NULL){
class TreeNode{
protected: ptrToRightChild->deleteLeftChild(); 1 4 21
int value; ptrToRightChild->deleteRightChild();
TreeNode* ptrToLeftChild; ptrToRightChild = NULL; Example tree: 9 is the parent of nodes 3 and 19. 3 is the
TreeNode* ptrToRightChild; }
};
parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
};
void TreeNode::setLeftChild(TreeNode* inLeftChild){ children of 9, and so on.
Data structure where a parent can if (ptrToLeftChild != NULL){
have at max 2 children char* message = new char[2048];
sprintf(message, "A left child already exists. This is an illegal state");
throw std::logic_error(message);
delete []message;
}
ptrToLeftChild = inLeftChild;
};
void TreeNode::setRightChild(TreeNode* inRightChild){
The code given on right if (ptrToRightChild != NULL){
char* message = new char[2048];
side maintains the tree sprintf(message, "A right child already exists. This is an illegal state");
throw std::logic_error(message);
data structure. There may delete []message;
}
be additional conditions ptrToRightChild = inRightChild;
depending on the type of };
– Pointers
3 D: 1, H: 1
19 D: 1, H: 1
D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
class TreeNode{
protected: 1 4 21
int value;
TreeNode* ptrToLeftChild; Example tree: 9 is the parent of nodes 3 and 19. 3 is the
TreeNode* ptrToRightChild; parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
};
children of 9, and so on.
void printTree(int offsetToPrint){
Data structure where a parent can offsetToPrint += offset_increment;
have at max 2 children if (this->ptrToRightChild != NULL)
this->ptrToRightChild->printTree(offsetToPrint);
std::string message;
int height = 0;
for (int i = offset_increment; i < offsetToPrint; i++){
height += 1;
message += "\t";
}
The code given on right char intValue[30];
sprintf(intValue, "%d, h=%d", this->value, height/offset_increment);
side prints the tree message += intValue;
structure. LogManager::writePrintfToLog(LogManager::Status, "TreeNode::printTree",
"%s", message.c_str());
if (this->ptrToLeftChild != NULL)
this->ptrToLeftChild->printTree(offsetToPrint);
}
Introduction to Tree Data Structure
};
}
3
D: 1, H: 1
19 D: 1, H: 1
void TreeNode::deleteRightChild(){
if (ptrToRightChild != NULL){ D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
class TreeNode{
protected:
ptrToRightChild->deleteLeftChild();
ptrToRightChild->deleteRightChild();
1 4 21
int value; ptrToRightChild = NULL;
TreeNode* ptrToLeftChild; } Example tree: 9 is the parent of nodes 3 and 19. 3 is the
TreeNode* ptrToRightChild; }; parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
}; void TreeNode::setLeftChild(TreeNode* inLeftChild){ children of 9, and so on.
Data structure where a parent can if (ptrToLeftChild != NULL){
char* message = new char[2048];
have at max 2 children
sprintf(message, "A left child already exists. This is an illegal state");
throw std::logic_error(message);
class TreeNode{ delete []message;
protected: }
int value; ptrToLeftChild = inLeftChild;
TreeNode** ptrToChildren; };
}; void TreeNode::setRightChild(TreeNode* inRightChild){
if (ptrToRightChild != NULL){
Data structure where a parent can char* message = new char[2048];
have more than 2 children sprintf(message, "A right child already exists. This is an illegal state");
throw std::logic_error(message);
delete []message;
The code given on create }
ptrToRightChild = inRightChild;
or print a tree will have to };
be modified if a node has
more than 2 children.
Introduction to Tree Data Structure
– Arrays
3 D: 1, H: 1
19 D: 1, H: 1
D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
Index: 0 1 2 3 4 5 6 7 1 4 21
Example tree: 9 is the parent of nodes 3 and 19. 3 is the
Value: 9 3 19 1 4 NULL 21 NULL parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
children of 9, and so on.
If index of parent is “p”, index of left child is: 2p + 1, index of
right child is: 2p + 2.
Eg:
Index of 9 is 0, indices of it’s children are: 1, 2.
Index of 3 is 1, indices of it’s children are: 3, 4.
Index of 19 is 2, indices of it’s children are: 5, 6.
Introduction to Tree Data Structure
– Arrays
3 D: 1, H: 1
19 D: 1, H: 1
D: 2, H: 0 D: 2, H: 0 D: 2, H: 0
Index: 0 1 2 3 4 5 6 7 1 4 21
Example tree: 9 is the parent of nodes 3 and 19. 3 is the
Value: 9 3 19 1 4 NULL 21 NULL parent of 1 and 4. 19 is the parent of 21. 3 and 19 are the
children of 9, and so on.
If index of parent is “p”, index of left child is: 2p + 1, index of
right child is: 2p + 2.
Eg: Note:
Index of 9 is 0, indices of it’s children are: 1, 2. This formulation assumes that the index starts from 0. The
Index of 3 is 1, indices of it’s children are: 3, 4. formulation to get indices will be different if the starting
Index of 19 is 2, indices of it’s children are: 5, 6. index is not zero.
• The BST has some interesting properties: Is this a BST? Yes !!!
• Code for inserting values into a tree has to be changed so that the
BST property is maintained
Binary Search Trees (BSTs)
• Code for inserting values into a tree has to be changed so that the
BST property is maintained.
class BinarySearchTreeNode : public TreeNode{
public:
class TreeNode{
protected:
int value;
TreeNode* ptrToLeftChild;
TreeNode* ptrToRightChild;
};
In this example, we have derived a class The addChild function shown above adds a new node while
of type BinarySearchTreeNode from the maintaining the Binary Search Tree property:
TreeNode. • The left child is smaller than or equal to the parent node
• The right child is greater than the parent node
class TreeNode{
protected:
int value;
TreeNode* ptrToLeftChild;
TreeNode* ptrToRightChild;
};
In this example, we have derived a class The buildTree function shown above takes as
of type BinarySearchTreeNode from the input an array of integer values and the number of
TreeNode. integers. It builds a BST by using the addChild
function, and returns the “root” or “head” node of
the tree.
int BinarySearchTreeNode::getMaximum(){
if (this->ptrToRightChild != NULL) Finding the maximum, minimum will take
this->ptrToRightChild->getMinimum(); O(h)
LogManager::writePrintfToLog(LogManager::Status, "BinarySearchTreeNode::getMaximum",
"Minimum value=%d", this->value);
} A successor of an int value is the smallest
element of the tree that is larger than the int
bool BinarySearchTreeNode::isPresent(int inValue){
if (this->value == inValue) value.
return true;
if (this->value < inValue)
if (this->ptrToLeftChild != NULL)
return ((BinarySearchTreeNode*) this->ptrToLeftChild)->isPresent(inValue);
else A predecessor of an int value is the largest
return false; element of the tree that is smaller than the
else
if (this->ptrToRightChild != NULL) int value.
return ((BinarySearchTreeNode*) this->ptrToRightChild)->isPresent(inValue);
else
return false; Finding successor or predecessor can also be
}
implemented in O(h)
int BinarySearchTreeNode::getMaximum(){
if (this->ptrToRightChild != NULL) Finding the maximum, minimum will take
this->ptrToRightChild->getMinimum(); O(h)
LogManager::writePrintfToLog(LogManager::Status, "BinarySearchTreeNode::getMaximum",
"Minimum value=%d", this->value);
} A successor of an int value is the smallest
element of the tree that is larger than the int
bool BinarySearchTreeNode::isPresent(int inValue){
if (this->value == inValue) value.
return true;
if (this->value < inValue)
if (this->ptrToLeftChild != NULL)
return ((BinarySearchTreeNode*) this->ptrToLeftChild)->isPresent(inValue);
else A predecessor of an int value is the largest
return false; element of the tree that is smaller than the
else
if (this->ptrToRightChild != NULL) int value.
return ((BinarySearchTreeNode*) this->ptrToRightChild)->isPresent(inValue);
else
return false; Finding successor or predecessor can also be
}
implemented in O(h)
int BinarySearchTreeNode::getMaximum(){
if (this->ptrToRightChild != NULL) Finding the maximum, minimum will take
this->ptrToRightChild->getMaximum(); O(h)
LogManager::writePrintfToLog(LogManager::Status, "BinarySearchTreeNode::getMaximum",
"Maximum value=%d", this->value);
} A successor of an int value is the smallest
element of the tree that is larger than the int
bool BinarySearchTreeNode::isPresent(int inValue){
if (this->value == inValue) value.
return true;
if (this->value < inValue)
if (this->ptrToLeftChild != NULL)
return ((BinarySearchTreeNode*) this->ptrToLeftChild)->isPresent(inValue);
else A predecessor of an int value is the largest
return false; element of the tree that is smaller than the
else
if (this->ptrToRightChild != NULL) int value.
return ((BinarySearchTreeNode*) this->ptrToRightChild)->isPresent(inValue);
else
return false; Finding successor or predecessor can also be
}
implemented in O(h)
Binary Search Trees (BSTs)
9
• Efficiency is dependent on the sequence of inserting values
3 19
– E.g: The tree shown as example here is obtained if the integers are inserted
in the following sequence: 9, 3, 19, 1, 4, 21. 1 4 21
– If values are inserted in the following sequence: 1, 19, 4, 3, 9, 21, the Is this a BST? Yes !!!
following BST is obtained:
1
19
4 21
3 9
Is this a BST? Yes !!!
Binary Search Trees (BSTs)
9
• Efficiency is dependent on the sequence of inserting values
3 19
– E.g: The tree shown as example here is obtained if the integers are inserted
in the following sequence: 9, 3, 19, 1, 4, 21. 1 4 21
– If values are inserted in the following sequence: 1, 19, 4, 3, 9, 21, the Is this a BST? Yes !!!
following BST is obtained:
1
19
4 21
3 9
Is this a BST? Yes !!!
19
4 21
3 9
Is this a BST? Yes !!!
19
4 21
3 9
Is this a BST? Yes !!!
– Red-Black trees are BST with an additional bit on each Node: color Red/
Black. If the bit is zero the node is Red. If the bit is one, the node is black
– Constraints on the colors present on a path from root to each leaf prevent a
leaf from from having more than twice the depth of any other leaf. This
makes Red-Black trees become approximately balanced, and hence more
efficient than BSTs
Red-Black Trees
9
• Constraints placed on Red-Black tree
3 19
1) Every node must have one color – red or black
1 4 21
2) Root node is black
3) All Leaf nodes must be black Is this a Red-Black tree? Yes !!!
9 21
4
Is this a Red-black tree? Not quite.
Number of black nodes between node 1
to 4 is more than from node 1 to 21.
Red-Black Trees
9
• Rotate operations used to modify red-black trees
3 19
19 4
Right-rotate(T, 19) 1 4 21
4 21 3 19
Is this a Red-Black tree? Yes !!!
3 9 9 21
In Right-rotate, the node on which you perform rotation is moved to the
right side of the tree. In this case, 19 is moved to the right. In order to
maintain BST condition, Note that 4 becomes the parent of 3 and 19. 9
becomes the child of 19.
4 19
3 19 4
Left-rotate(T, 4) 21
9 21 3 9
In Left-rotate, the node on which you perform rotation is moved to the left
side of the tree. In this case, 4 is moved to the left. In order to maintain
BST condition, 4 becomes the parent of 3 and 9. 19 becomes the parent
of 4 and 21.
Red-Black Trees
9
• Left-rotate and Right-rotate operations
3 19
Why are these operations useful? Using these operations, an unbalanced BST
can be converted to a balanced BST. i.e. a Tree that violates Red-black 1 4 21
conditions can be converted to a tree that follows Red-black constraints
Is this a Red-Black tree? Yes !!!
For example, consider the leftmost tree below. It is not a RedBlack tree. It can be
converted into a RedBlack tree by right-rotate and left-rotate operations.
19
9 21
4
Is this a Red-black tree? Not quite.
Number of black nodes between node 1
to 4 is more than from node 1 to 21.
Red-Black Trees
9
• Left-rotate and Right-rotate operations
3 19
Why are these operations useful? Using these operations, an unbalanced BST
can be converted to a balanced BST. i.e. a Tree that violates Red-black 1 4 21
conditions can be converted to a tree that follows Red-black constraints
Is this a Red-Black tree? Yes !!!
For example, consider the leftmost tree below. It is not a RedBlack tree. It can be
converted into a RedBlack tree by right-rotate and left-rotate operations.
19
1
9 21
Right-rotate(T, 19) 9
3
3 19
4
4 21
Is this a Red-black tree? Not quite.
Number of black nodes between node 1 Is this a Red-black tree? Yes !!!
to 4 is more than from node 1 to 21. While 4 and 9 are not black nodes, they can be
connected to “NULL” or dumy nodes which can
be marked black.
RedBlackTreeNode* RedBlackTree::addChild(int inValue, RedBlackTreeNode* possibleParent){
Red-Black Trees if (inValue == possibleParent->nodeValue) // Do not add the node if it already exists.
return NULL;
}
return addChild(inValue, possibleParent->leftChild);
5) All paths from a node to its’ leaf nodes must have same number of black nodes
Red-Black Trees if (inValue == possibleParent->nodeValue) // Do not add the node if it already exists.
return NULL;
}
return addChild(inValue, possibleParent->leftChild);
5) All paths from a node to its’ leaf nodes must have same number of black nodes
• Rotations and color changes in a specific sequence can fix violations to rules 2
and 4
Red-Black Trees
• Five constraints are placed on Red-Black tree
1) Every node must have one color – red or black
2) Root node is black
3) All Leaf nodes must be black
4) If a node is red, both children are black
5) All paths from a node to its’ leaf nodes must have same number of black nodes
• Steps in case 1:
– Set z.p and y to black
– z.p.p is set to red
– z = z.p.p; while loop continues
• Result of case 1:
– Below z.p.p, the Red-Black condition is satisfied
– Due to z.p.p becoming red color, it is possible that the
Red-Black condition may be violated if z.p.p is root node,
or z.p.p.p has red color. Fixup will be called on z.p.p
Red-Black Trees
• Three cases emerge where tree has to be adjusted/fixed:
– Nomenclature; z: Node to be fixed, z.p: parent of z, z’=z.p.p, grandfather of z, y: The sibling of z.p, or the other child
of z’.
– Case 1: z.color is red. z.p.color is red. y.color is red.
– Case 2: z.color is red. z.p.color is red. y.color is black. z is right child of z.p z.p.p
– Case 3: z.color is red. z.p.color is red. y.color is black. z is left child of z.p z.p y
– RB-Insert-Fixup ensures Red-Black tree conditions are satisfied
z
• Steps in case 1: Sample sub-tree for case 1
• Result of case 3:
– z.p is black, z.p is parent of z.p.p and z which are red; if z.p
had a right child, it would be black and will be left child of z.p.p
Red-Black Trees
• Three cases emerge where adjustments need to be made to tree:
– Nomenclature; z: Node to be adjusted, z.p: parent of z, z’=z.p.p, grandfather of z, y: The sibling of z.p, or the other
child of z’.
– Case 1: z.color is red. z.p.color is red. y.color is red.
– Case 2: z.color is red. z.p.color is red. y.color is black. z is right child of z.p z.p.p
– Case 3: z.color is red. z.p.color is red. y.color is black. z is left child of z.p z.p y
• Steps in case 2:
– Left-rotate is performed on z.p
• Result of case 2:
– z is now the left child of z.p.p
– z.p becomes the child of z
– Case 2 transforms into case 3.
Red-Black Trees
• Three cases emerge where adjustments need to be made to tree:
– Nomenclature; z: Node to be adjusted, z.p: parent of z, z’=z.p.p, grandfather of z, y: The sibling of z.p, or the other
child of z’.
– Case 1: z.color is red. z.p.color is red. y.color is red.
– Case 2: z.color is red. z.p.color is red. y.color is black. z is right child of z.p z.p.p
– Case 3: z.color is red. z.p.color is red. y.color is black. z is left child of z.p z.p y
• Steps in case 2: z
Sample sub-tree for case 2
– Left-rotate is performed on z.p
• Result of case 2:
– z is now the left child of z.p.p
– z.p becomes the child of z z.p.p
– Case 2 transforms into case 3.
z y
z.p
Result of case 2
Red-Black Trees
• Three cases emerge where adjustments need to be made to tree:
– Nomenclature; z: Node to be adjusted, z.p: parent of z, z’=z.p.p, grandfather of z, y: The sibling of z.p, or the other
child of z’.
z.p.p
– Case 1: z.color is red. z.p.color is red. y.color is red.
z.p y
– Case 2: z.color is red. z.p.color is red. y.color is black. z is right child of z.p
– Case 3: z.color is red. z.p.color is red. y.color is black. z is left child of z.p z
Sample sub-tree for case 2
• Steps in case 3: (After applying case 2) In the next iterataion of while loop
this node as the “new” value of z.
Note that in the new loop, the
parent of the new z is the parent
– z.p (old z) is set to black, z.p.p is set to red of previous z.p.p
– Right-rotate is performed on z.p.p
z
– While loop continues z.p.p
z.p z.p.p
z y
y
z.p
• Result of case 3:(After applying case 2) Result of case 3 (after
applying case 2) Result of case 2
• After a location is found, insert the node at that location. Correct violations in
the RedBlack properties by calling RB-Insert-Fixup
– This also takes O(lg n) time
Red-Black Trees
• Deletion of node from tree
– Deleting a node from a Red-Black tree may also need node adjustment
Left-Left (LL) at A
AVL Trees
• Types of rotations:
– Single rotations: Left-Left (LL), Right-Right (RR)
– Double rotations: Left-Right (LR), Right-left (RL)
bf=-2
A
bf=0
B
B bf=-1
A bf=0 C bf=0
C bf=0
Left-Left (LL) at A
The tree is imbalanced towards the Right side. The tree is imbalanced towards the Left side.
To balance this, rotate Left-Left To balance this, rotate Right-Right
AVL Trees
• Types of rotations:
– Single rotations: Left-Left (LL), Right-Right (RR)
– Double rotations: Left-Right (LR), Right-left (RL)
A bf=2 A bf=2
bf=0
C
B bf=-1 C bf=1
B bf=0 A bf=0
C bf=0 B bf=0
Left-Left (LL) at B Right-Right (RR) at A
Left-Right rotation at A
The tree is imbalanced towards left and then right. It is corrected by Left-Left rotation
followed by Right-Right rotation
AVL Trees
• Types of rotations:
– Single rotations: Left-Left (LL), Right-Right (RR)
– Double rotations: Left-Right (LR), Right-left (RL)
bf=-2 bf=-2
A A
bf=0
C
B bf=1 C bf=-1
A bf=0 B bf=0
C bf=0 B bf=0
Right-Right(RR) at B Left-Left(LL) at A
Right-Left rotation at A
The tree is imbalanced towards right and then left. It is corrected by Right-Right rotation
followed by Left-Left rotation
AVL Trees
• Example 1:
bf=-1 bf=-2
12 12
3 19 bf=-1 3 19 bf=-2
bf=0 bf=0
Insert 28
22 bf=0 22 bf=-1
28 bf=0
3 19 bf=-1 3 19 bf=-2
bf=0 bf=0
Insert 28
22 bf=0 22 bf=-1
28 bf=0
Select the nearest node from the added leaf having the
largest absolute “bf” value. This is node 19.
AVL Trees
• Example 1:
bf=-1 bf=-2
12 12
3 19 bf=-1 3 19 bf=-2
bf=0 bf=0
Insert 28
22 bf=0 22 bf=-1
28 bf=0
Select the nearest node from the added leaf having the
largest absolute “bf” value. This is node 19.
Select the nearest node from the added leaf having the
largest absolute “bf” value. This is node 19.
Select the nearest node from the added leaf having the
largest absolute “bf” value. This is node 12.
AVL Trees
• Example 2:
bf=-1 bf=-2
12 12
bf=0 bf=0
3 22 bf=0 3 22 bf=1
Insert 21
19 bf=0 28 bf=0 19 28 bf=0
bf=-1
21 bf=0
Balanced AVL Un-balanced
tree AVL tree
Select the nearest node from the added leaf having the
largest absolute “bf” value. This is node 12.
19 bf=0
bf=1
12 bf=0
Select the nearest node from the added leaf having the 21
largest absolute “bf” value. This is node 12. bf=0 Left-Left
3 22 bf=0 28 bf=0 rotation
The imbalance at node 12 is Right-Left at 12
Balanced AVL
Correct the tree by performing RL rotation, ie. Right-Right tree
followed by Left-Left at node 12
AVL Trees
• The insertion of a node into an AVL tree is made of two parts
• Finding the parent to which a node needs to be inserted.
– This can be performed in O(lg n) time.
• After a parent is found, insert a leaf node. Correct violations in the AVL balance
factor by performing rotation operations
– The AVL tree satisfies balance factor conditions before the new node is inserted
– Due to this nature, at most 2 rotations would be needed to correct the tree
Agenda
• Introduction to the Tree Data Structure
• Binary Search Trees
• Red-Black Trees
• Adelson-Velskii and Landis (AVL) Trees
• Other structures:
– Trie, Min/Max heap, B-Trees
Other Trees: Trie, Min/Max Heap, B-Trees
• Trie may be used to store heirarchical sequence data
• Min/Max Heaps are a special case of Binary trees
• B-Trees: Similar to Binary trees, but with several hundred children per node
Start/ NULL
A B L E
Head
B E Y NULL
P P L E NULL
B A S E NULL
Z O N E NULL
Other Trees: Trie, Min/Max Heap, B-Trees
• Trie may be used to store hierarchical sequence data, Example: dictionaries
– Time taken to search for a word having k alphabets: O(k lg m), where “m” is the number of
unique alphabets in the dictionary (For ASCII upper case m = 26)
Start/ NULL
A B L E
Head
B E Y NULL
P P L E NULL
B A S E NULL
Z O N E NULL
Other Trees: Trie, Min/Max Heap, B-Trees
• Min/Max Heaps are a special case of Binary trees
– The parent of each sub-tree is the smallest/largest element of the subtree
– Can be used to store store probability sequences, generate Huffman codes
– Insertion/Delection/Search and other operations take the same time as Binary tree
22
4 19
1 3 16
Existing Max-heap,
generated by inserting
integers: 19, 1, 4, 3,
22, 16
Other Trees: Trie, Min/Max Heap, B-Trees
• Min/Max Heaps are a special case of Binary trees
– The parent of each sub-tree is the smallest/largest element of the subtree
– Can be used to store store probability sequences, generate Huffman codes
– Insertion/Delection/Search and other operations take the same time as Binary tree
22 22
4 19 4 19
1 3 16 Insert 10 1 3 16
10
Existing Max-heap,
generated by inserting Insert 10 at a leaf node by following
integers: 19, 1, 4, 3, BST search conditions. This tree is
22, 16 not a max-heap, correct needs to be
called on node 10
Other Trees: Trie, Min/Max Heap, B-Trees
• Min/Max Heaps are a special case of Binary trees
– The parent of each sub-tree is the smallest/largest element of the subtree
– Can be used to store store probability sequences, generate Huffman codes
– Insertion/Delection/Search and other operations take the same time as Binary tree
22 22 22
4 19 4 19 10 19
1 3 16 Insert 10 1 3 16 Correct 1 4 16
10
10 3
Existing Max-heap,
generated by inserting Insert 10 at a leaf node by following Corrected max-heap tree
integers: 19, 1, 4, 3, BST search conditions. This tree is
22, 16 not a max-heap, correct needs to be
called on node 10
Other Trees: Trie, Min/Max Heap, B-Trees
• B-Trees: Similar to Binary trees, but with several hundred (or more) children per
node
– Used in managing memory paging/HDD caching systems, database entries
1900
120
2020 4800 5600
In this example, height of the tree = 2. Each parent has 2-4 child nodes. Red boxes
indicate the key values and yellow boxes indicate pointers to other nodes
Thank You !!!