Advanced Operating Systems Prof. Muhammad Saeed File Systems-I.
Trees-I Prof. Muhammad Saeed Analysis of Algorithms.
-
Upload
colin-walton -
Category
Documents
-
view
277 -
download
0
Transcript of Trees-I Prof. Muhammad Saeed Analysis of Algorithms.
Trees-I
Prof. Muhammad Saeed
Analysis of Algorithms
Analysis Of Algorithms 2
Tree Representation …..
Tree
Analysis Of Algorithms 3
Tree
….. Tree Representation
Analysis Of Algorithms 4
Nodes (13)Size (13)Degree of a nodeDepth of a tree (3)Height of a tree (3)Level of a nodeLeaf (terminal)NonterminalParentChildrenSiblingAncestor
K L
E F
B
G
C
M
H I J
D
A
Level
1
2
3
4
3
2 1 3
1 0 0 1 0 0
0 0 0
1
2 2 2
3 3 3 3 3 3
4 4 4
Degree Level
Nomenclature
Tree
Analysis Of Algorithms 5
Tree
Types
Binary TreeBinary Search TreeB-TreeAVL TreeRed-Black TreeSplay TreeBinomial Tree
Analysis Of Algorithms 6
Tree
A forest is a set of n >= 0 disjoint trees
A E G
B C D F H I G
H
I
A
B
C
D
F
E
Forest
Analysis Of Algorithms
1
2 3
75
9
4
8
6
1
2 3
75
11
4
10
6
98 15141312
Complete binary tree Full binary tree of depth 4
Tree
7
Analysis Of Algorithms 8
Tree
A binary tree can be traversed using four different algorithms 1.Pre-order: Root-Left-Right, It employs
Depth First Search.
2. Inorder: Left-Root-Right.
3. Post-order: Left-Right-Root
4. Level-by-level.
Binary Tree Traversal
Analysis Of Algorithms 9
inorder traversalA / B * C * D + Einfix expressionpreorder traversal+ * * / A B C D Eprefix expressionpostorder traversalA B / C * D * E +postfix expressionlevel order traversal+ * E * D / C A B
+
*
A
*
/
E
D
C
B
Arithmetic Expression Using Binary Tree
Tree
Analysis Of Algorithms 10
Tree
Property:The root of max heap (min heap) contains the largest (smallest).
Heaps
Analysis Of Algorithms 11
Tree
Priority queue representations
Representation Insertion Deletion
Unordered array
(1) (n)
Unordered linked list
(1) (n)
Sorted array O(n) (1) Sorted linked list
O(n) (1)
Max heap O(log2n) O(log2n)
Analysis Of Algorithms 12
56
26 200
18 28 190 213
12 24 27
Binary Search Tree …..
Stored keys must satisfy the binary search tree property.if y is in left subtree of x,
then key[y] key[x].If y is in right subtree of x,
then key[y] key[x].
The binary-search-tree property guarantees that: The minimum is located at the left-most node. The maximum is located at the right-most
node.
Tree
Analysis Of Algorithms 13
Tree
All BST operations are O(d), where d is tree depthminimum d is d=log2N for a binary tree with N nodes What is the best case tree? What is the worst case tree?
So, best case running time of BST operations is O(log N)
….. Binary Search Tree - Best Time …..
Analysis Of Algorithms 14
…..Binary Search Tree - Worst Time …..
Tree
Worst case running time is O(N) What happens when you Insert elements in ascending
order?• Insert: 2, 4, 6, 8, 10, 12 into an empty BST
Problem: Lack of “balance”: • compare depths of left and right subtree
Unbalanced degenerate tree
Analysis Of Algorithms 15
Balanced and unbalanced BST
Tree
4
2 5
1 3
1
5
2
4
3
7
6
4
2 6
5 71 3
Is this “balanced”?
Analysis Of Algorithms 16
Tree
Rotations: Single Rotation …..
Analysis Of Algorithms 17
0
1
0
2
0
6
4
9
8
1 5
1
0
7
2
10
2
0
6
4 9
81 5
1
0
7
Tree
…..Rotations: Single Rotation …..
j
k
XY
Zh
h+1 h
j
k
XY
Zh
h+1 h
Analysis Of Algorithms 18
Tree
……. Rotations …..
Analysis Of Algorithms 19
Tree
Tree
Analysis Of Algorithms 20
Analysis Of Algorithms 21
AVL trees are balanced.An AVL Tree is a binary
search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1.
88
44
17 78
32 50
48 62
2
4
1
1
2
3
1
188
44
17 78
32 50
48 62
2
4
1
1
2
3
1
1
An example of an AVL tree where the heights are shown next to the nodes:
AVL(Adelson-Velskii-Landis) trees
TreeTree
Analysis Of Algorithms 22
Height of an AVL Tree …..
Tree
Proposition: The height of an AVL tree T storing n keys is O(log n).Justification: The easiest way to approach this problem is to find n(h): the minimum number of internal nodes of an AVL tree of height h.We see that base case is n(0) = 1 and n(1) = 2For n ≥ 3, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and the other AVL subtree of height n-2.i.e. n(h) = 1 + n(h-1) + n(h-2)
Analysis Of Algorithms 23
Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2)n(h) > 2n(h-2)n(h) > 4n(h-4)n(h) > 8n(h-6)…n(h) > 2in(h-2i)For any integer I such that h-2i 1
Solving the base case we get: n(h) ≥ 2 h/2-1
Taking logarithms: h < 2log n(h) +2Thus the height of an AVL tree is O(log n)
………. Height of an AVL Tree
Tree
Analysis Of Algorithms 24
Rotation: Double rotation (inside case) …..
3
0
3
20
10 30
25
1
40
2
50
20
10 35
30
1
405
45
0 1
2
3
Imbalance
45
0
1
Insertion of 34
35
34
0
0
1 25 340
Tree
Analysis Of Algorithms 25
Tree
…..Rotations …..Double rotation
j
k
X
V
Z
W
i
Analysis Of Algorithms 26
…..Rotations
Tree
Double or Single
Tree
Analysis Of Algorithms 27
Analysis Of Algorithms 28
Red and Black Trees
Tree
Every node is red or black The root is blackEvery leaf is NIL and is blackIf a node is red, then both its children are blackFor each node, all paths from the node to descendant leaves contain the same number of black nodes.
A Red–Black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced.
Analysis Of Algorithms 29
A Red and Black Tree with n internal nodes has height at most 2log(n+1).
Tree
Analysis Of Algorithms 30
Case 1 – U is Red
Just recolor and move up
X
P
G
U
PG
U
X
Tree
Analysis Of Algorithms 31
XP
G
U
S X
P G
S UCase 2 – Zig-Zag
Double rotate X around P and X around G
Recolor G and X
Tree
Analysis Of Algorithms 32
Tree
X
P
G
US P
X G
S UCase 3 – Zig-Zig
Single Rotate P around G
Recolor P and G
Analysis Of Algorithms 33
Tree
11
14
152
1 7
5 8
Insert 4 into this R-B Tree
Analysis Of Algorithms 34
Red–Black trees offer worst-case guarantees for insertion time, deletion time, and search time.
Tree
Completely Fair Scheduler used in current Linux kernels uses Red–Black trees.
Red–Black trees are also particularly valuable in functional programming, where they are one of the most common persistent data structures, used to construct associative arrays and sets which can retain previous versions after mutations.
The persistent version of Red–Black trees requires O(log n) worst-case for each insertion or deletion, in addition to time whereas oher BST’s require O(n).