Data Structure - Advanced Topics in Tree -

Size: px
Start display at page:

Download "Data Structure - Advanced Topics in Tree -"

Transcription

1 Data Structure - Advanced Topics in Tree - AVL, Red-Black, B-tree Hanyang University Jong-Il Park

2 AVL TREE Division of Computer Science and Engineering, Hanyang University

3 Balanced binary trees Non-random insertion to BST can produce unbalanced trees Can we rebalance the tree so that the tree always has O(log n) height? We need balance information for each node. Balancing the root is not enough Balancing every node is too strict Division of Computer Science and Engineering, Hanyang University

4 AVL tree AVL tree is a binary search tree. Def: For every node in the tree, the heights of its left subtree and right subtree differ by at most 1. (the height of a null subtree is 1) Is this condition enough to guarantee the height of an AVL tree with n nodes is O(log n)? Division 4 of Computer Science and Engineering, Hanyang University

5 AVL tree with Minimum Number of Nodes N 0 = 1 N 1 = 2 N 2 = 4 N 3 = N 1 + N = 7 height of left=? height of right=? Division of Computer Science and Engineering, Hanyang University

6 Smallest AVL tree of height 9 Smallest AVL tree of height 7 Smallest AVL tree of height 8 Smallest AVL tree of height 9 Division of Computer Science and Engineering, Hanyang University

7 Height of AVL tree Denote N h the minimum number of nodes in an AVL tree of height h N 0 = 1, N 1 =2 (base) N h = N h-1 + N h-2 +1 (recursive definition) N > N h = N h-1 + N h-2 +1 > 2 N h-2 > 4 N h-4 > > 2 i N h-2i If h is even, let i = h/2 1. The equation becomes N > 2 h/2-1 N 2 N > 2 h/2-1 4 h = O(log N) Ifh is odd, let i = (h 1)/2. The equation becomes N > 2 (h-1)/2 N 1 N > 2 (h-1)/2 2 h = O(log N) Thus, many operations (i.e. searching) on an AVL tree will take O(lo g N) time 7 Division of Computer Science and Engineering, Hanyang University

8 Insertion into AVL tree Insertion routine is the same as BST, but if the resulting tree is unbalanced, we should rebalance it through rotating start from the node inserted, travel up the tree, and update balance info. if there s a bad node, rotate to resume balance Rotating is a local operation and takes a constant amount of time Division 8 of Computer Science and Engineering, Hanyang University

9 Rotation If the root of T is unbalanced after insertion of x, Single Rotation: if x < T->Left->Element (Rotate_Left) [LL] or if x > T->Right->Element (Rotate_Right) [RR] k2 k1 k1 k2 x y z x y z Division 9 of Computer Science and Engineering, Hanyang University

10 Rotation If the root of T is unbalanced after insertion of y, Does Single Rotation work? k2 k1 k1 k2 x y z x y z Division 10 of Computer Science and Engineering, Hanyang University

11 Rotation Double Rotation: if x > T->Left->Element (Rotate_Left) if x < T->right->Element (Rotate_Right) [LR] or [RL] k3 k2 w k1 x k2 y z k1 k3 w x y z k3 k2 w k2 k1 z k3 k1 w x y z x y Division 11 of Computer Science and Engineering, Hanyang University

12 From Wikipedia Division of Computer Science and Engineering, Hanyang University

13 Homework: Rotation exercises Single rotation Insert sequence: 3, 2, 1, 4, 5, 6, 7 Double rotation Insert sequence: 4, 2, 6, 1, 3, 5, 7, 16, 15, 14, 13, 12, 11 Another exercise Insert sequence: 2, 1, 4, 5, 9, 3, 6, 7 Insert sequence: 3, 2, 1, 4, 5, 6, 7, 16, 15, 14, 13, 12, 11, 10, 8, 9 Division 13 of Computer Science and Engineering, Hanyang University

14 Code for Rotations struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; struct AvlNode { ElementType Element; AvlTree Left; AvlTree Right; int Height; } static int Height(Position P) { if (P == NULL) return 1; else return P->Height; } Division 14 of Computer Science and Engineering, Hanyang University

15 Code for Rotations SingleRotateWithLeft( Position K2 ) { Position K1; k2 k1 K1 = K2->Left; K2->Left = K1->Right; K1->Right = K2; x k1 y z x y k2 z K2->Height = Max( Height( K2->Left ), Height( K2->Right ) ) + 1; K1->Height = Max( Height( K1->Left ), K2->Height ) + 1; return K1; /* New root */ } DoubleRotateWithLeft( Position K3 ) { K3->Left = SingleRotateWithRight( K3->Left ); return SingleRotateWithLeft( K3 ); } k3 k2 k1 k1 z k3 k2 w w x y z x y Division 15 of Computer Science and Engineering, Hanyang University

