You are on page 1of 169

Nonlinear Data Structure:

Tree

Dr. Jyoti Srivastava


Basics
• Linear Data Structure:
Linear data structure is linear if element is adjacent to
each other. It has exactly two neighbours elements to
which it is connected as its previous and next member.

• Example of Linear Data Structure


1. Array
2. Linked List
3. Stack
4. Queue
Non-Linear Data Structure

• Non-Linear data structure is that if one element can be


connected to more than two adjacent element then it is
known as non-linear data structure.

• Every data item is attached to several other data items


in a way that is specific for reflecting relationships.
The data items are not arranged in a sequential
structure.

• Ex: Trees, Graphs


Examples

Tree Graph
Non-Linear Data Structure
• Not stored in sequential order

• Branches to more than one node

• Can’t be traversed in a single run

• Data members are not processed one after another


Tree
A tree is a Multilevel data
structure that represent a
hierarchical relationship
between the set of
individual elements called
nodes.

The defining property of a


tree is that there is
precisely one path
connecting any two nodes.
Tree
• A vertex (or node) is a simple
object that can have a name and
can carry other associated
information. Left pointer, Right
pointer and a data element.
• The first or top node in a tree is
called the root node.
• An edge is a connection between
two vertices.
• A path in a tree is a list of distinct
vertices in which successive
vertices are connected by edges in
the tree.
example: {a, b, d, i } is path
• Nodes with no children are leaves
or terminal nodes.
Binary Trees
• A binary tree is a tree where each node has exactly
zero, one or two children.
• Each parent can have no more than 2 children.
• e, h, f, g are having zero or no successors and thus are
said to be empty sub-trees.
Siblings
• Siblings: B and C are said to be left
and right child of A, while B and C
are known as siblings.

• All nodes at same level and share


the same parent are known as
siblings.
Level Number
• Every node in a binary tree is
having Level Number.

• Root node is defined at level 0.

• Left and Right child of Root node is


at level 1.

• So, a child’s level number is defined


as parent’s level number + 1.
Degree
• Degree of a node is equal to the
number of children it has.

• Degree of leaf node is always


zero.

• 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.

• A complete binary tree is a binary tree in which every level,


except possibly the last, is completely filled, and all nodes are as
far left as possible.
Strictly Binary Trees
• If every non-leaf node in a binary tree has
nonempty left and right subtrees, the tree is
called a strictly binary tree.

• Or a node will have either 2 children or


no children at all.

• A strictly binary tree with n leaves always


contains 2n -1 nodes.
Complete Binary Tree
A binary tree T with n levels is complete
• if all levels except possibly the last are completely full,
• and the last level has all its nodes to the left side.
Complete Binary Tree

• Level 0 has 2^0 = 1 Node


• Level 1 has 2^1 = 2 Node
• Level 2 has 2^2 = 4 Node

• If K is the parent, then (Consider K as root node)


1. Left child – 2*K = 2*1 = 2
2. Right child – (2*K)+1 = (2*1)+1 = 3
Binary Tree Traversal
• Often, one wishes to visit each of the nodes in a tree
and examine the value there, a process called
Traversal.

• There are several common orders in which the nodes


can be visited, and each has useful properties that are
exploited in algorithms based on binary trees:

1. In-Order: Left child, Root, Right child.


2. Pre-Order: Root, Left child, Right child
3. Post-Order: Left Child, Right child, Root
Pre-order Algorithm
PREORDER(TREE)
Step 1: Repeat step 2 to 4 if TREE != NULL
Step 2: Write “TREE->DATA”
Step 3: PREORDER(TREE->LEFT)
Step 4: PREORDER(TREE->RIGHT)
[END OF WHILE]
Step 5: END
Pre-order Traversal
In-order Algorithm
INORDER(TREE)
Step 1: Repeat step 2 to 4 if TREE != NULL
Step 2: INORDER(TREE->LEFT)
Step 3: Write “TREE->DATA”
Step 4: INORDER(TREE->RIGHT)
[END OF WHILE]
Step 5: END
In-order Traversal
In-order by Projection
Post-order Algorithm

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

