Data Structures and Algorithms(12)

Size: px
Start display at page:

Download "Data Structures and Algorithms(12)"

Transcription

1 Ming Zhang "Data s and Algorithms" Data s and Algorithms(12) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 28.6 (the "Eleventh Five-Year" national planning textbook)

2 Chapter 12 Advanced data structure 12.1 Multidimensional Array 12.2 Generalized Lists 12.3 Storage management 12.4 Trie 2 Ming Zhang Data s and Algorithms"

3 12.3 Trie Ideal situation: The average time of insertion, deletion, and search is O(logN) Input 9, 4, 2, 6, 7, 15, 12, 21 2 Output 2, 4, 6, 7, 9, 12, 15, Ming Zhang Data s and Algorithms"

4 12.4 Trie of Trie Space division of key trie comes from retrieval Application Information retrieval Large scale of English dictionary 26-branch Trie Binary Trie Letters (numbers) represented as binary coding Coding includes just and 1 4 Ming Zhang Data s and Algorithms"

5 12.4 Trie Tree of English words: 26-branch Trie Store words and, ant, bad, bee Subtree an contains set {and, ant} that every word from the set has the same prefix an. n a a b e d t d e and ant bad bee A subtree contains the words with the same prefix 5 Ming Zhang Data s and Algorithms"

6 12.4 Trie Store words an, and ant, bad, bee a n * t d an * * and 6 b a e * * bad bee ant Ming Zhang Data s and Algorithms"

7 12.4 Trie Compact the Single Paths close to the leaf Store words an and ant bad bee a b n a e * an t d and ant bad bee 7 Ming Zhang Data s and Algorithms"

8 12.4 Trie Binary Trie Elements are (<32) 1 (>32) (<16) 1 (>16) 1 (>48) (<8) 1 (>8) (<48) 17 1 (>4) 63 (<4) 1 (>4) 9 1 (>44) (<44) Ming Zhang Data s and Algorithms"

9 12.4 Trie PATRICIA xxxxx 1xxxxx 1 1 xxxx 1xxxx 1xxxx 11xxxx Compression xxx 1xxx 11xxx xx 111xx xx 1xx Code: 2:1 5:11 9:11 17:11 41:111 45: : Ming Zhang Data s and Algorithms"

10 12.4 Trie Characteristics of PATRICIA Tree The compressed PATRICIA tree is a full binary tree Every internal node represents a 1-bit comparison Always at least two children are generated The number of comparisons will not exceed the length of the key 1 Ming Zhang Data s and Algorithms"

11 ab b a Chapter Trie explicit states a b b c b a c c c b Suffix Trees implicit states ab b ab b abc c abc T = ababc c c c Ascending Order ababc abc abc bc c c abc c bc abc bc ababc babc Suffix Trie 11 ababc babc Suffix Tree Ming Zhang Data s and Algorithms"

12 5 ALAM$ 1 ALAYALAM$ 7 AM$ 3 AYALAM$ 6 LAM$ 2 LAYALAM$ MALAYALAM$ 8 M$ 4 YALAM$ 9 $ 12.4 Trie M A L A Y A L A M $ Suffix Array Suffix Array The longest common prefix array Suffix 5 and Suffix 1 share ALA Suffix 1 and Suffix 7 share A LCP always adjacent 12 Ming Zhang Data s and Algorithms"

13 12.4 Trie 5 ALAM$ 1 ALAYALAM$ 7 AM$ AYALAM$ 6 LAM$ 2 LAYALAM$ MALAYALAM$ 8 M$ 4 YALAM$ 9 $ D = 3 SA lcp 13 D = LA D = 2 D = Ming Zhang Data s and Algorithms"

14 12.4 Trie Discussions Can Trie handle Chinese characters? What about PATRICIA Trie structure? Learn related document about Suffix Array and Suffix Tree. And think about their applications. 14 Ming Zhang Data s and Algorithms"

15 Chapter 12 Advanced data structure 12.1 Multidimensional Array 12.2 Generalized Lists 12.3 Storage management 12.4 Trie Balanced binary search tree Concept and inserting operation of AVL tree Deleting operation and efficiency analysis of AVL tree Splay Tree 15 Ming Zhang Data s and Algorithms"

16 AVL The performance of BST operations are affected by the input sequence Best O(log n); Worst O(n) Adelson-Velskii and Landis AVL tree, a balanced binary search tree Always O(log n) Ming Zhang Data s and Algorithms"

17 Single Rotation AVL Swap the node with its father, while keeping the property of BST y zig x x C A y A B B C zag 17 Ming Zhang Data s and Algorithms"

18 AVL Single Rotation and Double Rotation: Keep the BST property Ming Zhang Data s and Algorithms"

19 AVL Equivalent rotation: Keep the BST property Ming Zhang Data s and Algorithms"

20 AVL Empty tree is allowed The height of AVL tree with n nodes is O(log n) If T is an AVL tree Then the left and right subtree of T: T L, T R are also AVL trees And h L -h R 1 h L, h R are the heights of its left and right subtree. 2 Ming Zhang Data s and Algorithms"

21 Examples of AVL tree T1 T4 T2 T3 Ti-1 Ti-2 Ti 21 Ming Zhang Data s and Algorithms"

22 Balance Factor Balance Factor,bf (x): bf (x) = height x rchild height(x lchild ) Balance Factor might be, 1 and Ming Zhang Data s and Algorithms"