16 Code for insertion Insert( ElementType X, AvlTree T ) else if( X > T->Element ) { { T->Right = Insert( X, T->Right ); if( T == NULL ) { if( Height( T->Right ) Height( T->Left ) == 2 ) T = malloc( sizeof( struct AvlNode ) ); if( X > T->Right->Element ) if( T == NULL ) T = SingleRotateWithRight( T ); FatalError( Out of space!!! ); else else{ T = DoubleRotateWithRight( T ); T->Element = X; T->Height = 0; } T->Left = T->Right = NULL; /* Height adjustment when inserted without } rotation */ } T->Height = Max(Height( T->Left ), Height( T->Right ))+1; else if ( X < T->Element ) { return T; T->Left = Insert( X, T->Left ); } if( Height( T->Left ) Height( T->Right ) == 2 ) if( X < T->Left->Element ) T = SingleRotateWithLeft( T ); else T = DoubleRotateWithLeft( T ); } Division 16 of Computer Science and Engineering, Hanyang University

17 Deletion from an AVL tree May need O(log n) rotations when deleting a node, in the worst case. D E M A F X B G I C H J K L Division 17 of Computer Science and Engineering, Hanyang University

18 RED BLACK TREE Division of Computer Science and Engineering, Hanyang University

19 Red black tree Red black tree is a binary search tree in which every node is colored either red or black. All properties are based on the extended binary search tree; each null pointer is replaced with an external node. A pointer to a black child (including the external node) is black; a pointer to a red child is red. Properties of colored nodes root and all external nodes are black no consecutive red node is on the root-to-external node path all root-to-external node paths have the same number of black nodes Division 19 of Computer Science and Engineering, Hanyang University

20 Red black tree Rank (black height) of a node is the number of black pointers on any path from the node to any external node the rank of an external node is 0 Lemma 1 Let the length of a root-to-external-node path be the number of pointers on the path. If P and Q are two root-to-external-node paths in a red-black tree, length(p) 2 length(q) Proof: When r is the rank of the root, each root-to-external-node path has between r and 2r pointers Division 20 of Computer Science and Engineering, Hanyang University

21 Red black tree Division 21 of Computer Science and Engineering, Hanyang University

22 Insertion If a new node is colored in black, we will have an extra black node on paths require recoloring If a new node is colored in red, we might have two consecutive red nodes may or may not need recoloring Insert first make a normal insert into a binary search tree color it with red fix red-black properties Division of Computer Science and Engineering, Hanyang University

23 Insertion LLr new node LRr new node Division of Computer Science and Engineering, Hanyang University

24 Insertion a b LLb b c a c new node a a c LRb b c b a c new node b Division of Computer Science and Engineering, Hanyang University

25 Insertion insert 70, 60, 65, and 62 in order. Division 25 of Computer Science and Engineering, Hanyang University

26 Deletion We can delete a node with any external node in binary search tree delete a node without any child or with one child in the binary search tree When the node with two internal nodes is deleted, find the node of its predecessor or successor and delete that node delete a node with both children in the binary search tree delete 65 delete Division of Computer Science and Engineering, Hanyang University

27 Deletion If a red node is deleted, no rebalancing is needed since the rank is not changed (property #3) If a black node is deleted, rebalancing is needed color the edge double black Division of Computer Science and Engineering, Hanyang University

28 Deletion: how to remove the double edge black sibling with a red child - a node in blue can be either black or red - a node in dotted line can exist or not exist p s v s p z z v p z v s p s z v a b a b Division of Computer Science and Engineering, Hanyang University

29 Deletion: how to remove the double edge black sibling with black children (including any internal node) p p v s v s p p propagate upward! v s v s Division 29 of Computer Science and Engineering, Hanyang University

30 Deletion: how to remove the double edge red sibling restructure to have a black sibling p s s v s p b p b a b v a v a Division 30 of Computer Science and Engineering, Hanyang University

31 Homework: Deletion delete 9, 8, 7 Division 31 of Computer Science and Engineering, Hanyang University

32 B TREES Division of Computer Science and Engineering, Hanyang University

33 B-Trees Binary Trees are not quite appropriate for data stored on disks disk access is MUCH slower than memory access disk is partitioned into blocks (pages) and the access time of a word is the same as that of the entire block containing the word. We have to reduce the number of disk accesses Make each node of the tree wider (multi-way search tree) k k1 k2 k3 p0 p1 p2 p3 x < k x > k x < k1 k1<x<k2 k2<x<k3 k3 < x Division 33 of Computer Science and Engineering, Hanyang University

34 B-Trees (Rudolf Bayer 1972) Division 34 of Computer Science and Engineering, Hanyang University

35 An Example B-Tree with m = Tree 18 : 13 : 30 : 45 8 : : 22 : : 58 : 59 Division 35 of Computer Science and Engineering, Hanyang University

36 Height of a B-Tree Division 36 of Computer Science and Engineering, Hanyang University

37 Node Structure #define order 32 struct B_node { int n_child; /* number of children */ B_node *child[order]; /* children pointers */ int key[order-1]; /* keys */ } Division 37 of Computer Science and Engineering, Hanyang University

38 Search When we arrive at an internal node with key k 1 < k 2,... < k m-1, search for x in this list (either linearly or by binary search) if you found x, you are done otherwise, find the index i such that k i < x < k i+1 (k 0 = and k m = ), and recursively search the subtree pointed by p i. Complexity = log m log m n= O(log n) Division 38 of Computer Science and Engineering, Hanyang University

39 Insertion Do a search to find the appropriate leaf into which to insert the node if the leaf is not full (has < m 1keys), simply insert it if the node overflows, restore the balance (1) Key-Rotation: Check for Siblings for rotation 15 : : 42 9 : : 22 : 30 9 : : 30 T1 T1 Key-Rotation is convenient but neither sufficient nor necessary Division 39 of Computer Science and Engineering, Hanyang University

40 Insertion Do a search to find the appropriate leaf into which to insert the node if the leaf is not full (has < m 1keys), simply insert it if the node overflows, restore the balance (1) Key-Rotation: Check for Siblings for rotation 15 : : 42 9 : : 22 : 30 42: 9 : : : : 30 : 9 9 : 15 T1 18 : : 42 T1 Key-Rotation is convenient but neither sufficient nor necessary Division 40 of Computer Science and Engineering, Hanyang University

41 Insertion Division of Computer Science and Engineering, Hanyang University 41

42 8 : 19 3 : 12 : : 40 insert(36) 1 : 5 : 9 : 14 : 17 : 20 : : : 55 8 : 19 overflow! 3 : 12 : 16 1 : 5 : 9 : 14 : 17 : 8 : 19 : : 35 : : : 36 : 51 : 55 overflow! 3 : 12 : : 40 : 1 : 5 : 9 : 14 : 17 : 20 : : 36 : 51 : : 8 : 3 : 12 : : 27 : 40 : 1 : 5 : 9 : 14 : 17 : 20 : : 36 : 51 : 55 Division 42 of Computer Science and Engineering, Hanyang University

43 Deletion We still need to find a suitable replacement which is the largest key in the left child (or the smallest in the right) and move it to fill the hole. The replacement is always at the leaf level and creates a hole in a leaf node. if the leaf still has sufficient capacity, we are done. otherwise, node merging is performed. Division 43 of Computer Science and Engineering, Hanyang University

44 Merging Division of Computer Science and Engineering, Hanyang University 44

45 8 : 19 3 : 12 : 16 1 : 5 : 9 : 14 : 17 : 27 : : : : 55 delete(5) 8 : 19 3 : 12 : : 40 1 : 9 : 14 : 17 : 20 : : : 55 underflow! 8 : 19 Node Merge 12 : : 40 1 : 3 9 : 14 : 17 : 20 : : : : 19 8 : 16 : 27 : 40 Key Rotation 1 : 3 9 : 14 : 17 : 20 : : : 55 Division 45 of Computer Science and Engineering, Hanyang University

46 Difference from Weiss textbook (B+ Tree) All records are allowed to be stored only in the leaves. All non-leaf nodes include only the key values which can be found in the subtrees of the node. Leaf nodes can have a pointer to its next sibling so that a sequential access is possible Division 46 of Computer Science and Engineering, Hanyang University

47 Practical Use of B-Tree Division 47 of Computer Science and Engineering, Hanyang University

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

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

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

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

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

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

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

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

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

Data Structures in Java

Data Structures in Java Data Structures in Java Lecture 10: AVL Trees. 10/1/015 Daniel Bauer Balanced BSTs Balance condition: Guarantee that the BST is always close to a complete binary tree (every node has exactly two or zero

More information

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

Search Trees - 1 Venkatanatha Sarma Y

Search Trees - 1 Venkatanatha Sarma Y Search Trees - 1 Lecture delivered by: Venkatanatha Sarma Y Assistant Professor MSRSAS-Bangalore 11 Objectives To introduce, discuss and analyse the different ways to realise balanced Binary Search Trees

More information

Algorithms. Deleting from Red-Black Trees B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that

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

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

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

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2004 Goodrich, Tamassia (2,4) Trees 1 Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d -1 key-element

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

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

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

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

B-Trees. Version of October 2, B-Trees Version of October 2, / 22 B-Trees Version of October 2, 2014 B-Trees Version of October 2, 2014 1 / 22 Motivation An AVL tree can be an excellent data structure for implementing dictionary search, insertion and deletion Each operation

More information

(2,4) Trees. 2/22/2006 (2,4) Trees 1

(2,4) Trees. 2/22/2006 (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 2/22/2006 (2,4) Trees 1 Outline and Reading Multi-way search tree ( 10.4.1) Definition Search (2,4) tree ( 10.4.2) Definition Search Insertion Deletion Comparison of dictionary

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

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

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

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree B-Trees Disk Storage What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree Deletion in a B-tree Disk Storage Data is stored on disk (i.e., secondary memory) in blocks. A block is

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

Dictionaries. Priority Queues

Dictionaries. Priority Queues Red-Black-Trees.1 Dictionaries Sets and Multisets; Opers: (Ins., Del., Mem.) Sequential sorted or unsorted lists. Linked sorted or unsorted lists. Tries and Hash Tables. Binary Search Trees. Priority Queues

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

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

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

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

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

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

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

Augmenting Data Structures

Augmenting Data Structures Augmenting Data Structures [Not in G &T Text. In CLRS chapter 14.] An AVL tree by itself is not very useful. To support more useful queries we need more structure. General Definition: An augmented data

More information

Balanced search trees. DS 2017/2018

Balanced search trees. DS 2017/2018 Balanced search trees. DS 2017/2018 Red-black trees Symmetric binary B-tree, Rudolf Bayer, 1972. The balancing is maintained by using a coloring of the nodes. The red-black trees are binary search trees

More information

8. Binary Search Tree

8. Binary Search Tree 8 Binary Search Tree Searching Basic Search Sequential Search : Unordered Lists Binary Search : Ordered Lists Tree Search Binary Search Tree Balanced Search Trees (Skipped) Sequential Search int Seq-Search

More information

Motivation for B-Trees

Motivation for B-Trees 1 Motivation for Assume that we use an AVL tree to store about 20 million records We end up with a very deep binary tree with lots of different disk accesses; log2 20,000,000 is about 24, so this takes

More information

Chapter 4: Trees. 4.2 For node B :

Chapter 4: Trees. 4.2 For node B : Chapter : Trees. (a) A. (b) G, H, I, L, M, and K.. For node B : (a) A. (b) D and E. (c) C. (d). (e).... There are N nodes. Each node has two pointers, so there are N pointers. Each node but the root has

More information

Lecture 6: Analysis of Algorithms (CS )

Lecture 6: Analysis of Algorithms (CS ) Lecture 6: Analysis of Algorithms (CS583-002) Amarda Shehu October 08, 2014 1 Outline of Today s Class 2 Traversals Querying Insertion and Deletion Sorting with BSTs 3 Red-black Trees Height of a Red-black

More information

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it Balanced Trees bst algorithms can degenerate to worst case performance, which is bad because the worst case is likely to occur in practice, with ordered files, for example We will like to keep our trees

More information

Properties of red-black trees

Properties of red-black trees Red-Black Trees Introduction We have seen that a binary search tree is a useful tool. I.e., if its height is h, then we can implement any basic operation on it in O(h) units of time. The problem: given

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

ECE250: Algorithms and Data Structures AVL Trees (Part A)

ECE250: Algorithms and Data Structures AVL Trees (Part A) ECE250: Algorithms and Data Structures AVL Trees (Part A) Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University

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

Search Trees. COMPSCI 355 Fall 2016

Search Trees. COMPSCI 355 Fall 2016 Search Trees COMPSCI 355 Fall 2016 2-4 Trees Search Trees AVL trees Red-Black trees Splay trees Multiway Search Trees (2, 4) Trees External Search Trees (optimized for reading and writing large blocks)

More information

CSCI Trees. Mark Redekopp David Kempe

CSCI Trees. Mark Redekopp David Kempe CSCI 104 2-3 Trees Mark Redekopp David Kempe Trees & Maps/Sets C++ STL "maps" and "sets" use binary search trees internally to store their keys (and values) that can grow or contract as needed This allows

More information

CS 261 Data Structures. AVL Trees

CS 261 Data Structures. AVL Trees CS 261 Data Structures AVL Trees AVL Implementation struct AVLNode { TYPE val; struct AVLNode *left; struct AVLNode *rght; int hght; /* Height of node*/ ; Add Adam Alex Abner Angela Abigail Adela Add Insert

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

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

2-3 Tree. Outline B-TREE. catch(...){ printf( Assignment::SolveProblem() AAAA!); } ADD SLIDES ON DISJOINT SETS Outline catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } Balanced Search Trees 2-3 Trees 2-3-4 Trees Slide 4 Why care about advanced implementations? Same entries, different insertion sequence:

More information

AVL Trees (10.2) AVL Trees

AVL Trees (10.2) AVL Trees AVL Trees (0.) CSE 0 Winter 0 8 February 0 AVL 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

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

UNIT III BALANCED SEARCH TREES AND INDEXING

UNIT III BALANCED SEARCH TREES AND INDEXING UNIT III BALANCED SEARCH TREES AND INDEXING OBJECTIVE The implementation of hash tables is frequently called hashing. Hashing is a technique used for performing insertions, deletions and finds in constant

More information

Multi-way Search Trees

Multi-way Search Trees Multi-way Search Trees Kuan-Yu Chen ( 陳冠宇 ) 2018/10/24 @ TR-212, NTUST Review Red-Black Trees Splay Trees Huffman Trees 2 Multi-way Search Trees. Every node in a binary search tree contains one value and

More information

Red-black trees (19.5), B-trees (19.8), trees

Red-black trees (19.5), B-trees (19.8), trees Red-black trees (19.5), B-trees (19.8), 2-3-4 trees Red-black trees A red-black tree is a balanced BST It has a more complicated invariant than an AVL tree: Each node is coloured red or black A red node

More information

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25 Multi-way Search Trees (Multi-way Search Trees) Data Structures and Programming Spring 2017 1 / 25 Multi-way Search Trees Each internal node of a multi-way search tree T: has at least two children contains

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

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

Search Trees. Data and File Structures Laboratory.  DFS Lab (ISI) Search Trees 1 / 17 Search Trees Data and File Structures Laboratory http://www.isical.ac.in/~dfslab/2017/index.html DFS Lab (ISI) Search Trees 1 / 17 Binary search trees. Definition. Binary tree in which following property

More information

Background: disk access vs. main memory access (1/2)

Background: disk access vs. main memory access (1/2) 4.4 B-trees Disk access vs. main memory access: background B-tree concept Node structure Structural properties Insertion operation Deletion operation Running time 66 Background: disk access vs. main memory

More information

Trees. Eric McCreath

Trees. Eric McCreath Trees Eric McCreath 2 Overview In this lecture we will explore: general trees, binary trees, binary search trees, and AVL and B-Trees. 3 Trees Trees are recursive data structures. They are useful for:

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

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level. B Tree If there is just one item in the node, then the B Tree is organised as a binar search tree: all items in the left sub tree must be less than the item in the node, and all items in the right sub

More information

Data Structure - Binary Tree 1 -

Data Structure - Binary Tree 1 - Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists

More information

CHAPTER 10 AVL TREES. 3 8 z 4

CHAPTER 10 AVL TREES. 3 8 z 4 CHAPTER 10 AVL TREES v 6 3 8 z 4 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM NANCY

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

C SCI 335 Software Analysis & Design III Lecture Notes Prof. Stewart Weiss Chapter 4: B Trees

C SCI 335 Software Analysis & Design III Lecture Notes Prof. Stewart Weiss Chapter 4: B Trees B-Trees AVL trees and other binary search trees are suitable for organizing data that is entirely contained within computer memory. When the amount of data is too large to fit entirely in memory, i.e.,

More information

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height = M-ary Search Tree B-Trees Section 4.7 in Weiss Maximum branching factor of M Complete tree has height = # disk accesses for find: Runtime of find: 2 Solution: B-Trees specialized M-ary search trees Each

More information

Multiway Search Trees. Multiway-Search Trees (cont d)

Multiway Search Trees. Multiway-Search Trees (cont d) Multiway Search Trees Each internal node v of a multi-way search tree T has at least two children contains d-1 items, where d is the number of children of v an item is of the form (k i,x i ) for 1 i d-1,

More information

Search Trees. Undirected graph Directed graph Tree Binary search tree

Search Trees. Undirected graph Directed graph Tree Binary search tree Search Trees Undirected graph Directed graph Tree Binary search tree 1 Binary Search Tree Binary search key property: Let x be a node in a binary search tree. If y is a node in the left subtree of x, then

More information

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 and External Memory 1 1 (2, 4) Trees: Generalization of BSTs Each internal node

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

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

Lecture: Analysis of Algorithms (CS )

Lecture: Analysis of Algorithms (CS ) Lecture: Analysis of Algorithms (CS583-002) Amarda Shehu Fall 2017 1 Binary Search Trees Traversals, Querying, Insertion, and Deletion Sorting with BSTs 2 Example: Red-black Trees Height of a Red-black

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

Analysis of Algorithms

Analysis of Algorithms Analysis of Algorithms Concept Exam Code: 16 All questions are weighted equally. Assume worst case behavior and sufficiently large input sizes unless otherwise specified. Strong induction Consider this

More information

Multi-Way Search Tree

Multi-Way Search Tree Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) Rule: Number of children = 1

More information

COMP : Trees. COMP20012 Trees 219

COMP : Trees. COMP20012 Trees 219 COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural

More information

Data Structure: Search Trees 2. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Data Structure: Search Trees 2. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Data Structure: Search Trees 2 2017 Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering Search Trees Tree data structures that can be used to implement a dictionary, especially an ordered

More information

B-Trees and External Memory

B-Trees and External Memory Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 B-Trees and External Memory 1 (2, 4) Trees: Generalization of BSTs Each internal

More information

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in

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

COMS 3137 Class Notes

COMS 3137 Class Notes COMS 3137 Class Notes 1 AVL Trees When we do an insert into a Binary Search Tree (BST), we can never be sure how balanced the tree will be, since the order of insertions will determine this. A solution

More information

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1 (2,4) Trees 9 2 5 7 10 14 (2,4) Trees 1 Multi-Way Search Tree ( 9.4.1) A multi-way search tree is an ordered tree such that Each internal node has at least two children and stores d 1 key-element items

More information

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss) M-ary Search Tree B-Trees (4.7 in Weiss) Maximum branching factor of M Tree with N values has height = # disk accesses for find: Runtime of find: 1/21/2011 1 1/21/2011 2 Solution: B-Trees specialized M-ary

More information

Data Structure. Chapter 10 Search Structures (Part II)

Data Structure. Chapter 10 Search Structures (Part II) Data Structure Chapter 1 Search Structures (Part II) Instructor: ngela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 29 Spring Outline VL trees Introduction

More information

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is? Multiway searching What do we do if the volume of data to be searched is too large to fit into main memory Search tree is stored on disk pages, and the pages required as comparisons proceed may not be

More information

Design and Analysis of Algorithms Lecture- 9: B- Trees

Design and Analysis of Algorithms Lecture- 9: B- Trees Design and Analysis of Algorithms Lecture- 9: B- Trees Dr. Chung- Wen Albert Tsao atsao@svuca.edu www.408codingschool.com/cs502_algorithm 1/12/16 Slide Source: http://www.slideshare.net/anujmodi555/b-trees-in-data-structure

More information

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search. BST Deletion Deleting a value from a Binary Search Tree is a bit more complicated than inserting a value, but we will deal with the steps one at a time. First, we need to find the value which is easy because

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

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

Multi-Way Search Trees

Multi-Way Search Trees Multi-Way Search Trees Manolis Koubarakis 1 Multi-Way Search Trees Multi-way trees are trees such that each internal node can have many children. Let us assume that the entries we store in a search tree

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

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CMPS 2200 Fall 2017 Red-black trees Carola Wenk CMPS 2200 Fall 2017 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/13/17 CMPS 2200 Intro. to Algorithms 1 Dynamic Set A dynamic set, or dictionary, is a

More information

Multi-Way Search Trees

Multi-Way Search Trees Multi-Way Search Trees Manolis Koubarakis 1 Multi-Way Search Trees Multi-way trees are trees such that each internal node can have many children. Let us assume that the entries we store in a search 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

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

CS350: Data Structures Binary Search Trees

CS350: Data Structures Binary Search Trees Binary Search Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary search tree is a binary tree that

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