LEFT DATA RIGHT

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.

• Binary tree is a tree in which all nodes contain zero,


one or two children.

• The expression trees have been implemented as


binary trees mainly because binary trees allows you
to quickly find what you are looking for.
Expression Trees
Exp 1: (a-b)+(c*d)

Construct tree with In-order form, generate prefix and


postfix expression.
Tournament Trees

• In tournament of Chess or Cricket, n number of players


participate. To declare a winner among them, a couple of
matches are played.

• If there are 8 players then Round 1 will be having 4


matches and 4 winners.

• Within Round 2, 2 matches and 2 winners.

• At Round 3, 1 match and 1 winner as root node of our


tree.
Tournament Trees
Construct Tree from given In-order and
Preorder traversals
• Inorder sequence: D B E A F C
• Preorder sequence: A B D E C F

• In a Preorder sequence, leftmost element is the root of the


tree.

• So we know ‘A’ is root for given sequences.

• By searching ‘A’ in Inorder sequence, we can find out all


elements on left side of ‘A’ are in left subtree and elements
on right are in right subtree. So we know structure now.
Construct Tree
Inorder sequence: D B E A F C
Preorder sequence: A B D E C F

We recursively follow above steps and get the following tree.


Construct Tree
Root
Left Child
Left
child

• 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

Construct a binary tree from the traversals given below:


• Inorder: 15, 30, 35, 40, 45, 50, 60, 70, 72, 75, 77, 80
• Pre-order: 50, 30, 15, 40, 35, 45, 70, 60, 80, 75, 72, 77
Construct Tree

Construct a binary tree from the traversals given below:


• Inorder: 1, 10, 11, 12, 13, 14, 15, 17, 18, 21
• Postorder: 1, 11, 12, 10, 14, 18, 21, 17, 15, 13
Binary Search Tree
• A binary tree which confirms to the following properties is called a
binary search 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)

• Searching begins from root node.

• At every node we need to check value of current node with


the value we are searching for.
Search - BST
Create a BST with following elements.
45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81

Search for the following elements.


12, 40, 67
Search – BST
searchElement(TREE, VAL)
Step 1: IF TREE->DATA = VAL OR TREE = NULL then
Return TREE
ELSE
IF VAL < TREE->DATA
Return searchElement(TREE->LEFT, VAL)
ELSE
Return searchElement(TREE->RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: End
Insert - BST
• Create a BST with following elements.
45, 39, 56, 54, 78

• Insert following elements.


12, 55
Insert – BST
Insert(TREE, VAL)
Step 1: IF TREE = NULL, then
Allocate memory for TREE
SET TREE -> DATA = VAL
SET TREE -> LEFT = TREE -> RIGHT = NULL
ELSE
IF VAL < TREE -> DATA
Insert(TREE->LEFT, VAL)
ELSE
Insert(TREE->RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: End
Delete - BST
CASE 1: Delete a node that has no children
CASE 2: Delete a node that has one child
CASE 3: Delete a node that has two children
Delete – BST
CASE 1:
If we are trying to delete a leaf, there is no problem.
 We just delete it and the rest of the tree is exactly as it
was so it is still a BST.
 Example: Delete ‘8’
Delete – BST
CASE:2
• There is another simple situation: suppose the node
we're deleting has only one subtree.

• To delete a node with 1 subtree, we just ‘link past’ the


node, i.e. connect the parent of the node directly to the
node's only subtree.

• This always works, whether the one subtree is on the


left or on the right.
Delete – BST
CASE:2
•In the following example, ‘3’ has only 1 subtree.
•Deleting ‘3’ gives us:
Delete – BST CASE:3

• How to delete a node having two


subtrees. For example, how to delete
‘6’?

• The standard solution is based on this


idea: we leave the node containing ‘6’
exactly where it is
• but we get rid of the value 6 and find
another value to store in the ‘6’ node.
• This value is taken from a node below
the ‘6’s node, and it is that node that is
actually removed from the tree.
Delete – BST CASE:3

• Erase 6, but keep that node


Delete – BST CASE:3
• Now, what value can we move
into the vacated node and have a
binary search tree?

• Well, here's how to figure it out.


If we choose value X, then:
1) everything in the left subtree
must be smaller than X.
2) everything in the right subtree
must be bigger than X.
Delete – BST CASE:3
• Let's suppose we're going to get X
from the left subtree.

 2) is guaranteed because everything in