23 Insertion in an AVL tree Just like BST: insert the current node as a leaf node Situations during adjustment The current node was balanced. Now its left or right subtree becomes heavier. Modify the balance factor of the current node The current node had a balance factor of ±1. Now the current node becomes balanced. Height stays the same. Do not modify. The current node had a balance factor of ±1. Now the heavier side becomes heavier Unbalanced dangerous node 23 Ming Zhang Data s and Algorithms"

24 Rebalance Become unbalanced after inserting 17 Adjustment 24 Ming Zhang Data s and Algorithms"

25 Unbalanced situation occurs after insertion Insert the current node as leaf node in BST Assume a is the most close node to the current node. And its absolute value of balance factor is not zero. The current node s with key is in its left subtree or its right subtree. Assume that it is inserted into the right subtree. The original balance factor: (1) bf(a) = 1 (2) bf(a) = (3) bf(a) = Ming Zhang Data s and Algorithms"

26 Assume a is the most close node to the current node s. And its absolute value of balance factor is not zero. S is in a s left subtree or right subtree. Assume S is in the right subtree. Because balance factors of nodes in paths from s to a change from to +1. So as for node a: 1. bf(a) = 1, then bf(a) =, and the height of node a s subtree stays the same. 2. bf(a) =, then bf(a) = +1, and the height of node a s subtree stays the same. Because of the definition of a (bf(a) ), we can know that node a is the root. 3. bf(a) = +1, then bf(a) = +2, and adjustment is needed. 26 Ming Zhang Data s and Algorithms"

27 Unbalanced Cases The balance factors of any nodes must be, 1, 1 a s left subtree was heavier, bf(a) = 1, and bf(a) become -2 after insertion. LL: insert into the left subtree of a s left child. Left heavier+left heavier, bf(a) become 2 LR: insert into the right subtree of a s left child. Left heavier+right heavier, bf(a) become 2 Likewise, bf(a) = 1, and bf(a) become 2 after insertion RR: the node that causes unbalanced is in the right subtree of a s right child. RL: the node that causes unbalanced is in the left subtree of a s right child. 27 Ming Zhang Data s and Algorithms"

28 Unbalanced Cases a -2 a 2 b -1 h h 1 b h+1 h h h+1 LL RR 28 Ming Zhang Data s and Algorithms"

29 Summary of unbalanced cases LL is symmetry with RR, and LR is symmetry with RL. Unbalanced nodes happen on the path from inserted node to the root. Its balance factor must be 2 or 2 If 2, the balance factor before insertion is 1 If 2, the balance factor before insertion is 1 29 Ming Zhang Data s and Algorithms"

30 LL single rotation a -2 b -1 h h+1 h T 3 T1 T 2 3 Ming Zhang Data s and Algorithms"

31 Insight of Rotations Take RR for instance, there are 7 parts Three nodes: a, b, c Four subtrees T, T 1, T 2, T 3 The structure will not change after making c s subtree heavier. T 2, c, T 3 could be regarded as b s right subtree. Goal: construct a new AVL structure Balanced Keep the BST property T a T 1 b T 2 c T 3 31 Ming Zhang Data s and Algorithms"

32 Double Rotation RL or LR needs double rotation. They are symmetry with each other We discuss about RL only LR is the same. 32 Ming Zhang Data s and Algorithms"

33 First step of RL double rotation h T 2 a First step -1 c 1 Or -1 b h Height of a s subtree is h+2 before inserting Height of a s subtree is h+3 after inserting h-1 /h T 1 h/ h-1 T 2 T 3 33 Ming Zhang Data s and Algorithms"

34 Second step of RL double rotation Balance factor is meaningless in the middle status T h a 2 Balance factor of a is -1 or Balance factor of b is or 1 h-1 /h T 1 RL 第一步 RL 第二步 1 c h/ h-1 T 2 1 b h T 3 34 Ming Zhang Data s and Algorithms"

35 Insight of Rotations Doing any rotations (RR, RL, LL, LR) New tree keeps the BST property Few pointers need to be modified during rotations. Height of the new subtree is h+2, and heights of subtrees before insertion stay same Rest parts upon node a (if not empty) are always balanced 35 Ming Zhang Data s and Algorithms"

36 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia cup -1-2 cop 1 copy Unbalanced after inserting copy LR double rotation 36 Ming Zhang Data s and Algorithms"

37 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia copy 2 1 cop cup 12 hit -1 hi 37 Ming Zhang Data s and Algorithms"

38 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia copy RL double rotation cop cup hit hi 38 Ming Zhang Data s and Algorithms"

39 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia copy 12 cop hi 1 cup hit -1 his 39 Ming Zhang Data s and Algorithms"

40 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia copy RR single rotation cop hi cup hit his 4 Ming Zhang Data s and Algorithms"

41 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia hi 1 copy hit -2-1 cop cup his -1 hia 41 Ming Zhang Data s and Algorithms"

42 AVL tree after inserting word:cup, cop, copy, hit, hi, his and hia LL single rotation hi copy hit cop cup his hia 42 Ming Zhang Data s and Algorithms"

43 Discussions Can we modify the definition of balance factor of AVL tree? For example, allow the height difference as big as 2. Insert 1, 2, 3,..., 2 k -1 into an empty AVL tree consecutively. Try to prove the result is a complete binary tree with height k. 43 Ming Zhang Data s and Algorithms"

44 Chapter 12 Advanced data structure 12.1 Multidimensional Array 12.2 Generalized Lists 12.3 Storage management 12.4 Trie Balanced binary search tree Concept and insertion operation of AVL tree Deletion operation and efficiency analysis of AVL tree Splay Tree 44 Ming Zhang Data s and Algorithms"

