Beruflich Dokumente
Kultur Dokumente
Trees
Trees
Tree: Section 4.1 (Weiss)
ormal DeIinition
Tree is a sequence oI nodes.
There is a starting node known as root node.
Every node other than the root has a parent node.
Nodes may have any number oI children.
A has 3 children, B, C, D
A is parent oI B
A
B C D
E
!ath to a node p is a sequence oI nodes root,n
1
, n
2
, ...p such
that n
1
is a child oI root, n
2
is a child oI n
1
..
!ath to E is ?
How many paths are there to one node?
Depth oI a node is the length oI the unique path
Irom the root to the node (not counting the node).
Root is at depth 0
Depth oI E is ?
A,B, E
Just one!
2
Leaves are nodes without any children.
D and E are leaI nodes.
Height oI a nonleaI node is the length oI the
LONGEST path Irom the node to a leaI(not
counting the leaI).
Height oI a leaI is 0
Height oI A is ?
2
Application
Organization oI a Iile system.
Root directory
EE220, CSE260, TCOM 500
EE220: Lecture Notes, HW
Every node has one or more elements:
Directory example: element oI a node is the
name oI the corresponding directory
Root
EE220 CSE260 TCOM500
Lec
HW
mplementation
Using pointers
A node has a pointer to all its children
Since a node may have many children, the child pointers
have a linked list.
A has a pointer to B, C, D each.
B has pointers to E and its other child.
E does not have any pointers.
A
B C D
E
Addition oI a child:
Create the new child node
Add a pointer to this child in the link list oI
its parent.
A
B C D
E
print(node-~name, depth)
Ior each child C oI node
Listdepth(C,depth1)
}
Root
EE220 CSE260 TCOM500
Lec
HW
!rint sequence:
Root, 0 EE220 1 Lec 2, HW 2,
CSE260 1, Lec 2, TCOM500 1
Lec
!ostorder Example
Need to Iind the size oI each directory in the unix
Iile system
Dirsize(node)
iI (node.leItchild NULL)
insert the new node at node.leItchild;
else nsert(node.leItchild, element);
}
Else I (element ~ node.key)
?????????? }
} Complexity: O(d)
5
3 8
1
4 10
nsert 3.5
Sequence Traveled:
5, 3, 4
nsert 3.5 as leIt child oI 4
5
3 8
1
4 10
3.5
DELETON
When we delete a node, we need to consider how we
take care oI the children oI the deleted nodes.
This has to be done such that the property oI the
search tree is maintained.
Any problem iI the node has no child?
Any problem iI the node has only one child?
No, simply delete the
node!
No, simply replace the
node with its child
Suppose, the node has two children.
Look at the right subtree oI the node (subtree rooted
at the right child oI the node).
ind the Minimum there.
Replace the key oI the node to be deleted by the
minimum element.
Delete the minimum element.
Any problem deleting it?
or deletion convenience, always
have a pointer Irom a node to its
parent.
Need to take care oI the
children oI this min.
element,
but the min element can
at most one child;
!seudo Code
I a node is childless, then
node-~parent-~ptrtonode NULL
Iree node;
}
I a node has one child
node-~parent-~child node-~child;
Iree node;
}
Delete(node)
I a node has 2 children,
minnode Iindmin(rightsubtree)-~key;
node-~key minnode-~key;
delete(minnode);
}
}
Complexity?
O(d)
5
3 8
1
4 10
3.5
Delete 3;
3 has 2 children;
indmin right subtree
Ior 3 returns 3.5
So 3 is replaced by 3.5, and
3.5 is deleted.
5
3.5 8
1
4 10
Lazy Deletion
Don`t physically delete the node, but mark it as
``junk.``
Any problem?
ncreases the depth oI the tree.
However, iI the number oI deleted nodes is oI
the same order as the number oI existing
nodes, then lazy deletion does not alter the
order oI the depth signiIicantly.
AVL Trees
We have seen that all operations depend on the depth oI
the tree.
We don`t want trees with nodes which have large height
This can be attained iI both subtrees oI each node have
roughly the same height.
AVL tree is a binary search tree where the height oI the
two subtrees oI a node diIIers by at most one
Height oI a null tree is -1
5
3 8
1
4 10
5
3
1
4
AVL Tree
Not AVL Tree
Section 4.4, Weiss
Suppose an AVL tree oI height h contains
contains at most S(h) nodes:
S(h) L(h) R(h) 1
L(h) is the number oI nodes in leIt subtree
R(h) is the number oI nodes in right subtree
You have larger number oI nodes iI there is larger
imbalance between the subtrees
This happens iI one subtree has height h-1, another h-2
Thus, S(h) S(h-1) S(h-2) 1
Using this you can show that h O(log N)
Rings a bell! ibonacci numbers
N
S(h)
h
h is O(log N)
Operations in AVL Tree
Searching, Complexity?
indMin, Complexity?
Deletion? nsertion?
O(log N)
O(log N)
nsertion
Search Ior the element
I it is not there, insert it in its place.
Any problem?
nsertion may imbalance the tree. Heights oI two
children oI a node may diIIer by 2 aIter an
insertion.
Tree Rotations used to restore the balance.
I an insertion cause an imbalance, which nodes can be
aIIected?
Nodes on the path oI the inserted node.
Let U be the node nearest to the inserted one which has an imbalance.
insertion in the leIt subtree oI the leIt child oI U
insertion in the right subtree oI the leIt child oI U
insertion in the leIt subtree oI the right child oI U
insertion in the right subtree oI the right child oI U
nsertion in leIt child oI leIt
subtree
Single Rotation
U
V
X
Y
Z
V
U
X
Y
Z
BeIore Rotation
AIter Rotation
5
3
1
4
nsert 0.8
AVL Tree
8
0.8
5
3
1
4
8
U
V
X
Y
Z
3
5
1
0.8
4
8
AIter Rotation
Double Rotation
Suppose, imbalance is due to an insertion in the leIt subtree oI
right child
Single Rotation does not work!
U
V
A
D
W
B C
W
V
U
A D B
C
BeIore Rotation AIter Rotation
5
3
1
4
nsert 3.5
AVL Tree
8
3.5
5
3
1
4
8
4
5
0.8
3
3.5
AIter Rotation
U
V
A W
B
D
8
!seudocode
nsert(X, T)
I (T NULL)
insert X at T; T-~height 0;
I (XT.element)
nsert(X, T -~leIt)
I Height(T -~leIt) - Height(T -~right) 2
!seudocode
Singlerotate routine in ig 4.41 (Weiss)
Separate Ior leIt and right nodes
Doublerotate routine in ig 4.43 (Weiss)
Separate Ior leIt and right nodes
I (X T.leItchild.element) T singleroratewithleIt(T);
else T doubleroratewithleIt(T);
} }
Else I (X~T.element)
nsert(X, T -~right)
I Height(T -~right) - Height(T -~leItt) 2
I (X ~ T.righchild.element) T singleroratewithright(T);
else T doubleroratewithright(T);
} }
T-~height max(height(T-~leIt), height(T-~right)) 1;
Return(T);
EXTENDED EXAM!LE
Example in book
Extended Example
nsert 3,2,1,4,5,6,7, 16,15,14
3
ig 1
3
2
ig 2
3
2
1
ig 3
2
1
3
ig 4
2
1
3
4
ig 5
2
1
3
4
5
ig 6
2
1
4
5
3
ig 7
6
2
1
4
5
3
ig 8
4
2
5
6
1 3
ig 9
4
2
5
6
1 3
7
ig 10
4
2
6
7
1 3
5 ig 11
4
2
6
7
1 3
5
16
ig 12
4
2
6
7
1 3
5
16
15
ig 13
4
2
6
15
1 3
5
16
7
ig 14
5
4
2
7
15
1 3
6
16 14
ig 16
4
2
6
15
1 3
5
16
7
14
ig 15
Continued in Book
Deletions can be done with similar rotations