350 likes | 613 Views
Analysis of Algorithms. Trees-I. Prof. Muhammad Saeed. Tree. Tree Representation …. Tree. ….. Tree Representation. Tree. 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
E N D
Analysis of Algorithms Trees-I Prof. Muhammad Saeed
Tree Tree Representation ….. Analysis Of Algorithms
Tree ….. Tree Representation Analysis Of Algorithms
Tree Nomenclature Nodes (13) Size (13) Degree of a node Depthof a tree (3) Height of a tree (3) Level of a node Leaf (terminal) Nonterminal Parent Children Sibling Ancestor Level 1 2 3 4 Degree Level 3 1 2 2 1 2 3 2 1 3 3 0 3 0 1 0 3 0 3 3 0 0 4 4 0 4 Analysis Of Algorithms
Tree Types • Binary Tree • Binary Search Tree • B-Tree • AVL Tree • Red-Black Tree • Splay Tree • Binomial Tree Analysis Of Algorithms
Tree A forest is a set of n >= 0 disjoint trees A Forest G E A B E I H F G F D C C B H D I Analysis Of Algorithms
Tree Complete binary tree Full binary tree of depth 4 1 1 2 2 3 3 7 4 6 5 5 7 6 4 13 14 12 15 10 9 11 8 8 9 Analysis Of Algorithms
Tree Binary Tree Traversal • A binary tree can be traversed using four different algorithms • 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
+ * E * D C / B A Tree Arithmetic Expression Using Binary Tree 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
[1] [1] 9 [1] 30 14 [2] [2] [3] [2] 3 6 [3] 7 25 12 [6] [5] [4] 5 10 8 6 Tree Heaps Property: The root of max heap (min heap) contains the largest (smallest). [4] Analysis Of Algorithms
Tree Priority queue representations Analysis Of Algorithms
26 200 28 190 213 18 12 24 27 Tree Binary Search Tree ….. 56 • 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. Analysis Of Algorithms
Tree ….. Binary Search Tree - Best Time ….. • All BST operations are O(d), where d is tree depth • minimum d is d=log2Nfor 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
Tree …..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
Tree Balanced and unbalanced BST 1 4 2 2 5 3 1 3 4 4 Is this “balanced”? 5 2 6 6 1 3 5 7 7 Analysis Of Algorithms
Tree Rotations: Single Rotation ….. Analysis Of Algorithms
Tree …..Rotations: Single Rotation ….. j j 2 2 6 6 1 2 1 1 k k 4 9 4 8 h h 0 0 0 0 1 0 0 Z Z 7 9 1 5 8 1 5 h+1 h+1 h h 0 Y Y 7 X X Analysis Of Algorithms
Tree ……. Rotations ….. Analysis Of Algorithms
Tree Analysis Of Algorithms
Tree AVL Trees Analysis Of Algorithms
Tree 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. An example of an AVL tree where the heights are shown next to the nodes: Analysis Of Algorithms
Tree 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
Tree ………. 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) > 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) +2 • Thus the height of an AVL tree is O(log n) Analysis Of Algorithms
Tree Rotation: Double rotation (inside case) ….. 3 3 20 20 1 3 1 2 10 30 10 35 0 2 0 0 1 1 5 Imbalance 40 5 40 25 30 0 25 34 1 0 45 35 45 0 Insertion of 34 0 34 Analysis Of Algorithms
Tree …..Rotations ….. Double rotation j k Z i X W V Analysis Of Algorithms
Tree …..Rotations A Double or Single X B Z C V W Analysis Of Algorithms
Tree Red-BlackTrees Analysis Of Algorithms
Tree Red andBlack 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
Tree A Red andBlack Tree with n internal nodes has height at most 2log(n+1). Analysis Of Algorithms
Tree G X U P G X P U Case 1 – U is Red Just recolor and move up Analysis Of Algorithms
Tree G P U X X S G P S U Case 2 – Zig-Zag Double rotate X around P and X around G Recolor G and X Analysis Of Algorithms
Tree G P U P S X G X Case 3 – Zig-Zig Single Rotate P around G Recolor P and G U S Analysis Of Algorithms
Tree Insert 4 into this R-B Tree 11 14 2 15 1 7 5 8 Analysis Of Algorithms
Tree • Red–Blacktrees 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–Blacktrees 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–Blacktrees. Analysis Of Algorithms