45 Deletion in an AVL Tree Deletion is the reverse operation of insertion. Deletion in an AVL tree is almost the same with the deletion of BST. Deletion in an AVL tree is a little bit complicated. 45 Ming Zhang Data s and Algorithms"

46 Adjustment after Deletion Conditions of adjustment Height and balance factor has changed. Adjust these changes from the current node to the root Balance factor needs to be modified Modify it If modification is not needed then stop the adjustment Use bool variable modified to mark, initialize it with TRUE when modified = FALSE, stop the adjustment. 46 Ming Zhang Data s and Algorithms"

47 Case 1 Balance factor of the current node a is Its left or right subtree is cut, balance factor is 1 or -1 modified = FALSE Modification will not influent the nodes above it. So the adjustment is over. 1 delete h h h-1 h 47 Ming Zhang Data s and Algorithms"

48 Case 2 Balance factor of the current node is not, but the taller subtree is cut. Modify its balance factor to modified = TRUE Keep modifying upward -1 h+1 h Height reduced by1 h h delete 48 Ming Zhang Data s and Algorithms"

49 Case 3.1 Balance factor of the current node a is not zero. And the shorter subtree is cut. So node a isn t balanced. Assume the root of the taller subtree is b. There are three cases. case 3.1:balance factor of b is zero Single rotation modified = FALSE a b 1-1 delete h h b h zag h-1 a 1 h h 49 Ming Zhang Data s and Algorithms"

50 Case 3.2 Case 3.2: balance factor of b is equal to that of a. Single rotation Balance factors of a and b change to zero modified =TRUE delete h 1 a b zag 1 h-1 h h-1 a b h-1 h 5 Ming Zhang Data s and Algorithms"

51 Case 3.3 Case 3.3: balance factor of b is opposite to that of a Double rotation, rotate b and c, then rotate c and a Because of balance factor of the new root is Other nodes need adjustment Besides modified = TRUE delete h h-2 /h-1 1 c a -1 h-1 /h-2 b h-1 Double rotation Height reduced by one c a b h-1 h-1 h-1 or h-2 51 Ming Zhang Data s and Algorithms"

52 Series of adjustments after deletion Series of adjustments Ancestor nodes may be unbalanced after the adjustment Keep adjusting. These adjustments might pass to the root. Find the grandfather of the node deleted just now. Start single rotation or double rotation Rotation times is O(log n) 52 Ming Zhang Data s and Algorithms"

53 Examples of deletion d -1 1 b g -1 k n i m -1 1 d -1 l b g k n -1-1 i -1 a c f h j l e -1 a c f h j (a) Delete node m, replace m with node I (case 1) e 53 Ming Zhang Data s and Algorithms"

54 Examples of deletion d b g -1 k -1 n i l d -1 1 b g k -1-1 i -2 l a c f h j e (b) delete n (Case 3.2) -1 a c f h j Ming Zhang Data s and Algorithms" e (c) Regard l as the root, doing LL single rotation (Case 3.2)

55 Examples of deletion a b d c 1 g -1-1 f 55-2 h i k e (d) After LL single rotation, then adjust its father node I, doing LR double rotation (Case 3.3) j l b d Ming Zhang Data s and Algorithms" f -1 g h 1 i a c e j k (e) Adjustment is done, AVL tree become balanced l

56 Height of AVL tree Height of AVL tree with n nodes must be O(log n) The maximum height of AVL tree with n nodes is no more than K log 2 n K is a small factor AVL tree that is the most close to unbalanced Construct a series of AVL tree T 1, T 2, T 3, 56 Ming Zhang Data s and Algorithms"

57 Ti s height is i T1 T2 T3 Any other AVL trees with height I have more nodes than T i Ti Ti-1 Ti-2 T4 T i is the most unbalanced AVL tree. Deleting any node will cause it to be unbalanced. 57 Ming Zhang Data s and Algorithms"

58 Proof of height We can see that: t(1) = 2 t(2) = 4 t(i) = t(i-1) + t(i-2) + 1 (t(i)+1) = (t(i-1)+1) + (t(i-2)+1) As for i>2 these relations are very similar to Fibonacci numbers: F() = F(1) = 1 F(i) = F(i-1) + F(i-2) 58 Ming Zhang Data s and Algorithms"

59 Proof of height As for i>l, we have t (i) = F (i+3) - 1 Fibonacci number has this formula F( i) 1 5 i So the approximate formula is,here t(i) i Ming Zhang Data s and Algorithms"

60 Proof of height (result) Relation between height I and the number of nodes t(i) i 3 5(t(i) 1) Use formula log φ X = log 2 X / log 2 φ and log 2 φ.694, calculate the approximate upper bound. t (i) = n i 3 log 5 log (t(i) 1) So height of AVL tree with n nodes must be O (log n) 3 i log 2 (n 1) Ming Zhang Data s and Algorithms"

61 Efficiency of AVL tree Time cost of searching, inserting and deleting is O(1og 2 n) Height of AVL tree with n nodes must be O(log n) AVL tree is fit to data of small scale in memory For large scale data stored in external memory B tree or B+ tree 61 Ming Zhang Data s and Algorithms"

62 Discussion Compare AVL tree with black-red tree, which is better? Height of tree in the worst case Efficiency of operation in statistics Which one is easier to implement? 62 Ming Zhang Data s and Algorithms"

63 Chapter 12 Advanced data structure 12.1 Multidimensional Array 12.2 Generalized Lists 12.3 Storage management 12.4 Trie Balanced binary search tree Splay tree 63 Ming Zhang Data s and Algorithms"

