# Trees-I Prof. Muhammad Saeed Analysis of Algorithms.

## Presentation on theme: "Trees-I Prof. Muhammad Saeed Analysis of Algorithms."— Presentation transcript:

Trees-I Prof. Muhammad Saeed Analysis of Algorithms

Analysis Of Algorithms2 Tree Representation ….. Tree

Analysis Of Algorithms3 Tree ….. Tree Representation

Analysis Of Algorithms4 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 Level 1 2 3 4 3 2 13 1 0 0 1 0 0 0 0 0 1 22 2 3 3 3 3 3 3 4 4 4 Degree Level Nomenclature Tree

Analysis Of Algorithms5 Tree Types Binary Tree Binary Search Tree B-Tree AVL Tree Red-Black Tree Splay Tree Binomial Tree

Analysis Of Algorithms6 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 7 5 9 4 8 6 1 2 3 7 5 11 4 10 6 9 8 15 14 13 12 Complete binary treeFull binary tree of depth 4 Tree 7

Analysis Of Algorithms8 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 Algorithms9 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 + * A * / E D C B Arithmetic Expression Using Binary Tree Tree

Analysis Of Algorithms10 Tree Property: The root of max heap (min heap) contains the largest (smallest). Heaps

Analysis Of Algorithms11 Tree Priority queue representations

Analysis Of Algorithms12 56 26200 18 28 190 213 12 2427 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 Algorithms13 Tree 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) ….. Binary Search Tree - Best Time …..

Analysis Of Algorithms14 …..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 Algorithms15 Balanced and unbalanced BST Tree 4 25 13 1 5 2 4 3 7 6 4 26 5713 Is this balanced?

Analysis Of Algorithms16 Tree Rotations: Single Rotation …..

Analysis Of Algorithms17 0 1 0 2 0 6 4 9 8 15 1 0 7 2 10 2 0 6 49 815 1 0 7 Tree …..Rotations: Single Rotation ….. j k X Y Z h h+1h j k X Y Z h h

Analysis Of Algorithms18 Tree ……. Rotations …..

Analysis Of Algorithms19 Tree

Analysis Of Algorithms20

Analysis Of Algorithms21 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: AVL(Adelson-Velskii-Landis) trees Tree

Analysis Of Algorithms22 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) = 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 Algorithms23 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) ………. Height of an AVL Tree Tree

Analysis Of Algorithms24 Rotation: Double rotation (inside case) ….. 3 0 3 20 1030 25 1 40 2 5 0 20 1035 30 1 405 45 0 1 2 3 Imbalance 45 0 1 Insertion of 34 35 34 0 0 1 2534 0 Tree

Analysis Of Algorithms25 Tree …..Rotations ….. Double rotation j k X V Z W i

Analysis Of Algorithms26 …..Rotations Tree Double or Single

Tree Analysis Of Algorithms27

Analysis Of Algorithms28 Red and Black Trees Tree 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. 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 Algorithms29 A Red and Black Tree with n internal nodes has height at most 2log(n+1). Tree

Analysis Of Algorithms30 Case 1 – U is Red Just recolor and move up X P G U P G U X Tree

Analysis Of Algorithms31 X P G U S X P G S U Case 2 – Zig-Zag Double rotate X around P and X around G Recolor G and X Tree

Analysis Of Algorithms32 Tree X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G

Analysis Of Algorithms33 Tree 11 14 15 2 1 7 5 8 Insert 4 into this R-B Tree

Analysis Of Algorithms34 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 BSTs require O(n).

End Trees I