Analysis of Algorithms Trees-I Prof. Muhammad Saeed
Tree Representation.. Analysis Of Algorithms 2
.. Tree Representation Analysis Of Algorithms 3
Nomenclature Nodes (13) Size (13) Degree of a node Depth of a tree (3) Height of a tree (3) Level of a node Leaf (terminal) Nonterminal Parent Children Sibling Ancestor K E Degree Level B L F 3 C G A H I J M D 2 2 1 2 3 2 1 3 0 3 0 3 1 3 0 3 0 3 0 4 0 4 0 4 1 Level 1 2 3 4 Analysis Of Algorithms 4
Types Binary Tree Binary Search Tree B-Tree AVL Tree Red-Black Tree Splay Tree Binomial Tree Analysis Of Algorithms 5
A forest is a set of n >= 0 disjoint trees A Forest A E G B E B C D F H I C F G D H I Analysis Of Algorithms 6
Complete binary tree Full binary tree of depth 4 1 1 2 3 2 3 4 5 6 7 4 5 6 7 8 9 8 9 10 11 12 13 14 15 Analysis Of Algorithms 7
Binary Tree Traversal 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. Analysis Of Algorithms 8
Arithmetic Expression Using Binary Tree A / * B * C + D E inorder traversal A / B * C * D + E infix expression preorder traversal + * * / A B C D E prefix expression postorder traversal A B / C * D * E + postfix expression level order traversal + * E * D / C A B Analysis Of Algorithms 9
Heaps Property: The root of max heap (min heap) contains the largest (smallest). [1] 14 [1] 9 [1] 30 [2] [3] 12 7 [2] [3] 6 3 [2] 25 [4] [5] [6] 10 8 6 [4] 5 Analysis Of Algorithms 10
Priority queue representations Representation Insertion Deletion Unordered array (1) (n) Unordered linked list (1) (n) Sorted array O(n) (1) Sorted linked O(n) list (1) Max heap O(log 2 n) O(log 2 n) Analysis Of Algorithms 11
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]. 56 26 200 18 28 190 213 12 24 27 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. Analysis Of Algorithms 12
.. Binary Search Tree - Best Time.. All BST operations are O(d), where d is tree depth minimum d is d=log 2 N 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) Analysis Of Algorithms 13
..Binary Search Tree - Worst Time.. 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 14
Balanced and unbalanced BST 1 4 4 2 3 4 5 2 5 1 3 Is this balanced? 2 6 6 1 3 5 7 7 Analysis Of Algorithms 15
Rotations: Single Rotation.. Analysis Of Algorithms 16
Analysis Of Algorithms 17 0 1 0 2 0 6 4 9 8 1 5 1 0 7 2 1 0 2 0 6 4 9 8 1 5 1 0 7 Tree..Rotations: Single Rotation.. j k X Y Z h h+1 h j k X Y Z h h+1 h
. Rotations.. Analysis Of Algorithms 18
Analysis Of Algorithms 19
Analysis Of Algorithms 20
AVL(Adelson-Velskii-Landis) trees 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. 2 44 4 3 17 78 1 2 32 50 1 48 62 1 88 An example of an AVL tree where the heights are shown next to the nodes: 1 Analysis Of Algorithms 21
Height of an AVL 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) = 2 For 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 22
. Height of an AVL Tree 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) > 2 i n(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) +2 Thus the height of an AVL tree is O(log n) Analysis Of Algorithms 23
Rotation: Double rotation (inside case).. 0 5 1 3 20 0 3 10 30 Imbalance Insertion of 34 25 0 2 40 5 1 20 10 35 0 1 30 1 35 45 0 0 25 34 34 3 2 40 1 0 45 Analysis Of Algorithms 24
..Rotations.. Double rotation j X k i Z V W Analysis Of Algorithms 25
..Rotations A Double or Single X B C Z V W Analysis Of Algorithms 26
Analysis Of Algorithms 27
Red and Black Trees A Red Black tree is a binary search tree that inserts and deletes in such a way that the tree is always reasonably balanced. Every node is red or black The root is black Every leaf is NIL and is black If a node is red, then both its children are black For each node, all paths from the node to descendant leaves contain the same number of black nodes. Analysis Of Algorithms 28
A Red and Black Tree with n internal nodes has height at most 2log(n+1). Analysis Of Algorithms 29
G X P U P G X U Case 1 U is Red Just recolor and move up Analysis Of Algorithms 30
G S P X U X P G Case 2 Zig-Zag Double rotate X around P and X around G Recolor G and X S U Analysis Of Algorithms 31
G X P S U X P G Case 3 Zig-Zig Single Rotate P around G Recolor P and G S U Analysis Of Algorithms 32
Insert 4 into this R-B Tree 2 1 7 11 14 15 5 8 Analysis Of Algorithms 33
Red Black trees offer worst-case guarantees for insertion time, deletion time, and search time. 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). 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. Completely Fair Scheduler used in current Linux kernels uses Red Black trees. Analysis Of Algorithms 34
End Trees I