64 Splay Tree A self-organized data structure Adjust data during search Dictionary of Chinese input method Splay tree is not a new data structure. It just adds some rules to BST to improve efficiency. Make sure the total cost of search is not high and achieve a satisfactory performance. Heights of tree are not balanced. 64 Ming Zhang Data s and Algorithms"

65 Splay Tree Single Rotation Swap the current node and its father. Keep the BST property y zig x x C A y A B zag B C 65 Ming Zhang Data s and Algorithms"

66 Splay Tree Double Rotation: LL and RR. Keep the BST property. z y D x C A B zig zag x A y B z C D 66 Ming Zhang Data s and Algorithms"

67 Splay Tree A Double Rotation: LL and RR. Keep the BST property. zig-zag zag-zig z y D x B C x y z A B C D A B x y z C D 67 Ming Zhang Data s and Algorithms"

68 Splaying Visit a node (e.g., node x). Then do an operation called splaying. After inserting or visiting node x, splay (move) node x to the root. After deleting node x. Splay (move) the father of node x to the root. Like AVL tree. The operation of splaying x consists of a series of rotations. Adjust x, father of x and grandfather of x. Move x toward the root. 68 Ming Zhang Data s and Algorithms"

69 Single rotation x is the child of the root Swap x and the father of x Keep the BST property X, y is marks of inner node not value. A, B, C represent subtree. y x C A B 69 Ming Zhang Data s and Algorithms"

70 Double rotation Double rotation involves: Node x The father of node x (called y) The grandfather of node x (called z) Rise node x by two (height). zigzig rotation Also called homogeneous configuration zigzag rotation Also called heterogeneous configuration 7 Ming Zhang Data s and Algorithms"

71 ZigZig y z D Node x is the left child of y Node y is the left child of z x C A B 71 Ming Zhang Data s and Algorithms"

72 ZigZig y x z A B C D 72 Ming Zhang Data s and Algorithms"

73 z ZigZag y D Node x is the right child of y Node y is the left child of z A B x C 73 Ming Zhang Data s and Algorithms"

74 ZigZag z x D y C A B 74 Ming Zhang Data s and Algorithms"

75 ZigZag x y z A B C D 75 Ming Zhang Data s and Algorithms"

76 Different results of ZigZig and ZigZag ZigZag Move the current node toward the root The height of its subtree decreased by one Make the tree more balanced ZigZig The height of the subtree will not change usually Move the current node toward the root 76 Ming Zhang Data s and Algorithms"

77 Adjustment of Splay Tree A series of rotations Until node x become root or the child of root. If the node x is the child of root Single rotation will make x become root This process makes tree more balanced Make nodes, which are frequently visited, closer to the root Reduce the cost of search 77 Ming Zhang Data s and Algorithms"

78 Adjustment of Splay Tree h A f g e H I (a-b-c) ZagZag B d c G (a, c)zag C b (a, b)zag D a E F 78 Ming Zhang Data s and Algorithms"

79 Adjustment of Splay Tree h g I A f d e H G a-d-e ZagZig B a b F c E C D 79 Ming Zhang Data s and Algorithms"

80 Adjustment of Splay Tree (a, g) Zig f g h H I (a-f-g) ZagZig A a (a, f) Zag d e B b F G c E C D 8 Ming Zhang Data s and Algorithms"

81 Single rotation Adjustment of Splay Tree (a, h) Zag a h I f g A d e H B b F G c E C D 81 Ming Zhang Data s and Algorithms"

82 Adjustment of Splay Tree a f h A d g I B b e H c E F G C D 82 Ming Zhang Data s and Algorithms"

83 Splay Tree struct TreeNode { int key; ELEM value; TreeNode *father, * left, *right; }; Splay(TreeNode *x, TreeNode *f);// Splay x to the child of f Splay(x, NULL); // Splay x to the root Find(int k) ; Insert(int k); Delete(TreeNode *x) ; // delete node x DeleteTree(TreeNode *x); // delete subtree x 83 Ming Zhang Data s and Algorithms"