the left subtree is smaller than
everything in the right subtree.

 What about 1)? If X is coming from


the left subtree, 1) says that there is a
unique choice for X - we must choose
X to be the largest value in the left
subtree.
Delete – BST CASE:3

• In our example, 3 is the largest value in the left subtree.

• So if we put 3 in the vacated node and delete it from its


current position we will have a BST with 6 deleted.
Delete – BST CASE:3
So our general algorithm is: to
delete N,
• if it has two subtrees, replace the
value in N with the largest value
in its left subtree
• and then delete the node with the
largest value from its left
subtree.
Delete – BST CASE:3
• Note: The largest value in the left subtree will never
have two subtrees.

• Why?

• Because if it's the largest value it cannot have


right subtree.

• Finally, there is nothing special about the left subtree.


We could do the same thing with the right subtree: just
use the smallest value in the right subtree.
Delete – BST CASE:3
Other Solution

To delete N, if it has two subtrees:


Replace the value in N with the value in its inorder successor and then
delete the inorder successor node.
Height of a Tree

• Height of Left Subtree: 2

• Height of Right Subtree: 1

• 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

• Total nodes in BST = 4 + 3 + 1 = 8


Determine number of nodes

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

• Total internal nodes in BST = 2 + 1 + 1 = 4


Number of Internal Nodes
totalInternalNodes(TREE)
Step 1: IF TREE = NULL, then
Return 0
IF TREE -> LEFT = NULL and TREE -> RIGHT = NULL, then
Return 0
ELSE
Return totalInternalNodes(TREE->LEFT) +
totalInternalNodes(TREE->RIGHT) + 1
[END OF IF]
Step 2: End
Number of External Nodes
• Total external nodes of left sub-tree = 2
• Total external nodes of right sub-tree = 2

• Total external nodes in BST = 2 + 2 = 4


Number of External Nodes

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.

•Largest Node :11


Finding the largest Node

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?

• Insert: 2, 4, 6, 8, 10, 12 into an empty BST

• Problem: Lack of “balance”:


• compare depths of left and right subtree

• Unbalanced degenerated tree


Height Balanced Binary Trees

• A height-balanced binary tree is one for which at


every node, the absolute value of the difference in
heights of the left and right children is no larger than
one.

• Note: Every complete binary tree is height balanced.

• Named after their inventor Adelson, Velski &


Landis, AVL trees are height balancing binary
search tree.
Balance Factor
• AVL tree checks the height of the left and the right sub-trees and
assures that the difference is not more than 1. This difference is
called the Balance Factor.

• Balance Factor = height(left-subtree) − height(right-subtree)

• The balance factor (bf) of a height balanced binary tree may take on
one of the values -1, 0, +1.

• An AVL node is "leftheavy" when bf = 1, "equalheight" when bf =


0, and "rightheavy" when bf = -1
Balance Factor

 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

• If at any time they differ by more than one, rebalancing is done to


restore this property.

• Insertions and deletions may require the tree to be rebalanced by


one or more tree rotations.

• Store current heights in each node.


AVL Tree?
Search
• Search operation in AVL tree does not modify the
tree structure, no special provisions are required.
Insertion
• Rebalancing an AVL Tree

• An AVL tree whose root has zero balance factor cannot be


unbalanced by the insertion of a single node.

• A new node will be added as a leaf to one of the subtrees,


