Data Structure Unit 4
Data Structure Unit 4
Engineering
U20EST356
DATA STRUCTURES
Handling by
Mr.J.Muruganandham
Assistant Professor
Department of EEE
UNIT IV TREES
branche
s root
Computer Scientist’s View
root
leaves
branches
nodes
What is a Tree
• A tree is a finite nonempty
set of elements.
• It is an abstract model of a Computers”R”U
hierarchical structure. s
• consists of nodes with a
parent-child relation.
• Applications: Sale Manufacturin R&
– Organization charts s g D
– File systems
– Programming environments
U Internationa Laptop Desktop
S l s s
I J K
subtree
Forest
Tree Properties
Property Value
A Number of nodes
Height
Root Node
B C Leaves
Interior nodes
Ancestors of H
D E F Descendants of B
Siblings of E
Right subtree of A
Degree of this tree
G
H I
Binary and non binary tree
Non-Binary tree
Binary tree
• A full binary tree (sometimes proper binary
tree or 2-tree) is a tree in which every node
other than the leaves has two children.
b c
a b c
Preorder Example (Visit = print)
a
b c
f
d e
g h i j
a b d g h e i c f j
Preorder Of Expression Tree
/
* +
e f
+ -
a b c d
/ * + a b - c d + e f
b c
b a c
Inorder Example (Visit = print)
a
b c
f
d e
g h i j
g d h b e i a f j c
Inorder By Projection (Squishing)
a
b c
f
d e
g h i j
g d h b e i a f j c
Inorder Of Expression Tree
/
* +
e f
+ -
a b c d
a + b * c - d / e + f
b c
b c a
Postorder Example (Visit = print)
a
b c
f
d e
g h i j
g h d i e b j f c a
Postorder Of Expression Tree
/
* +
e f
+ -
a b c d
a b + c d - * e f + /
b c
f
d e
g h i j
• Make a clone.
• Determine height.
•Determine number of nodes.
Level Order
Let ptr be a pointer to the tree root.
while (ptr != NULL)
{
visit node pointed at by ptr and put its children
on a FIFO queue;
if FIFO queue is empty, set ptr = NULL;
otherwise, delete a node from the FIFO queue
and call it ptr;
}
Level-Order Example (Visit = print)
a
b c
f
d e
g h i j
a b c d e f g h i j
Conversion of General Tree in to Binary Tree
Threaded Binary Trees
Given a binary tree with n nodes,
the total number of links in the tree is 2n.
Each node (except the root) has exactly one incoming arc
only n - 1 links point to nodes
remaining n + 1 links are null.
One can use these null links to simplify some traversal processes.
E K
B F J L
A D G I M
The first node visited in an inorder traversal is the leftmost leaf, node A.
Since A has no right child, the next node visited in this traversal is its
parent, node B.
E K
B F J L
A D G I M
E K
B F J L
A D G I M
C
Threaded Binary Trees
Node D has a null right pointer which can be replaced with a pointer to D’s
inorder successor, node E:
E K
B F J L
A D G I M
C
Threaded Binary Trees
The next node visited with a null right pointer is G. Replace the null pointer
with the address of G’s inorder successor: H
E K
B F J L
A D G I M
C
Threaded Binary Trees
Finally, we replace:
first, the null right pointer of I with a pointer to its parent
and then, likewise, the null right pointer of J with a pointer to its parent
E K
B F J L
A D G I M
C
Threaded Tree Example
6
3 8
1 5 7 11
9 13
Threaded Tree Traversal
• We start at the leftmost node in the tree, print
it, and follow its right thread
• If we follow a thread to the right, we output
the node and continue to its right
• If we follow a link to the right, we go to the
leftmost node, print it, and continue
Threaded Tree Traversal
Output
6 1
3 8
1 5 7 11
9 13
Start at leftmost node, print it
Threaded Tree Traversal
Output
6 1
3
3 8
1 5 7 11
9 13
Follow thread to right, print node
Threaded Tree Traversal
Output
6 1
3
3 8 5
1 5 7 11
9 13
Follow link to right, go to leftmost
node and print
Threaded Tree Traversal
Output
6 1
3
3 8 5
6
1 5 7 11
9 13
Follow thread to right, print node
Threaded Tree Traversal
Output
6 1
3
3 8 5
6
7
1 5 7 11
9 13
Follow link to right, go to
leftmost node and print
Threaded Tree Traversal
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9 13
Follow thread to right, print node
Threaded Tree Traversal
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9
9 13
Follow link to right, go to
leftmost node and print
Threaded Tree Traversal
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9
11
9 13
Follow thread to right, print node
Threaded Tree Traversal
Output
6 1
3
3 8 5
6
7
1 5 7 11 8
9
11
9 13 13
2
2 5
3
1 3
4
4 Is this “balanced”?
5
2 6
6
1 3 5 7 7
Approaches to balancing trees
• Don't balance
› May end up with some nodes very deep
• Strict balance
› The tree must always be balanced perfectly
• Pretty good balance
› Only allow a little out of balance
• Adjust on access
› Self-adjusting
Balancing Binary Search Trees
• Many algorithms exist for keeping binary
search trees balanced
› Adelson-Velskii and Landis (AVL) trees (height-
balanced trees)
› Splay trees and other self-adjusting trees
› B-trees and other multiway search trees
Perfect Balance
• Want a complete tree after every operation
› tree is full except possibly in the lower right
• This is expensive
› For example, insert 2 in the tree on the left and
then rebuild as a complete tree
6 5
Insert 2 &
4 9 2 8
complete tree
1 5 8 1 4 6 9
AVL - Good but not Perfect
Balance
• AVL trees are height-balanced binary search
trees
• Balance factor of a node
› height(left subtree) - height(right subtree)
• 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
› Store current heights in each node
Height of an AVL Tree
• N(h) = minimum number of nodes in an AVL
tree of height h.
• Basis
› N(0) = 1, N(1) = 2
• Induction h
height of node = h
balance factor = hleft-hright
empty height = -1
Node Heights after Insert 7
Tree A (AVL) Tree B (not AVL)
balance factor
2 3 1-(-1) = 2
6 6
1 1 1 2
4 9 4 9
0 0 0 0 0 1 -1
1 5 7 1 5 8
0
7
height of node = h
balance factor = hleft-hright
empty height = -1
Insert and Rotation in AVL Trees
• Insert operation may cause balance factor to
become 2 or –2 for some node
› only nodes on the path from insertion point to
root node have possibly changed in height
› So after the Insert, go back up to the root node by
node, updating heights
› If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
Single Rotation in an AVL Tree
2 2
6 6
1 2 1 1
4 9 4 8
0 0 1 0 0 0 0
1 5 8 1 5 7 9
0
7
Insertions in AVL Trees
Let the node that needs rebalancing be .
k h
h
h
Z
X Y
AVL Insertion: Outside Case
j Inserting into X
destroys the AVL
property at node j
k h
h+1 h Z
Y
X
AVL Insertion: Outside Case
j Do a “right rotation”
k h
h+1 h Z
Y
X
Single right rotation
j Do a “right rotation”
k h
h+1 h Z
Y
X
Outside Case Completed
“Right rotation” done!
h+1
j
h h
X Y Z
AVL property has been restored!
AVL Insertion: Inside Case
Consider a valid
AVL subtree j
k h
h h Z
X Y
AVL Insertion: Inside Case
Inserting into Y
destroys the
AVL property
j Does “right rotation”
restore balance?
at node j
k h
h h+1 Z
X
Y
AVL Insertion: Inside Case
k
“Right rotation”
does not restore
balance… now k is
j
out of balance
h
X h+1
h
Z
Y
AVL Insertion: Inside Case
Consider the structure
of subtree Y… j
k h
h h+1 Z
X
Y
AVL Insertion: Inside Case
Y = node i and
subtrees V and W j
k h
h
i h+1 Z
X h or h-1
V W
AVL Insertion: Inside Case
j We will do a left-right
“double rotation” . . .
k
i Z
X
V W
Double rotation : first rotation
j left rotation complete
i
k Z
W
X V
Double rotation : second
rotation
j Now do a right rotation
i
k Z
W
X V
Double rotation : second
rotation
right rotation complete
i restored
k j
h h
h or h-1
X V W Z
Implementation
balance (1,0,-1)
key
left right
V W
Insertion in AVL Trees
• Insert at the leaf (as for all BST)
› only nodes on the path from insertion point to
root node have possibly changed in height
› So after the Insert, go back up to the root node by
node, updating heights
› If a new balance factor (the difference hleft-hright) is
2 or –2, adjust tree by rotation around the node
Insert in BST
Insert(T : reference tree pointer, x : element) : integer {
if T = null then
T := new tree; T.data := x; return 1;//the links to
0 45
Now Insert 34
Double rotation (inside case)
3
3
20 20
1 3 1 2
10 30 10 35
0 0 2
0 1
5 Imbalance 25 40 5 30 40 1
0
1 35 0 25 34 45
45 0
Insertion of 34 0
34
AVL Tree Deletion
• Similar but more complex than insertion
› Rotations and double rotations needed to
rebalance
› Imbalance may propagate upward so that
many rotations may be needed.
Pros and Cons of AVL Trees
Arguments for AVL trees:
V W
B-Trees
Considerations for disk-based storage
systems.
Indexed Sequential Access Method
(ISAM)
m-way search trees
B-trees
Data Layout on Disk
• Track: one ring
• Sector: one pie-shaped piece.
• Block: intersection of a track and a sector.
Disk Block Access Time
Seek time = maximum of
Time for the disk head to move to the correct track.
Time for the beginning of the correct sector to spin round
to the head. (Some authors use “latency” as the term for this component,
or they use latency to refer to all of what we are calling seek time.)
Transfer time =
Time to read or write the data.
(Approximately the time for the sector to spin by the head).
12
2 3 8 13 27
12
2 3 8 10 13 27
We find the location for 10 by following a path from the root using the stored
key values to guide the search.
The search falls out the tree at the 4th child of the 1st child of the root.
The 1st child of the root has room for the new element, so we store it there.
Insert 11
12
2 3 8 10 11 13 27
We fall out of the tree at the child to the right of key 10.
But there is no more room in the left child of the root to hold 11.
Therefore, we must split this node...
Insert 11 (Continued)
8 12
2 3 10 11 13 27
The m + 1 children are divided evenly between the old and new nodes.
The parent gets one new child. (If the parent become overfull, then it, too, will
have to be split).
Remove 8
8 12
2 3 10 11 13 27
Removing 8 might force us to move another key up from one of the children. It
could either be the 3 from the 1st child or the 10 from the second child.
However, neither child has more than the minimum number of children (3), so
the two nodes will have to be merged. Nothing moves up.
Remove 8 (Continued)
12
2 3 10 11 13 27
The root contains one fewer key, and has one fewer child.
Remove 13
12
2 3 10 11 13 27
11
2 3 10 12 27
11
2 3 10 12 27
10
2 3 12 27
Remove 2
10
2 3 12 27
3 10 12 27
The result is illegal, because the root does not have at least 2 children.
Therefore, we must remove the root, making its child the new root.
Remove 2 (Cont)
3 10 12 27
3 10 12 27
3 10 12 27 49
Adding this key make the node overfull, so it must be split into two.
But this node was the root.
So we must construct a new root, and make these its children.
Insert 49 (Cont)
12
3 10 27 49
j a0 k1 a1 k2 a2 … kj aj
5
16 30
1 3 5 6 9 16 17 30 40
index node
leaf/data node
B+-tree—Search
5
16 30
1 3 5 6 9 16 17 30 40
key = 5
5
16 30
1 5 6 9 16 17 30 40
Insert 10
Insert
9
5
16 30
1 3 5 6 9 16 17 30 40
1 23
Insert
9
5 2
16 30
2 3
1 5 6 9 16 17 30 40
2 5 16 30
1 2 3 5 6 9 16 17 30 40
17
2 5 16 30 17 18
1 2 3 5 6 9 16 30 40
2 5 16 30
1 2 3 5 6 9 16 17 18 30 40
2 5 16 30
1 2 3 5 6 9 16 17 18 30 40
2 5 16 30
1 2 3 5 6 9 16 17 30 40
2 5 16 30
1 2 3 5 6 9 17 30 40
2 5 16 30
1 2 3 5 6 9 17 30 40
3 5 16 30
2 3 5 6 9 17 30 40
3 5 16 30
2 3 5 6 9 17 30 40
5 16 30
3 5 6 9 17 30 40
6 16 30
5 6 9 17 30 40
6 30
17 30 40
5 6
Delete
9
6 16 30
5 6 9 17 30 40
16 30
5 9 17 30 40
9 30
5 9 17 30 40
• Delete 9.
• Merge with sibling, delete in-between key in parent.
Delete
16
30
5 17 30 40
16 30
5 17 30 40