84 void Splay (TreeNode *x, TreeNode *f) { while (x->parent!= f) { TreeNode *y = x-> parent, *z = y-> parent; } if (z!= NULL) { } } else { if (z->lchild == y) { if (y->lchild == x) { Zig(y); Zig(x); } // ZigZig else { Zag(x); Zig(x); } } else { if (y->lchild == x) // grandfather is null // Zag x then Zig x { Zig(x); Zag(x); } // Zig x then Zag x else { Zag(y); Zag(x); } // ZagZag if (y->lchild == x) Zig(x); else Zag(x); } } if (x->parent == NULL) Root = x; // Zig // Zag 84 Ming Zhang Data s and Algorithms"

85 Delete all nodes range from u to v Splay u to the root Splay v to the right child of u Delete the left subtree of v void DeleteUV(TreeNode* rt, TreeNode* u, TreeNode* v ) { Splay(u, NULL); Splay(v, u); DeleteTree(v->lchild); v->lchild = NULL; } Ming Zhang Data s and Algorithms"

86 Efficiency of Splay tree The number of nodes is N m operations (insertion, deletion and search). when m n the total time cost is O(m logn) Single operation may not be efficient The average time cost is O(log n) 86 Ming Zhang Data s and Algorithms"

87 ZigZig A * Semi-Splay Tree z y D x C B ==> * A x y B z C D Semi- ZigZig A * x y B z C D ==> * x A B y z C D Next rotation start from y not x 87 Ming Zhang Data s and Algorithms"

88 Discussion What are the applications of splay tree? Comparisons among red-black tree, AVL tree and Splay tree Visiting frequency? Relationship between tree structure and input data? Which is better from the statistics? Which is easier to implement? 88 Ming Zhang Data s and Algorithms"

89 Ming Zhang Data s and Algorithms Data s and Algorithms Thanks the National Elaborate Course (Only available for IPs in China) Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 28.6 (awarded as the "Eleventh Five-Year" national planning textbook)

Algorithms. AVL Tree

Algorithms. AVL Tree Algorithms AVL Tree Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other

More information

Part 2: Balanced Trees

Part 2: Balanced Trees Part 2: Balanced Trees 1 AVL Trees We could dene a perfectly balanced binary search tree with N nodes to be a complete binary search tree, one in which every level except the last is completely full. A

More information

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees Dynamic Access Binary Search Trees 1 * are self-adjusting binary search trees in which the shape of the tree is changed based upon the accesses performed upon the elements. When an element of a splay tree

More information

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times Balanced BST Balanced BSTs guarantee O(logN) performance at all times the height or left and right sub-trees are about the same simple BST are O(N) in the worst case Categories of BSTs AVL, SPLAY trees:

More information

CS Transform-and-Conquer

CS Transform-and-Conquer CS483-11 Transform-and-Conquer Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 1:30pm - 2:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/ lifei/teaching/cs483_fall07/

More information

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees Dynamic Access Binary Search Trees 1 * are self-adjusting binary search trees in which the shape of the tree is changed based upon the accesses performed upon the elements. When an element of a splay tree

More information

Data Structures Week #6. Special Trees

Data Structures Week #6. Special Trees Data Structures Week #6 Special Trees Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees 21.Aralık.2010 Borahan Tümer, Ph.D. 2 AVL Trees 21.Aralık.2010 Borahan Tümer, Ph.D. 3 Motivation for

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 21, 2018 Outline Outline 1 C++ Supplement 1.3: Balanced Binary Search Trees Balanced Binary Search Trees Outline

More information

Ch04 Balanced Search Trees

Ch04 Balanced Search Trees Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 05 Ch0 Balanced Search Trees v 3 8 z Why care about advanced implementations? Same entries,

More information

Data Structures Week #6. Special Trees

Data Structures Week #6. Special Trees Data Structures Week #6 Special Trees Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees October 5, 2015 Borahan Tümer, Ph.D. 2 AVL Trees October 5, 2015 Borahan Tümer, Ph.D. 3 Motivation for

More information

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013) More Binary Search Trees AVL Trees bstdelete if (key not found) return else if (either subtree is empty) { delete the node replacing the parents link with the ptr to the nonempty subtree or NULL if both

More information

CS 261 Data Structures. AVL Trees

CS 261 Data Structures. AVL Trees CS 261 Data Structures AVL Trees 1 Binary Search Tree Complexity of BST operations: proportional to the length of the path from a node to the root Unbalanced tree: operations may be O(n) E.g.: adding elements

More information

More BSTs & AVL Trees bstdelete

More BSTs & AVL Trees bstdelete More BSTs & AVL Trees bstdelete if (key not found) return else if (either subtree is empty) { delete the node replacing the parents link with the ptr to the nonempty subtree or NULL if both subtrees are

More information

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6 COMP11 Spring 008 AVL Trees / Slide Balanced Binary Search Tree AVL-Trees Worst case height of binary search tree: N-1 Insertion, deletion can be O(N) in the worst case We want a binary search tree with

More information

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17 AVL Trees (AVL Trees) Data Structures and Programming Spring 2017 1 / 17 Balanced Binary Tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time

More information

COMP171. AVL-Trees (Part 1)

COMP171. AVL-Trees (Part 1) COMP11 AVL-Trees (Part 1) AVL Trees / Slide 2 Data, a set of elements Data structure, a structured set of elements, linear, tree, graph, Linear: a sequence of elements, array, linked lists Tree: nested

More information

Data Structures and Algorithms(2)

Data Structures and Algorithms(2) Ming Zhang Data Structures and Algorithms Data Structures and Algorithms(2) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Spring 2009-2010 Outline BST Trees (contd.) 1 BST Trees (contd.) Outline BST Trees (contd.) 1 BST Trees (contd.) The bad news about BSTs... Problem with BSTs is that there

More information

Balanced Binary Search Trees. Victor Gao

Balanced Binary Search Trees. Victor Gao Balanced Binary Search Trees Victor Gao OUTLINE Binary Heap Revisited BST Revisited Balanced Binary Search Trees Rotation Treap Splay Tree BINARY HEAP: REVIEW A binary heap is a complete binary tree such

More information

Data Structures Week #6. Special Trees

Data Structures Week #6. Special Trees Data Structures Week #6 Special Trees Outline Adelson-Velskii-Landis (AVL) Trees Splay Trees B-Trees October 5, 2018 Borahan Tümer, Ph.D. 2 AVL Trees October 5, 2018 Borahan Tümer, Ph.D. 3 Motivation for

More information

CISC 235: Topic 4. Balanced Binary Search Trees

CISC 235: Topic 4. Balanced Binary Search Trees CISC 235: Topic 4 Balanced Binary Search Trees Outline Rationale and definitions Rotations AVL Trees, Red-Black, and AA-Trees Algorithms for searching, insertion, and deletion Analysis of complexity CISC

More information

CMPE 160: Introduction to Object Oriented Programming

CMPE 160: Introduction to Object Oriented Programming CMPE 6: Introduction to Object Oriented Programming General Tree Concepts Binary Trees Trees Definitions Representation Binary trees Traversals Expression trees These are the slides of the textbook by

More information

CSCI2100B Data Structures Trees

CSCI2100B Data Structures Trees CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree

More information

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree. The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012

More information

Analysis of Algorithms

Analysis of Algorithms 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

More information

Search Structures. Kyungran Kang

Search Structures. Kyungran Kang Search Structures Kyungran Kang (korykang@ajou.ac.kr) Ellis Horowitz, Sartaj Sahni and Susan Anderson-Freed, Fundamentals of Data Structures in C, 2nd Edition, Silicon Press, 2007. Contents Binary Search

More information

ICS 691: Advanced Data Structures Spring Lecture 3

ICS 691: Advanced Data Structures Spring Lecture 3 ICS 691: Advanced Data Structures Spring 2016 Prof. Nodari Sitchinava Lecture 3 Scribe: Ben Karsin 1 Overview In the last lecture we started looking at self-adjusting data structures, specifically, move-to-front

More information

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture TREE is hierarchical (non linear) data structure Binary trees Definitions Full tree,

More information

Balanced Search Trees. CS 3110 Fall 2010

Balanced Search Trees. CS 3110 Fall 2010 Balanced Search Trees CS 3110 Fall 2010 Some Search Structures Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need

More information

Data Structure - Advanced Topics in Tree -

Data Structure - Advanced Topics in Tree - Data Structure - Advanced Topics in Tree - AVL, Red-Black, B-tree Hanyang University Jong-Il Park AVL TREE Division of Computer Science and Engineering, Hanyang University Balanced binary trees Non-random

More information

Trees in General. A general tree is: Either the empty trädet,, or a recursive structure: root /. \ /... \ t1... tn

Trees in General. A general tree is: Either the empty trädet,, or a recursive structure: root /. \ /... \ t1... tn Trees in General Trees in General A general tree is: Either the empty trädet,, or a recursive structure: root /. \ /... \ t1... tn where t1,..., and tn in turn are trees and are named subtrees, whose root

More information

8.1. Optimal Binary Search Trees:

8.1. Optimal Binary Search Trees: DATA STRUCTERS WITH C 10CS35 UNIT 8 : EFFICIENT BINARY SEARCH TREES 8.1. Optimal Binary Search Trees: An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such

More information

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min Binary Search Trees FRIDAY ALGORITHMS Sorted Arrays Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min 6 10 11 17 2 0 6 Running Time O(1) O(lg n) O(1) O(1)

More information

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive

More information

Section 1: True / False (1 point each, 15 pts total)

Section 1: True / False (1 point each, 15 pts total) Section : True / False ( point each, pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered

More information

Binary Search Trees. Analysis of Algorithms

Binary Search Trees. Analysis of Algorithms Binary Search Trees Analysis of Algorithms Binary Search Trees A BST is a binary tree in symmetric order 31 Each node has a key and every node s key is: 19 23 25 35 38 40 larger than all keys in its left

More information

Red-Black, Splay and Huffman Trees

Red-Black, Splay and Huffman Trees Red-Black, Splay and Huffman Trees Kuan-Yu Chen ( 陳冠宇 ) 2018/10/22 @ TR-212, NTUST AVL Trees Review Self-balancing binary search tree Balance Factor Every node has a balance factor of 1, 0, or 1 2 Red-Black

More information

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi Binary Search Tree: Balanced Data Structures and Algorithms Emory University Jinho D. Choi Binary Search Tree Worst-case Complexity Search Insert Delete Unbalanced O(n) O(n) O(n) + α Balanced O(log n)

More information

Unit III - Tree TREES

Unit III - Tree TREES TREES Unit III - Tree Consider a scenario where you are required to represent the directory structure of your operating system. The directory structure contains various folders and files. A folder may

More information

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees Some Search Structures Balanced Search Trees Lecture 8 CS Fall Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need

More information

Lesson 21: AVL Trees. Rotation

Lesson 21: AVL Trees. Rotation The time required to perform operations on a binary search tree is proportional to the length of the path from root to leaf. This isn t bad in a well-balanced tree. But nothing prevents a tree from becoming

More information

Balanced Binary Search Trees

Balanced Binary Search Trees Balanced Binary Search Trees Pedro Ribeiro DCC/FCUP 2017/2018 Pedro Ribeiro (DCC/FCUP) Balanced Binary Search Trees 2017/2018 1 / 48 Motivation Let S be a set of comparable objects/items: Let a and b be

More information

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium CSC 172 Data Structures and Algorithms Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium Announcement Coming week: Nov 19 Nov 25 No Quiz No Workshop No New Lab Monday and Tuesday: regular Lab

More information

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE CSI Section E01 AVL Trees AVL Property While BST structures have average performance of Θ(log(n))

More information

Self-Balancing Search Trees. Chapter 11

Self-Balancing Search Trees. Chapter 11 Self-Balancing Search Trees Chapter 11 Chapter Objectives To understand the impact that balance has on the performance of binary search trees To learn about the AVL tree for storing and maintaining a binary

More information

Search Trees. Chapter 11

Search Trees. Chapter 11 Search Trees Chapter 6 4 8 9 Outline Binar Search Trees AVL Trees Spla Trees Outline Binar Search Trees AVL Trees Spla Trees Binar Search Trees A binar search tree is a proper binar tree storing ke-value

More information

lecture17: AVL Trees

lecture17: AVL Trees lecture17: Largely based on slides by Cinda Heeren CS 225 UIUC 9th July, 2013 Announcements mt2 tonight! mp5.1 extra credit due Friday (7/12) An interesting tree Can you make a BST that looks like a zig

More information

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition

More information

AVL Trees Heaps And Complexity

AVL Trees Heaps And Complexity AVL Trees Heaps And Complexity D. Thiebaut CSC212 Fall 14 Some material taken from http://cseweb.ucsd.edu/~kube/cls/0/lectures/lec4.avl/lec4.pdf Complexity Of BST Operations or "Why Should We Use BST Data

More information

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees 4. Trees 4.1 Preliminaries 4.2 Binary trees 4.3 Binary search trees 4.4 AVL trees 4.5 Splay trees 4.6 B-trees Malek Mouhoub, CS340 Fall 2002 1 4.1 Preliminaries A Root B C D E F G Height=3 Leaves H I J

More information

Splay Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018

Splay Trees. Programming II - Elixir Version. Johan Montelius. Spring Term 2018 Splay Trees Programming II - Elixir Version Johan Montelius Spring Term 2018 Introduction A splay tree is an ordered binary tree with the advantage that the last key we looked for is found in the root

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is d = ëlog for a binary tree

More information

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(lg n) is guaranteed when

More information

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27 Splay Trees (Splay Trees) Data Structures and Programming Spring 2017 1 / 27 Basic Idea Invented by Sleator and Tarjan (1985) Blind rebalancing no height info kept! Worst-case time per operation is O(n)

More information

[ DATA STRUCTURES ] Fig. (1) : A Tree

[ DATA STRUCTURES ] Fig. (1) : A Tree [ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several

More information

CS350: Data Structures Red-Black Trees

CS350: Data Structures Red-Black Trees Red-Black Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Red-Black Tree An alternative to AVL trees Insertion can be done in a bottom-up or

More information

Advanced Tree Data Structures

Advanced Tree Data Structures Advanced Tree Data Structures Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park Binary trees Traversal order Balance Rotation Multi-way trees Search Insert Overview

More information

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees Reading materials Dale, Joyce, Weems: 9.1, 9.2, 8.8 Liang: 26 (comprehensive edition) OpenDSA:

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba COMP 3170 - Analysis of Algorithms & Data Structures

More information

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation! Lecture 10: Multi-way Search Trees: intro to B-trees 2-3 trees 2-3-4 trees Multi-way Search Trees A node on an M-way search tree with M 1 distinct and ordered keys: k 1 < k 2 < k 3

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

CS102 Binary Search Trees

CS102 Binary Search Trees CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one

More information

Transform & Conquer. Presorting

Transform & Conquer. Presorting Transform & Conquer Definition Transform & Conquer is a general algorithm design technique which works in two stages. STAGE : (Transformation stage): The problem s instance is modified, more amenable to

More information

COMP Analysis of Algorithms & Data Structures

COMP Analysis of Algorithms & Data Structures COMP 3170 - Analysis of Algorithms & Data Structures Shahin Kamali Lecture 9 - Jan. 22, 2018 CLRS 12.2, 12.3, 13.2, read problem 13-3 University of Manitoba 1 / 12 Binary Search Trees (review) Structure

More information

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1 AVL Trees v 6 3 8 z 20 Goodrich, Tamassia, Goldwasser AVL Trees AVL Tree Definition Adelson-Velsky and Landis binary search tree balanced each internal node v the heights of the children of v can 2 3 7

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

CS350: Data Structures AVL Trees

CS350: Data Structures AVL Trees S35: Data Structures VL Trees James Moscola Department of Engineering & omputer Science York ollege of Pennsylvania S35: Data Structures James Moscola Balanced Search Trees Binary search trees are not

More information

3137 Data Structures and Algorithms in C++

3137 Data Structures and Algorithms in C++ 3137 Data Structures and Algorithms in C++ Lecture 4 July 17 2006 Shlomo Hershkop 1 Announcements please make sure to keep up with the course, it is sometimes fast paced for extra office hours, please

More information

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood Red-Black Trees Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood Quick Review of Binary Search Trees n Given a node n... q All elements of n s left subtree are less than n.data q

More information

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 0 AVL Trees v 6 3 8 z 0 Goodrich, Tamassia, Goldwasser

More information

Lecture 16 Notes AVL Trees

Lecture 16 Notes AVL Trees Lecture 16 Notes AVL Trees 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning 1 Introduction Binary search trees are an excellent data structure to implement associative arrays,

More information

Data Structures and Algorithms(8)

Data Structures and Algorithms(8) Data Structures and Algorithms(8) Instructor: Ming Zhang Textbook Authors:Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the Eleventh Five-Year national planning textbook) https://courses.edx.org/courses/pekingx/04830050x/2t2014/

More information

Data Structures and Algorithms Lecture 7 DCI FEEI TUKE. Balanced Trees

Data Structures and Algorithms Lecture 7 DCI FEEI TUKE. Balanced Trees Balanced Trees AVL trees reconstruction of perfect balance can be quite expensive operation less rigid criteria of balance (e.g. AVL) [2] inorder, self-balancing binary search tree (BST) Definition: AVL

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 26 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? Balanced Binary Search trees AVL trees Michael Eckmann - Skidmore College - CS

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 04 / 25 / 2018 Instructor: Michael Eckmann Today s Topics Questions? Comments? Balanced Binary Search trees AVL trees / Compression Uses binary trees Balanced

More information

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10 CSE 32: Data Structures Splay Trees James Fogarty Autumn 2007 Lecture 10 AVL Trees Revisited Balance condition: Left and right subtrees of every node have heights differing by at most 1 Strong enough :

More information

Chapter 22 Splay Trees

Chapter 22 Splay Trees Chapter 22 Splay Trees Introduction Splay trees support all the operations of binary trees. But they do not guarantee Ο(log N) worst-case performance. Instead, its bounds are amortized, meaning that although

More information

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor COSC160: Data Structures Balanced Trees Jeremy Bolton, PhD Assistant Teaching Professor Outline I. Balanced Trees I. AVL Trees I. Balance Constraint II. Examples III. Searching IV. Insertions V. Removals

More information

Lecture 7. Transform-and-Conquer

Lecture 7. Transform-and-Conquer Lecture 7 Transform-and-Conquer 6-1 Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance simplification)

More information

Data Structures Lesson 7

Data Structures Lesson 7 Data Structures Lesson 7 BSc in Computer Science University of New York, Tirana Assoc. Prof. Dr. Marenglen Biba 1-1 Binary Search Trees For large amounts of input, the linear access time of linked lists

More information

LECTURE 18 AVL TREES

LECTURE 18 AVL TREES DATA STRUCTURES AND ALGORITHMS LECTURE 18 AVL TREES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD PROTOTYPICAL EXAMPLES These two examples demonstrate how we can correct for imbalances: starting

More information

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees // AVL Trees AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel son-vel skii and Landis AVL tree approximates the ideal tree (completely balanced

More information

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search

Why Trees? Alternatives. Want: Ordered arrays. Linked lists. A data structure that has quick insertion/deletion, as well as fast search Why Trees? Alternatives Ordered arrays Fast searching (binary search) Slow insertion (must shift) Linked lists Want: Fast insertion Slow searching (must start from head of list) A data structure that has

More information

Data Structures and Algorithms(12)

Data Structures and Algorithms(12) Ming Zhang "Data s and Algorithms" Data s and Algorithms(12) Instructor: Ming Zhang Textbook Authors: Ming Zhang, Tengjiao Wang and Haiyan Zhao Higher Education Press, 2008.6 (the "Eleventh Five-Year"

More information

A set of nodes (or vertices) with a single starting point

A set of nodes (or vertices) with a single starting point Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of

More information

Recall from Last Time: AVL Trees

Recall from Last Time: AVL Trees CSE 326 Lecture 8: Getting to now AVL Trees Today s Topics: Balanced Search Trees AVL Trees and Rotations Splay trees Covered in Chapter 4 of the text Recall from Last Time: AVL Trees AVL trees are height-balanced

More information

Lecture 23: Binary Search Trees

Lecture 23: Binary Search Trees Lecture 23: Binary Search Trees CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki 1 BST A binary tree is a binary search tree iff it is empty or if the value of every node is both greater than or equal

More information

Module 4: Dictionaries and Balanced Search Trees

Module 4: Dictionaries and Balanced Search Trees Module 4: Dictionaries and Balanced Search Trees CS 24 - Data Structures and Data Management Jason Hinek and Arne Storjohann Based on lecture notes by R. Dorrigiv and D. Roche David R. Cheriton School

More information

Advanced Set Representation Methods

Advanced Set Representation Methods Advanced Set Representation Methods AVL trees. 2-3(-4) Trees. Union-Find Set ADT DSA - lecture 4 - T.U.Cluj-Napoca - M. Joldos 1 Advanced Set Representation. AVL Trees Problem with BSTs: worst case operation

More information

Fundamental Algorithms

Fundamental Algorithms WS 2007/2008 Fundamental Algorithms Dmytro Chibisov, Jens Ernst Fakultät für Informatik TU München http://www14.in.tum.de/lehre/2007ws/fa-cse/ Fall Semester 2007 1. AVL Trees As we saw in the previous

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2 CSCI 136 Data Structures & Advanced Programming Lecture 25 Fall 2018 Instructor: B 2 Last Time Binary search trees (Ch 14) The locate method Further Implementation 2 Today s Outline Binary search trees

More information

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due tonight at midnight -asking for regrades through assignment 5 and midterm must

More information

AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22

AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22 VL Trees Version of September 6, 6 VL Trees Version of September 6, 6 / inary Search Trees x 8 4 4 < x > x 7 9 3 inary-search-tree property For every node x ll eys in its left subtree are smaller than

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees Computer Science 10 Data Structures Siena College Fall 018 Topic Notes: Binary Search Trees Possibly the most common usage of a binary tree is to store data for quick retrieval. Definition: A binary tree

More information

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University Trees Reading: Weiss, Chapter 4 1 Generic Rooted Trees 2 Terms Node, Edge Internal node Root Leaf Child Sibling Descendant Ancestor 3 Tree Representations n-ary trees Each internal node can have at most

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010 Uses for About Binary January 31, 2010 Uses for About Binary Uses for Uses for About Basic Idea Implementing Binary Example: Expression Binary Search Uses for Uses for About Binary Uses for Storage Binary

More information

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node. UNIT 2 TREE STRUCTURES ADVANCED DATA STRUCTURES STUDY NOTES Binary Search Trees- AVL Trees- Red-Black Trees- B-Trees-Splay Trees. HEAP STRUCTURES: Min/Max heaps- Leftist Heaps- Binomial Heaps- Fibonacci

More information

Chapter 12 Advanced Data Structures

Chapter 12 Advanced Data Structures Chapter 12 Advanced Data Structures 2 Red-Black Trees add the attribute of (red or black) to links/nodes red-black trees used in C++ Standard Template Library (STL) Java to implement maps (or, as in Python)

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information