increasing that subtree's height by (at most) 1. Therefore, the
balance factor of the tree may change to +1 or -1, keeping the
tree balanced.
Insertion
• New node will be inserted as leaf node, so balance factor of it will
be always 0.

• Rotation is not required if balance factor of each node is +1, 0 or -1

 Case1: Tree was left- or right-heavy, new insertion makes it


balanced.

 Case2: Inserted node makes tree left- or right-heavy.

 Case3: Tree was left- or right-heavy, insertion makes it unbalanced.


Tree was left- or right-heavy, new
insertion makes it balanced.
Tree was left- or right-heavy,
insertion makes it unbalanced.
Imbalance Types
After an insertion, when the balance factor of node A is –2 or 2,
the node A is one of the following four imbalance types

1. LL: new node is in the left subtree of the left subtree of A


2. LR: new node is in the right subtree of the left subtree of A
3. RR: new node is in the right subtree of the right subtree of A
4. RL: new node is in the left subtree of the right subtree of A
AVL Rotations
To balance itself, an AVL tree may perform the following
four kinds of rotations −
1. Left rotation
2. Right rotation
3. Left-Right rotation
4. Right-Left rotation

• The first two (1 and 2) rotations are single rotations


• and the next two (3 and 4) rotations are double rotations.
• To have an unbalanced tree, we at least need a tree of height 2 or -2.
Left Rotation (single rotation)
AVL tree may become unbalanced, when a node is inserted into the
right subtree of the right subtree(RR), then we perform a single left
rotation.
The unbalanced node (A) becomes the left child of its right child
(B) by performing a left rotation.
Right Rotation (single rotation)
AVL tree may become unbalanced, if a node is inserted in the left
subtree of the left subtree (LL). The tree then needs a single right
rotation.
The unbalanced node (C) becomes the right child of its left child
(B) by performing a right rotation.
Left-Right Rotation (Double rotation)
A left-right rotation is a combination of left rotation
followed by right rotation.

• A node has been inserted into the right


subtree of the left subtree (LR).

• This makes C an unbalanced node.

• These scenarios cause AVL tree to


perform left-right rotation.
Left-Right Rotation (Double rotation)
• We first perform the left rotation on the left subtree of C. This
makes A, the left subtree of B.

• Node C is still unbalanced, however now, it is because of the left-


subtree of the left-subtree (LL).
Left-Right Rotation (Double rotation)
 We shall now right-rotate the tree, making B the new root
node of this subtree. C now becomes the right subtree of its own
left subtree.

 The tree is now balanced.


Right-Left Rotation (Double rotation)
It is a combination of right rotation followed by left
rotation.

A node has been inserted into the left


subtree of the right subtree (RL).

This makes A, an unbalanced node with


balance factor 2.

These scenarios cause AVL tree to


perform right-left rotation.
Right-Left Rotation (Double rotation)
 First, we perform the right rotation along C node, making C
the right subtree of its own left subtree B. Now, B becomes the
right subtree of A.
 Node A is still unbalanced because of the right subtree of its
right subtree (RR) and requires a left rotation.
Right-Left Rotation (Double rotation)
 A left rotation is performed by making B the new root node of
the subtree. A becomes the left subtree of its right subtree B.

 The tree is now balanced.


Imbalance Types
After an insertion, when the balance factor of node A is –2 or 2,
the node A is one of the following four imbalance types

1. LL: new node is in the left subtree of the left subtree of A


2. LR: new node is in the right subtree of the left subtree of A
3. RR: new node is in the right subtree of the right subtree of A
4. RL: new node is in the left subtree of the right subtree of A
LL
A is unbalanced
Perform single right rotation:
 A (unbalanced node) will become right child of B
 Right child of B (T2) will become left child of A

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

Next step: insert 1 -->


Insert 1 and recalculate balance factors Balance not ok
(Balance factor
of +2 is not
allowed)
+1 57

+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

Next step: insert 30 -->


Insert 30 and recalculate balance factors Balance ok

