Professional Documents
Culture Documents
Tree Traversal
General Tree
15/02/2024 1
Preorder Traversal
• RACDEBF
Level order Traversal
• RABCDEF
Postorder Traversal
• CDEAFBR
• Note: Inorder traversal is not
applicable in general tree.
15/02/2024 2
General Tree Representation
• List Representation
• Left(First) Child/Right Sibling Representation
15/02/2024 3
List Representation
15/02/2024 4
Left(First) Child/Right Sibling
Representation
15/02/2024 5
Example: Activity
15/02/2024 6
Binary Tree
• A binary tree is a data structure specified as a set of so-called node
elements. The topmost element in a binary tree is called the root
node, and each node has 0, 1, or at most 2 nodes.
Examples:
15/02/2024 7
Properties of Binary Tree
• At each level of i, the maximum number of nodes is 2 power I ( ).
• Therefore, the maximum number of nodes at height 3 is equal to
(1+2+4+8) = 15. In general, the maximum number of nodes possible
at height h is (20 + 21 + 22+….2h) = 2(power h)+1 -1.
• The minimum number of nodes possible at height h is equal to h+1.
• If the number of nodes is minimum, then the height of the tree would
be maximum. Conversely, if the number of nodes is maximum, then
the height of the tree would be minimum
15/02/2024 8
Types of Binary Tree
• Full/Proper/Strict Binary tree
• Complete Binary tree
• Almost complete Binary tree
• Perfect Binary tree
• Left Skewed Binary tree
• Right Skewed Binary tree
• Balanced tree
• Extended Binary tree
15/02/2024 9
Full/Proper/Strict/2- tree Binary tree
• The full binary tree is also known as a strict binary tree.
• The tree can only be considered as the full binary tree if each node
must contain either 0 or 2 children.
• The full binary tree can also be defined as the tree in which each node
must contain 2 children except the leaf nodes.
15/02/2024 10
Complete Binary tree
• The complete binary tree is a tree in which all the nodes are
completely filled except the last level.
• In the last level, all the nodes must be as left as possible.
• In a complete binary tree, the nodes should be added from the left.
15/02/2024 11
Almost complete Binary tree
• An almost complete binary tree is a special kind of binary tree where
insertion takes place level by level and from left to right order at each
level and the last level is not filled fully always.
15/02/2024 12
Perfect Binary tree
• A binary tree is said to be ‘perfect’ if all the internal nodes have
strictly two children, and every external or leaf node is at the same
level or same depth within a tree.
• A perfect binary tree having height ‘h’ has (2 power h) – 1[ ]node.
Here is the structure of a perfect binary tree:
15/02/2024 13
Left Skewed Binary Tree
• These are those skewed binary trees in which all the nodes are having a left child or no child at all.
• It is a left side dominated tree.
• All the right children remain as null.
15/02/2024 14
Right Skewed Binary Tree
• These are those skewed binary trees in which all the nodes are having a right child or no child at all.
• It is a right side dominated tree.
• All the left children remain as null.
15/02/2024 15
Balanced Binary Tree
• In a balanced binary tree, the height of the left and the right subtrees of
each node should vary by at most one.
15/02/2024 16
Extended Binary Tree
Extended binary tree is a type of binary tree in which all the null sub tree of the original tree are replaced with special
nodes called external nodes whereas other nodes are called internal nodes
15/02/2024 17
Binary Tree Representation
• A binary tree data structure is represented using two methods. Those
methods are as follows...
1. Array Representation
2. Linked List Representation
15/02/2024 18
Array Representation
• If Parent position is “i” then
• Leftchild is at position “2i+1”
• Right child is at position “2i+2”
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A B C D F G H I J - - - K - - - -
15/02/2024 19
Linked List Representation struct node {
int data;
Left Child Parent Right Child
Address Address Address struct node* left;
struct node* right;
};
15/02/2024 20
1. Inorder Traversal
15/02/2024 22
Binary Tree Traversal
3. Postorder Traversal (Practice): Program:
void printPostorder(struct node* node)
Algorithm Postorder(tree)
{
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
if (node == NULL)
3. Visit the root. return;
15/02/2024 23
Binary Tree Traversal
• 4. Level Order Traversal
15/02/2024 24
Expression Tree
• The expression tree is a binary tree in which each internal node corresponds to
the operator and each leaf node corresponds to the operand.
• Example expression tree for 3 + ((5+9)*2) would be:
15/02/2024 25
Example
• a + (b * c) + d * (e + f)
15/02/2024 26
Steps to Construct Expression tree
• Read one symbol at a time from the postfix expression.
• Check if the symbol is an operand or operator.
• If the symbol is an operand, create a one node tree and push a pointer onto a
stack
• If the symbol is an operator, pop two pointers from the stack namely T1 & T2
and form a new tree with root as the operator, T1 & T2 as a left and right child
• A pointer to this new tree is pushed onto the stack
• Thus, An expression is created or constructed by reading the symbols or
numbers from the left. If operand, create a node. If operator, create a tree
with operator as root and two pointers to left and right subtree
15/02/2024 27
Example – Construct an Expression tree for
the given expression ab+c*
1. The first two symbols are operands, we create one- node tree and 3. Next, 'c' is read, we create one node tree and push a pointer to it onto the
push a pointer to them onto the stack. stack.
2. Next, read a'+' symbol, so two pointers to tree are popped, a new tree is 4. Finally, the last symbol is read ' * ', pop two tree pointers and form a new tree with a, '
formed and push a pointer to it onto the stack. * ' as root, and a pointer to the final tree remains on the stack.
15/02/2024 28
Traversing the Expression Tree
• Inorder Traversal – L P R
Infix Expression: a + b * c + d * (e + f)
• Preorder Traversal – P L R
Prefix Expression: ++a*bc*d+ef
• Postorder Traversal – L R P
Postfix Expression: a b c * + d e f + * +
15/02/2024 29
Threaded Binary Tree
• In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage space.
• If a binary tree consists of n nodes then n+1 link fields contain NULL
values.
• So in order to effectively manage the space, a method was devised by
Perlis and Thornton in which the NULL links are replaced with special
links known as threads.
• Such binary trees with threads are known as threaded binary trees.
• Each node in a threaded binary tree either contains a link to its child
node or thread to other nodes in the tree.
15/02/2024 30
Threaded Binary Tree
• One-way threaded Binary Tree
• Two-way threaded Binary Tree
15/02/2024 31
One-way threaded Binary Tree
• In one-way threaded binary trees, a thread will appear either in the right or left
link field of a node.
• If it appears in the right link field of a node then it will point to the next node that
will appear on performing in order traversal. Such trees are called Right threaded
binary trees.
• If thread appears in the left field of a node then it will point to the nodes inorder
predecessor. Such trees are called Left threaded binary trees.
• Left threaded binary trees are used less often as they don't yield the last
advantages of right threaded binary trees.
• In one-way threaded binary trees, the right link field of last node and left link field
of first node contains a NULL.
• In order to distinguish threads from normal links they are represented by dotted
lines.
15/02/2024 32
One-way threaded Binary Tree
15/02/2024 33
Two-way threaded Binary Tree
• In two-way threaded Binary trees,
• the right link field of a node containing NULL values is replaced by a thread that points to nodes inorder
successor and
• left field of a node containing NULL values is replaced by a thread that points to nodes inorder predecessor.
15/02/2024 34
Threaded Binary Tree Node Structure
A
struct node
{ Inorder Traversal: B A C
struct node *left; B C
boolean lthread;
int info;
boolean rthread;
struct node *right; 0 A 0
};
NULL 0 B 1 1 C 0 NULL
15/02/2024 35
Binary Search Tree Operations
• Create
• Search
• Insert
• Delete
• Preorder
• Inorder
• Postorder
• Maximum
• minimum
15/02/2024 36
node *create()
{
Create node *temp;
printf("nEnter data:");
temp=(node*)malloc(sizeof(node));
45, 15, 79, 90, 10, 55, 12, 20, 50 scanf("%d",&temp->data);
temp->left=temp->right=NULL;
return temp;
}
15/02/2024 37
Search
Pseudocode:
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)
Search 4
Compare 4 with 8, 4<8, check with the left child of 8
Compare 4 with 3, 4>3, check with the right child of 3
Compare 4 with 6, 4<6, check with the left child of 6
Compare 4 with 4, 4==8, node found, return the address.
15/02/2024 38
EXAMPLE 1:
Searching a node with value 12 in the given binary search tree
15/02/2024 42
15/02/2024 43
Delete
There are three cases for deleting a node from a binary search tree.
Case I – Delete leaf node
• In the first case, the node to be deleted is the leaf node. In such a case, simply delete
the node from the tree.
15/02/2024 45
Case 2: Deleting a node with one child
EXAMPLE : Deleting node 54 from the given binary search tree
15/02/2024 46
Case 3: Deleting a node with two children
EXAMPLE 1 : Deleting node 56 from the given binary search tree
15/02/2024 47
15/02/2024 48
15/02/2024 49
15/02/2024 50
Better BST
• Binary Search Tree which has optimal performance
• AVL Trees
• Splay Trees
• B-Trees
• Red-Black Trees
15/02/2024 51
AVL Tree
An AVL tree is a binary search tree in which the difference between the height of the right
and left sub trees or the root node is never more than one.
The definition of a height balanced binary tree requires that every sub tree also be height
balanced. The balance factor of the nodes decides if the tree is balanced or not.
Balance Factor: The balance factor BF(T) of a node T is a binary tree is defined to be
B.F = hL – hR
15/02/2024 52
Rotations
• LL: New node Y is inserted in the Left Subtree of the Left Subtree of A
-> Single Rotation with left.
• LR: Y is inserted in the right subtree of the Left Subtree of A -> Double
Rotation with Left
• RR: Y is inserted in the right subtree of the right subtree of A -> Single
Rotation with Right
• RL: Y is inserted in the left subtree of the right subtree of A -> Double
Rotation with Right
15/02/2024 53
Single Rotation – Left Rotation (LL)
static Position SingleRotateWithLeft (Position K2)
{
Position K1;
K1=K2left;
K2Left = K1Right;
K1Right =K2;
K2height= Max (height (K2left), height (K2-Right)) +1;
K1height= Max (height(K1Left), height(K1Right)) +1;
return K1;
}
After LL
Insert (1)
15/02/2024 54
Single Rotation – Right Rotation (RR)
static Position SingleRotateWithRight (Position K1)
{
Position K2;
K2=K1Right;
K1Right = K2Left;
K2Left=K1;
K2height= Max (height (K2left), height (K2-Right)) +1;
K1height= Max (height(K1left), height(K1Right)) +1;
return K2;
}
After RR
Insert (13)
15/02/2024 55
Double Rotation- LR Rotation
15/02/2024 56
Double Rotation- LR Rotation
After Insert 12
and 18
After RR After LL
15/02/2024 57
Double Rotation- RL Rotation
15/02/2024 58
Double Rotation- RL Rotation
After Insert 11
and 14
After LL After RR
15/02/2024 59
Insert
15/02/2024 60
15/02/2024 61
15/02/2024 62
AVL TREE – NODE DELETION -
Example
• Delete nodes 52, 36, and 61 from the AVL tree given
15/02/2024 63
Splay Tree
• Splay tree in data structure is actually a variant or type of binary search tree which
is capable of self-adjusting and self-balancing functionality.
• In this type of data structure whenever any operation is carried out on the tree it is
followed by a special operation called as splay.
• The splay operation involves rearrangement of the data nodes in the tree so that
the current element which the operations are being carried out is set to as the root
node of the tree data structure.
15/02/2024 64
Splay Tree
• Self-balancing BST with an additional property that recently accessed
elements can be re-accessed fast.
• All operations can be performed in O(log n) time.
• All operations can be performed by combining with the basic operation
called splaying.
• Splaying the tree for a particular node rearranges the tree to place that
node at the root.
• Each splay step depends on three factors:
• Whether N is the left or right child of its parent P,
• Whether P is the root or not, and if not,
• Whether P is the left or right child of its parent, G (N’s grandparent).
15/02/2024 65
Splay tree operations
• Insertion
• Deletion
• Search
• Rotations(splaying)
15/02/2024 66
Splay Tree Rotations
15/02/2024 67
Splay Tree Rotations
• Zig: When the left rotation is performed.
• Zag: When right rotation is carried out.
• Zig zag: When we perform zig followed by zag that means right followed
by left.
• Zag zig: When we perform zag followed by zig that means left followed
by the right.
• Zig zig: When two right operations are carried out.
• Zag zag: When two left operations are performed.
15/02/2024 68
Zig Rotation
15/02/2024 69
Zag Rotation
15/02/2024 70
Zig-Zig Rotation
15/02/2024 71
Zag-Zag Rotation
15/02/2024 72
Zig-Zag Rotation
15/02/2024 73
Zag-Zig Rotation
15/02/2024 74
Example
15/02/2024 75
Class Activity
15/02/2024 76
Zig Rotation
• The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree rotations.
In zig rotation, every node moves one position to the right from its current position.
15/02/2024 77
Zag Rotation
• The Zag Rotation in splay tree is similar to the single left
rotation in AVL Tree rotations. In zag rotation, every node
moves one position to the left from its current position.
15/02/2024 78
Zig-Zig Rotation
• The Zig-Zig Rotation in splay tree is a double zig rotation. In
zig-zig rotation, every node moves two positions to the right
from its current position.
15/02/2024 79
Zag-Zag Rotation
• The Zag-Zag Rotation in splay tree is a double zag rotation. In
zag-zag rotation, every node moves two positions to the left
from its current position.
15/02/2024 80
Zig-Zag Rotation
• The Zig-Zag Rotation in splay tree is a sequence of zig rotation followed
by zag rotation. In zig-zag rotation, every node moves one position to
the right followed by one position to the left from its current position.
15/02/2024 81
Zag-Zig Rotation
• The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed
by zig rotation. In zag-zig rotation, every node moves one position to the
left followed by one position to the right from its current position.
15/02/2024 82
Insertion Operation in Splay Tree
• The insertion operation in Splay tree is performed using
following steps...
• Step 1 - Check whether tree is Empty.
• Step 2 - If tree is Empty then insert the newNode as Root
node and exit from the operation.
• Step 3 - If tree is not Empty then insert the newNode as leaf
node using Binary Search tree insertion logic.
• Step 4 - After insertion, Splay the newNode
15/02/2024 83
Deletion Operation in Splay Tree
• The deletion operation in splay tree is similar to deletion
operation in Binary Search Tree. But before deleting the
element, we first need to splay that element and then delete
it from the root position.
15/02/2024 84
15/02/2024 85
15/02/2024 86
15/02/2024 87
Red-Black Trees
• Self balancing binary search tree
• Also called as symmetric binary B-tree
• Although red-black tree is complex, all operations can be done in a worst case time complexity of
O(log n) where n is the number of nodes in the tree.
• Properties of red-black trees
• A red-black tree is a BST which has the following properties
1. The color of a node is either red or black.
2. The color of the root node is always black.
3. All leaf nodes are black.
4. Every red node has both the children colored in black.
5. Every simple path from a given node to any of its leaf nodes has an equal number of black nodes
15/02/2024 88
Red Black Trees
• Red Black Tree is a Binary Search Tree in which every node is colored either RED Or BLACK.
15/02/2024 89
Red-Black Tree - Example
15/02/2024 90
Exercise
• Say whether the following trees are red-black or not.
15/02/2024 91
Red Black Tree Rotations
• Left Rotate • Right Rotate
15/02/2024 96