Download presentation

Presentation is loading. Please wait.

Published byColin Walton Modified over 2 years ago

1
Trees-I Prof. Muhammad Saeed Analysis of Algorithms

2
Analysis Of Algorithms2 Tree Representation ….. Tree

3
Analysis Of Algorithms3 Tree ….. Tree Representation

4
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 Degree Level Nomenclature Tree

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

6
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

7
Analysis Of Algorithms Complete binary treeFull binary tree of depth 4 Tree 7

8
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

9
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

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

11
Analysis Of Algorithms11 Tree Priority queue representations

12
Analysis Of Algorithms 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

13
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 …..

14
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

15
Analysis Of Algorithms15 Balanced and unbalanced BST Tree Is this balanced?

16
Analysis Of Algorithms16 Tree Rotations: Single Rotation …..

17
Analysis Of Algorithms Tree …..Rotations: Single Rotation ….. j k X Y Z h h+1h j k X Y Z h h

18
Analysis Of Algorithms18 Tree ……. Rotations …..

19
Analysis Of Algorithms19 Tree

20
Analysis Of Algorithms20

21
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

22
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)

23
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

24
Analysis Of Algorithms24 Rotation: Double rotation (inside case) … Imbalance Insertion of Tree

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

26
Analysis Of Algorithms26 …..Rotations Tree Double or Single

27
Tree Analysis Of Algorithms27

28
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.

29
Analysis Of Algorithms29 A Red and Black Tree with n internal nodes has height at most 2log(n+1). Tree

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

31
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

32
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

33
Analysis Of Algorithms33 Tree Insert 4 into this R-B Tree

34
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).

35
End Trees I

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google