+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

Next step: Insert 32 -->


Insert 32 and recalculate balance factors 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
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

Next step: Insert 35 -->


Insert 35 and recalculate balance factors Balance not ok

+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

Start from first spot


-2 26 (from bottom of tree) -1 72
where balance factor is
incorrect.
0 3 +2 38 0 63 +1 94

0 1 0 25 -1 32 0 47 0 78
Left Right Case
0 30 +1 37

0 35

Next step: Rebalance (Step 1) -->


Rebalance (Step 1) 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

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

Next step: Finished! -->


Deletion

• Deletion of a node may also produce an imbalance

• Imbalance incurred by deletion is classified into


the types R0, R1, R-1, L0, L1, and L-1

• Rotation is also needed for rebalancing

• Read the observations after deleting a node from an


AVL search tree
R0 Rotation
B is the root node of left sub tree where BF of its is 0.
Example#1 Delete 72
R1 Rotation
B is the root node of left sub tree where BF of its is 1.
Example#2 Delete 72
R-1 Rotation (LR rotation)
B is the root node of left sub tree where BF of its is -1.
Example#3 Delete 72
Practice
•Insert 14, 17, 11, 7, 53, 4, 13, 12, 8 into an empty
AVL tree
•Now remove 53, 11, 8

•Build an AVL tree with the following values:


15, 20, 24, 10, 13, 7, 30, 36, 25
•Remove 24 and 20 from the AVL tree.
AVL Tree

• 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.

• In any binary tree linked list representation, there are


more number of NULL pointer than actual pointers.

• Generally, in any binary tree linked list representation,


if there are 2N number of reference fields, then N+1
number of reference fields are filled with NULL ( N+1
are NULL out of 2N ).
Threaded Binary Tree
• This NULL pointer does not play any role except
indicating there is no link (no child).

A. J. Perlis and C. Thornton have proposed new binary
tree called "Threaded Binary Tree", which make use
of NULL pointer to improve its traversal processes.

• In threaded binary tree, NULL pointers are replaced by


references to other nodes in the tree, called threads.
Threaded Binary Tree
• Threaded Binary Tree is also a binary tree in which

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.

• By doing this threading we avoid the recursive method


of traversing a Tree, which consumes a lot of memory
and time.
Let's make the Threaded Binary tree out
of a normal binary tree...
• The INORDER traversal for the below tree is -- D B A E
C. So, the respective Threaded Binary tree will be

• B has no right child and its Inorder successor is A and so a


thread has been made in between them. Similarly, for D
and E. C has no right child and it has no Inorder successor
even, so it has a hanging thread.
Threaded Binary Tree
Advantage
1. By doing threading we save a lot of memory and
time.
2. The node can keep record of its root .

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.

• M is called the degree of the tree. A binary search


tree, therefore, has degree 2.
M-way Search Tree (M=3)
M-way Search Tree
• In fact, it is not necessary for every node to contain
exactly (M-1) values and have exactly M subtrees.

• In an M-way subtree a node can have anywhere from 1


to (M-1) values, and the number of (non-empty)
subtrees.

• M is thus a fixed upper limit on how much data can be


stored in a node.
B-trees (Height Balanced m-way
Search Tree)
• In a binary search tree, AVL Tree, every node can have only one
value (key) and maximum of two children.

• But there is another type of search tree called B-Tree in which


•a node can store more than one value (key)
•and it can have more than two children.

• B-Tree was developed in the year of 1972 by Bayer and


McCreight with the name Height Balanced m-way Search Tree.
Later it was named as B-Tree.
B-trees
B-Tree is a self-balanced search tree with multiple
keys in every node and more than two children for
every node.

Every B-Tree has order.

Number of keys in a node and number of children for


a node depends on the order of the B-Tree.
B-trees
B-Tree of Order m has the following properties...

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.
B-trees
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.

Example: B-Tree of Order 4 contains maximum 3 key


