Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

AVL Trees binary tree for every node x, define its balance factor
Part II. Delete an Node from an AVL Tree Consider to delete
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Dynamic Dictionaries Primary Operations:  Get(key) => search  Insert(key, element) => insert  Delete(key) => delete Additional operations:  Ascend()
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
Red Black Trees Colored Nodes Definition Binary search tree.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get,
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
CS Data Structures Chapter 10 Search Structures.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Balanced Trees. Maintaining Balance Binary Search Tree – Height governed by Initial order Sequence of insertion/deletion – Changes occur at leaf nodes.
Binary Tree Representation Of Trees Problems with trees. 2- and 3-nodes waste space. Overhead of moving pairs and pointers when changing among.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Binary Search Tree 황승원 Fall 2011 CSE, POSTECH 2 2 Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
M-ary Trees. m-ary trees Some trees need to be searched efficiently, but have more than two children l parse trees l game trees l genealogical trees,
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
Dynamic Dictionaries Primary Operations:  get(key) => search  put(key, element) => insert  remove(key) => delete Additional operations:  ascend()
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
Lecture 23 Red Black Tree Chapter 10 of textbook
AVL Trees CSE, POSTECH.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AA Trees.
Balanced Binary Search Trees
G64ADS Advanced Data Structures
Red Black Trees Colored Nodes Definition Binary search tree.
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Red Black Trees Colored Nodes Definition Binary search tree.
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Chapter 29 AVL Trees.
Balanced Trees (AVL and RedBlack)
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
Balanced Binary Search Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
Dynamic Dictionaries Primary Operations: Additional operations:
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL Tree By Rajanikanth B.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CSE 373 Data Structures Lecture 8
Red Black Trees Colored Nodes Definition Binary search tree.
Presentation transcript:

Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH

2 Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get, put, and remove take O(log n) time

3 Balanced Binary Search Trees Indexed AVL trees Indexed red-black trees Indexed operations also take O(log n) time

4 Balanced Search Trees weight balanced binary search trees 2-3 & trees AA trees B-trees BBST etc.

5 5 AVL Tree Definition Binary tree. If T is a nonempty binary tree with T L and T R as its left and right subtrees, then T is an AVL tree iff 1. T L and T R are AVL trees, and 2. |h L – h R |  1 where h L and h R are the heights of T L and T R, respectively

6 Balance Factors This is an AVL tree.

7 Height The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). N h =N h-1 + N h-2 +1 – reminds you anything? F n =F n-1 + F n-2 N h =F h+2 -1 when

8 AVL Search Tree

9 put(9)

10 put(29) RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)

11 put(29) RR rotation

12 AVL Rotations RR LL RL LR

13 Imbalance Types After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types 1. LL: new node is in the left subtree of the left subtree of A 2. LR: new node is in the right subtree of the left subtree of A 3. RR: new node is in the right subtree of the right subtree of A 4. RL: new node is in the left subtree of the right subtree of A

14 Rotation Definition To switch children and parents among two or three adjacent nodes to restore balance of a tree. A rotation may change the depth of some nodes, but does not change their relative ordering.

15 AVL Rotations To balance an unbalanced AVL tree (after an insertion), we may need to perform one of the following rotations: LL, RR, LR, RL

16 An LL Rotation

17 An LR Rotation

18 RR and RL? Symmetric!

19 Inserting into an AVL Search Tree 29 Insert(29) Where is 29 going to be inserted into? After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

20 Inserting into an AVL Search Tree What are the balance factors for 20, 25, 29? -2 0 Insert(29) RR imbalance  new node is in the right subtree of right subtree of node 20 (node with bf = -2)

21 After RR Rotation What would the left subtree of 30 look like after an RR rotation? After the RR rotation, is the resulting tree an AVL search tree now?

22 Deletion from an AVL Search Tree Deletion of a node may also produce an imbalance Imbalance incurred by deletion is classified into the types R0, R1, R-1, L0, L1, and L-1 Rotation is also needed for rebalancing Read the observations after deleting a node from an AVL search tree Read Section for Deletion from an AVL search tree

23 An R0 Rotation

24 An R1 Rotation

25 An R-1 Rotation

26 Complexity of Dictionary Operations: Search, Insert, and Delete Data StructureWorst CaseExpected Hash Table Binary Search Tree Balanced Binary Search Tree O(n) O(log n) O(1) O(log n) n is the number of elements in dictionary.

27 Complexity of Other Operations Ascend, Search(rank), and Delete(rank) Data Structureascendget and remove Hash Table Indexed BST Indexed Balanced BST O(D + n log n) O(n) O(D + n log n) O(n) O(log n) D is the number of buckets.

28 Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes

29 Example Red Black Tree

30 Properties The height of a red black tree that has n (internal) nodes is between log 2 (n+1) and 2log 2 (n+1).

31 Lemma 16.2 r : rank of root

32 Insert New pair is placed in a new node, which is inserted into the red-black tree. New node color options. – Black node => one root-to-external-node path has an extra black node (black pointer). Hard to remedy. – Red node => one root-to-external-node path may have two consecutive red nodes (pointers). May be remedied by color flips and/or a rotation.

33 Classification Of 2 Red Nodes/Pointers XYz – X => relationship between gp and pp. pp left child of gp => X = L. – Y => relationship between pp and p. p right child of pp => Y = R. – z = b (black) if d = null or a black node. – z = r (red) if d is a red node. ab c d gp pp p

34 XYr Color flip. ab c d gp pp p ab c d gp pp p Continue rebalancing if changing gp to red causes problems

35 LLb Rotate. Done! Same as LL rotation of AVL tree. y x ab z cd ab c d gp pp p x y z

36 LRb Rotate. Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric. y x ab z cd bc a d gp pp p y x z

37 Red-black Tree Animation k/redblack.html k/redblack.html java.util.TreeMap => red black tree

38 Coming Up Next READING: Ch 16.1~2 NEXT: Graphs (Ch 17.1~7)