Professional Documents
Culture Documents
Tree
Tree Graph
Non-Linear Data Structure
• Not stored in sequential order
• Degree of D = 0
• Degree of B = 1
• Degree of C = 2
Types of Binary tree
• A strictly binary tree is a tree in which every node other than the
leaves has two children.
• A perfect binary tree is a full binary tree in which all leaves are
at the same depth or same level.
POSTORDER(TREE)
Step 1: Repeat step 2 to 4 if TREE != NULL
Step 2: POSTORDER(TREE->LEFT)
Step 3: POSTORDER(TREE->RIGHT)
Step 4: Write “TREE->DATA”
[END OF WHILE]
Step 5: END
Post-order Traversal
Level-order traversal
• In Level-order traversal, all the nodes at a level are
accessed before going to the next level.
• This is known as breadth-first traversal algorithm.
Different Traversals
• Depth-first
o Pre-order:
F, B, A, D, C, E, G, I, H
(root, left, right)
o In-order:
A, B, C, D, E, F, G, H, I
(left, root, right)
o Post-order:
A, C, E, D, B, H, I, G, F
(left, right, root)
• Breadth-first
o Level-order:
F,B,G,A,D,I,C,E,H
Linked Binary Tree Implementation
Memory Representation
1 7 B -1
START 2 1 A 4
3 -1 E -1
4 3 C 6
AVAIL 5
6 -1 F -1
7 -1 D -1
Expression Trees
• Binary Trees are widely used to store algebraic
expressions.
• Preorder: 1 2 4 7 5 3 6 8 9
• Inorder: 7 4 2 5 1 8 6 9 3
• Postorder: 7 4 5 2 8 9 6 3 1
Right
Left Root
child
child
Right
child
Construct Tree
• Properties:
1. Each value (key) in the tree exists at most once (i.e. no
duplicates).
2. The "greater-than" and "less-than" relations are well defined for
the data value.
• Sorting constraints:- for every node n :
1. All data in the left subtree of n is less than the data in the root
of that subtree.
2. All data in the right subtree of n is greater than the data in the
root of that subtree.
Binary Search Tree
Draw a BST
Create a BST with following elements.
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81
Operations on BST
Search (Finding a value, whether it is present or not in tree)
• Why?
• Height of a BST
=2+1=3
Height of a Tree
Height(TREE)
Step 1: IF TREE = NULL, then
Return 0
ELSE
SET LeftHeight = Height(TREE->LEFT)
SET RightHeight = Height(TREE->RIGHT)
IF LeftHeight > RightHeight
Return LeftHeight + 1
ELSE
Return RightHeight + 1
[END OF IF]
[END OF IF]
Step 2: End
Determine number of nodes
• Total nodes of left sub-tree = 4
• Total nodes of right sub-tree = 3
totalNodes(TREE)
Step 1: IF TREE = NULL, then
Return 0
ELSE
Return totalNodes(TREE->LEFT) +
totalNodes(TREE-> Right) + 1
[END OF IF]
Step 2: End
Number of Internal Nodes
• Total internal nodes of left sub-tree = 2
• Total internal nodes of right sub-tree = 1
totalExternalNodes(TREE)
Step 1: IF TREE = NULL, then
Return 0
IF TREE -> LEFT = NULL and TREE -> RIGHT = NULL, then
Return 1
ELSE
Return totalExternalNodes(TREE->LEFT) +
totalExternalNodes (TREE->RIGHT)
[END OF IF]
Step 2: End
Finding the smallest Node
• Smallest Node must be in left side, as it is BST.
• Smallest Node :0
Finding the smallest Node
findSmallestNode(TREE)
Step 1: IF TREE = NULL OR TREE->LEFT = NULL, then
Return TREE
ELSE
Return findSmallestNode(TREE->LEFT)
[END OF IF]
Step 2: End
Finding the largest Node
•Largest Node must be in right side, as it is BST.
findLargestNode(TREE)
Step 1: IF TREE = NULL OR TREE->RIGHT = NULL,
then
Return TREE
ELSE
Return findLargestNode(TREE->RIGHT)
[END OF IF]
Step 2: End
Binary Search Tree - Worst Time
• Worst case running time is O(N)
• What if the input to binary search tree comes in a
sorted (ascending or descending) manner?
• The balance factor (bf) of a height balanced binary tree may take on
one of the values -1, 0, +1.
In the second tree, the left subtree of C has height 2 and the right
subtree has height 0, so the difference is 2.
In the third tree, the right subtree of A has height 2 and the left is
missing, so it is 0, and the difference is 2 again.
AVL Tree
• An AVL tree has balance factor calculated at every node.
• For every node, heights of left and right subtree can differ by no
more than 1
1 A 2 A 0 B
0 B 0 T3 1 B 0 T3 1 T1 0A
0 T1 0 T2 1 T1 0 T2 0 NN 0T2 0T3
0 NN
Example: LL Insert 9
Example: LL Insert 9
Example: LL Insert 18
RR
RR
A is unbalanced
Perform single left rotation:
A (unbalanced node) will become left child of B
Left child of B (T2) will become right child of A
0 A -2 A
1 1 B
T1 0 B 1 T1 -1 B
0 A 0 T3
0T2 0T3 0T2 -1 T3 0
0 0 T1 0 T2 NN
NN
Example: RR Insert 91
Example: RR Insert 91
Example: RR Insert 89
LR
LR
A is unbalanced
2 A
1 A -1 B 0 T3
0 B 0 T3 0 T1 -1 T2
0 T1 0 T2
0 NN
LR
First perform left rotation on B(left subtree):
• B will become left child of T2
• Left subtree of T2 will become right subtree of B
Then perform right rotation
• A will become right child of T2
• Right subtree of T2 will become left subtree of A
2 A 2 A
0 T2
-1 B 0 T3 1 T2 0 T3 1
B 0 A
0 T1 -1 T2 0 B 0
NN
0 T1 0 NN 0 T3
0 NN T1 0
Example#1 LR Insert 37
RL
A B
RL
A is unbalanced
-2 A
-1 A
0 T1 1 B
0 T1 0 B
-1 T2 0 T3
0 T2 0 T3
0 NN
RL
First perform right rotation on B(right subtree):
• B will become right child of T2
• Right subtree of T2 will become left subtree of B
Then perform left rotation
• A will become right child of T2
• Right subtree of T2 will become left subtree of A
-2 A -2 A
0 T2
0 -2 T2
0 T1 T1
1 B 1 A
0 B 0 B
-1 T2 0 T3 0 0 T3 0 T1 0 NN 0 T3
NN
0 NN
Construct AVL tree with 63, 9, 19, 27, 18,
108, 99, 81
Construct AVL tree with 63, 9, 19, 27, 18,
108, 99, 81
Initial AVL tree with balance factors: Balance ok
0 57
0 26 -1 72
+1 25 0 38 0 63 +1 94
0 3 0 37 0 47 0 78
+1 26 -1 72
+2 25 0 38 0 63 +1 94
+1 3 0 37 0 47 0 78
0 1
Next step: Find rebalancing case -->
Find rebalancing case Balance not ok
+1 57
+1 26 -1 72
Left Left Case
+2 25 0 38 0 63 +1 94
Pivot
+1 3 0 37 0 47 0 78
0 1
Next step: apply Left Left rebalancing -->
Rebalance and recalculate balance factors Balance ok
0 57
0 26 -1 72
0 3 0 38 0 63 +1 94
0 1 0 25 0 37 0 47 0 78
+1 57
-1 26 -1 72
0 3 +1 38 0 63 +1 94
0 1 0 25 +1 37 0 47 0 78
0 30
+2 57
-2 26 -1 72
0 3 +2 38 0 63 +1 94
0 1 0 25 +2 37 0 47 0 78
-1 30
0 32
Next step: Find rebalancing case -->
Find rebalancing case Balance not ok
+2 57
-2 26 -1 72
0 3 +2 38 0 63 +1 94
0 1 0 25 +2 37 0 47 0 78
-1 30
Left Right
Case 0 32
Next step: Rebalance (Step 1) -->
Rebalance (Step 1) Balance not ok
+2 57
-2 26 -1 72
0 3 +2 38 063 +1 94
0 1 0 25 +2 37 0 47 078
+1 32
0 30
Next step: Rebalance (Step 2) -->
Rebalance (Step 2) and recalculate balance Balance ok
factors
+1 57
-1 26 -1 72
0 3 +1 38 0 63 +1 94
0 1 0 25 0 32 0 47 0 78
0 30 0 37
+2 57
-2 26 -1 72
0 3 +2 38 063 +1 94
0 1 0 25 -1 32 0 47 078
0 30 +1 37
0 35
Next step: Find rebalancing case -->
Insert 35 Balance not ok
+2 57
0 1 0 25 -1 32 0 47 0 78
Left Right Case
0 30 +1 37
0 35
+2 57
-2 26 -1 72
0 3 +2 38 0 63 +1 94
0 1 0 25 +2 37 0 47 0 78
0 32
0 30 0 35
Next step: Rebalance (Step 2) -->
Rebalance (Step 2) Balance ok
+1 57
-1 26 -1 72
0 3 0 37 0 63 +1 94
0 1 0 25 0 32 -1 38 0 78
0 30 0 35 0 47
• https://www.cs.usfca.edu/~galles/visualization/AVLtree.html
Drawback of Binary Tree
• In linked list representation of binary tree, if any node
is not having a child we use NULL pointer in that
position.
all left child pointers that are NULL (in Linked list
representation) points to its in-order predecessor,
and all right child pointers that are NULL (in Linked
list representation) points to its in-order successor.
Disadvantage
1. This makes the Tree more complex .
2. More prone to errors.
Binary Tree w/o threading
Linked representation of BT
Linked Rep. of BT with Threading
BT with Threading
BT with Two Threads
BT with Two Threads
M-way Search Tree
• A binary search tree has one value in each node
and two subtrees. This notion easily generalizes to
an M-way search tree, which has (M-1) values per
node and M subtrees.
8. If x and y are any two ith and (i+1)th values of a node, where x<y,
then all the values appearing on (i+1)th sub-tree of that node are
greater than x and less than y.
• Insert 8
B-tree: Insert Operation
Insert 9, 39, 4
B-tree: Insert Operation
Insert 4
Practice
Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.
Insert the following elements in a B-Tree of order 3.
a, g, f, b, k, c, h, n, j
Construct a B-Tree of Order 5 for following numbers:
1,7,6,2,11,5,10,13,12,20,16,24,3,4,18,19,14,25
1. All the leaf nodes must be at same level.
2. All nodes except root must have at least ⌈m/2⌉ -1 keys and maximum of m-1 keys.
3. All non leaf nodes (internal nodes) except root must have at least m/2 children and at most
m children.
4. If the root node is a non leaf node, then it must have at least 2 children.
5. A non leaf node with n-1 keys must have n number of children.
6. All the key values within a node must be in Ascending Order.
7. All the values that appear on the left most child of a node are smaller than the first value of
that node. All the values that appear on the right most child of a node are greater than the last
value of that node.
8. If x and y are any two ith and (i+1)th values of a node, where x<y, then all the values
appearing on (i+1)th sub-tree of that node are greater than x and less than y.
B-tree: Delete Operation
1. Choose leaf node to delete.
2. If leaf node contains more than minimum of key
values(m/2), then delete it
3. Else choose from left or right sibling.
1. If left sibling has more than minimum then choose
largest key.
2. If right sibling has more than minimum then choose
smallest key.
3. Else choose from parent and balance tree.
• All nodes except root must have at least ⌈m/2⌉-1
keys and maximum of m-1 keys.
Order: 5 • All non leaf nodes (internal nodes) except root
Delete 93, 201, 180 must have at least m/2 children and at most m
children.
Order: 5| Delete 93, 201, 180
B+ Tree
• Similar to B trees, with a few slight differences.
• Leaf nodes are linked to each other.
General Trees
• General trees are those in which the number of subtrees for
any node is not required to be 0, 1, or 2.
2) Now consider T1, T2, T3 ... Tn are child nodes of the root node
in general tree.
3) The left most child (T1) of the root node in general tree becomes
left most child of root node in the binary tree.
• Size is not fixed: Like Linked Lists and unlike Arrays, Pointer
implementation of trees don’t have an upper limit on number of
nodes as nodes are linked using pointers.
• Trees can hold objects that are sorted by their keys.
• Router algorithms
• Expression Trees