values in a node and maximum 4 children for a node.
• All nodes except root must have at least ⌈m/2⌉ -
1 keys and maximum of m-1 keys.
B-trees of order 4 • Non leaf nodes except root must have at least
m/2 children and at most m children.
• If the root node is a non leaf node, then it must
have at least 2 children.
• A non leaf node with n-1 keys must
have n number of children.
• All nodes except root must have at least ⌈m/2⌉ -1 keys
and maximum of m-1 keys.
B-trees of order 4 • Non leaf nodes except root must have at least m/2
children and at most m children.
• If the root node is a non leaf node, then it must
have at least 2 children.
• A non leaf node with n-1 keys must have n number of
children.
B-trees
• B-tree is a specialized M-way tree widely used for
disk access.

• The B-tree is optimized for systems that read and


write large blocks of data. It is commonly used
in databases and file systems.
Search -- B-Tree
• In a B-Tree, the search operation is similar to that of
Binary Search Tree.

• In B-Tree also search process starts from the root node


but every time we make n-way decision where n is the
total number of children that node has.

• In a B-Tree, the search operation is performed


with O(log n) time complexity.
Search -- B-Tree
Step 1: Compare, the search element with first key value of root
node in the tree.
Step 2: If both are matching, then display "Given node
found!!!" and terminate the function.
Step 3: If search element is smaller, then continue the search
process in left subtree.
Step 4: If search element is larger, then compare with next key
value in the same node and repeat step 2, 3 and 4 until we found
exact match or comparison completed with last key value in a leaf
node.
Step 5: If we completed with last key value in a leaf node, then
display "Element is not found" and terminate the function.
B-trees of order 4
B-Tree
(Search an element)
Step 1: FOR i = 1 TO N-1
Step 2: IF TREE->K[i] = VAL OR TREE = NULL then
Return TREE
ELSE
IF VAL < TREE->K[i]
Return searchElement(TREE->CHILD[i], VAL)
ELSE IF i = N-1
Return searchElement(TREE->CHILD[i+1], VAL)
[END OF IF]
[END OF IF]
[END OF FOR LOOP]
Step 3: End
B-tree: Insert Operation
All insertions start at a leaf node. To insert a new element, search
the tree to find the leaf node where the new element should be
added. Insert the new element into that node with the following
steps:
• If the node contains fewer than the maximum legal number of
elements, then there is room for the new element. Insert the new
element in the node, keeping the node's elements ordered.
• Otherwise the node is full, evenly split it into two nodes so:
• A single median is chosen from among the leaf's elements and the
new element.
• Values less than the median are put in the new left node and
• Values greater than the median are put in the new right node, with
the median acting as a separation value.
B-tree: Insert Operation
Insert 8, 9, 39, 4
• Order: 5

• 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.

• The tree may be highly structured and therefore have 3


subtrees per node in which case it is called a ternary tree.

• However, it is often the case that the number of subtrees for


any node may be variable. Some nodes may have 1 or no
subtrees, others may have 3, some 4, or any other
combination.
Convert General T to Binary T
1) Root node of general tree becomes root node of Binary Tree.

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.

4) Now Node T2 becomes right child of Node T1, Node T3


becomes right child of Node T2 and so on in binary tree.

5) The same procedure of step 2 is repeated for each leftmost node


in the general tree.
Convert General T to Binary T
Application of Trees
• Manipulate hierarchical data: To store information that naturally
forms a hierarchy. For example, the file system on a computer.

• Make information easy to search: If we organize keys in form of a


tree (with some ordering e.g., BST), we can search for a given key in
moderate time (quicker than Linked List and slower than arrays).
Self-balancing search trees like AVL and Red-Black trees guarantee
an upper bound of O(Log n) for search.
Application of Trees
• Easy Insert/Delete: We can insert/delete keys in moderate time
(quicker than Arrays and slower than Unordered Linked Lists). Self-
balancing search trees like AVL and Red-Black trees guarantee an
upper bound of O(Log n) for insertion/deletion.

• 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

You might also like