E.G.M. PetrakisTrees in Main Memory1 Balanced BST  Balanced BSTs guarantee O(logN) performance at all times  the height or left and right sub-trees are.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
1 Lecture 12 AVL Trees. 2 trees static dynamic game treessearch trees priority queues and heaps graphs binary search trees AVL trees 2-3 treestries Huffman.
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.
1 AVL-Trees (Adelson-Velskii & Landis, 1962) In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
CS202 - Fundamental Structures of Computer Science II
CS Data Structures Chapter 10 Search Structures (Selected Topics)
CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties and maintenance.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
E.G.M. Petrakissearching1 Searching  Find an element in a collection in the main memory or on the disk  collection: (K 1,I 1 ),(K 2,I 2 )…(K N,I N )
Chapter 4: Trees Binary Search Trees
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
Splay Trees Splay trees are binary search trees (BSTs) that:
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
CS Data Structures Chapter 10 Search Structures.
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
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.
© 2004 Goodrich, Tamassia Binary Search Trees1 CSC 212 Lecture 18: Binary and AVL Trees.
CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.
Chapter 10: Search Trees Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures AVL Trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 20 AVL Trees.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Balanced Binary Search Trees
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.
Balancing Binary Search Trees
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Balanced Binary Search Trees
Introduction Applications Balance Factor Rotations Deletion Example
Chapter 26 AVL Trees Jung Soo (Sue) Lim Cal State LA.
Chapter 29 AVL Trees.
Splay Trees Binary search 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.
CS202 - Fundamental Structures of Computer Science II
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE 373: Data Structures and Algorithms
AVL Trees CSE 373 Data Structures.
AVL Search Tree put(9)
Lecture No.20 Data Structures Dr. Sohail Aslam
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
short illustrative repetition
AVL Tree By Rajanikanth B.
AVL-Trees (Part 1).
AVL Tree Chapter 6 (cont’).
CSE 373 Data Structures Lecture 8
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

E.G.M. PetrakisTrees in Main Memory1 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: dynamic environment  optimal trees: static environment

E.G.M. PetrakisTrees in Main Memory2 AVL Trees  AVL ( Adelson, Lelskii, Landis): the height of the left and right subtrees differ by at most 1  the same for every subtree  Number of comparisons for membership operations  best case: completely balanced  worst case: 1.44 log(N+2)  expected case: logN +.25 <=

E.G.M. PetrakisTrees in Main Memory3 AVL Trees  “—” : completely balanced sub-tree  “/” : the left sub-tree is 1 higher  “\” : the right sub-tree is 1 higher

E.G.M. PetrakisTrees in Main Memory4 AVL Trees

E.G.M. PetrakisTrees in Main Memory5 “//” : the left sub-tree is 2 higher “\\” : the right sub-tree is 2 higher critical node Non AVL Trees

E.G.M. PetrakisTrees in Main Memory6 Single Right Rotations  Insertions or deletions may result in non AVL trees => apply rotations to balance the tree T3T3 T1T1T2 Α Β T1T1 T3T3 Α Β

E.G.M. PetrakisTrees in Main Memory7 T1T1 Α Β Α T3T3T2 ΑB ΑΑ T3T3 T1T2 Single Left Rotations

E.G.M. PetrakisTrees in Main Memory single right rotation 2 1 insert insert single left rotation

E.G.M. PetrakisTrees in Main Memory9 Double Left Rotation  Composition of two single rotations (one right and one left rotation) ΑΒ T4 Α A C T1 ΑB T2 T3T3 or T4 Α Β C Α T1T1 or Τ2 T3T3 Α A T3T3 or ΑC Τ1 T4T4T2T2  

E.G.M. PetrakisTrees in Main Memory10 Example of Double Left Rotation \\ / / == ==   Critical node insert 6

E.G.M. PetrakisTrees in Main Memory11 Double Right Rotation  Composition of two single rotations (one left and one right rotation) ΑB ΑΑ T4T4 T1T1 ΑΒ T2 T3T3 or ΑC ΑB T4T4 or A T1 T3 Α C Β T3T3 or ΑA Τ4Τ4Τ4Τ4 T2T2 T1  

E.G.M. PetrakisTrees in Main Memory12 Insertion (deletion) in AVL 1.Follow the search path to verify that the key is not already there 2.Insert (delete) the key 3.Retreat along the search path and check the balance factor 4.Rebalance if necessary (see next)

E.G.M. PetrakisTrees in Main Memory13 Rebalancing  For every node reached coming up from its left sub-tree after insertion readjust balance factor  ‘=’ becomes ‘/’ => no operation  ‘\’ becomes ‘=’ => no operation  ‘/’ becomes ‘//’ => must be rebalanced!!  The “//” node becomes a “critical node”  Only the path from the critical node to the leaf has to be rebalanced!!  Rotation is applied only at the critical node!

E.G.M. PetrakisTrees in Main Memory14 Rebalancing (cont.)  The balance factor of the critical node determines what rotation is to take place  single or double  If the child and the grand child (inserted node) of the critical node are on the same direction (both “/”) => single rotation  Else => double rotation  Rebalance similarly if coming up from the right sub-tree (opposite signs)

E.G.M. PetrakisTrees in Main Memory15 Performance  Performance of membership operations on AVL trees:  easy for the worst case!  An AVL tree will never be more than 45% higher that its perfectly balanced counterpart (Adelson, Velskii, Landis):  log(N+1) <= h b (N) <= l.4404log(N+2) – 0.302

E.G.M. PetrakisTrees in Main Memory16 Worst case AVL  Sparse tree => each sub-tree has minimum number of nodes

E.G.M. PetrakisTrees in Main Memory17 Fibonacci Trees  T h : tree of height h  T h has two sub-trees, one with height h-1 and one with height h-2  else it wouldn’t have minimum number of nodes  T 0 is the empty sub-tree (also Fibonacci)  T 1 is the sub-tree with 1 node (also Fibonacci)

E.G.M. PetrakisTrees in Main Memory18 Fibonacci Trees (cont.)  Average height  N h number of nodes of T h  N h = N h-1 + N h  N 0 = 1  N 1 = 2  From which h <= 1.44 log(N+1)

E.G.M. PetrakisTrees in Main Memory19 single rotation insert More Examples

E.G.M. PetrakisTrees in Main Memory20 double rotation insert Examples (cont.)

E.G.M. PetrakisTrees in Main Memory21 double rotation insert Examples (cont.)

E.G.M. PetrakisTrees in Main Memory22 delete single rotation Examples (cont.)

E.G.M. PetrakisTrees in Main Memory delete single rotation Examples (cont.)

E.G.M. PetrakisTrees in Main Memory delete double rotation Examples (cont.)

E.G.M. PetrakisTrees in Main Memory delete delete 8 General Deletions

E.G.M. PetrakisTrees in Main Memory26 delete 8 delete delete General Deletions (cont.)

E.G.M. PetrakisTrees in Main Memory27 delete General Deletions (cont.)

E.G.M. PetrakisTrees in Main Memory28 Self Organizing Search  Splay Trees: adapt to query patterns  move to root (front): whenever a node is accessed move it to the root using rotations  equivalent to move-to-front in arrays  current node  insertions: inserted node  deletions: father of deleted node or null if this is the root  membership: the last accessed node

E.G.M. PetrakisTrees in Main Memory Search(10) first rotation second rotation third rotation

E.G.M. PetrakisTrees in Main Memory30 Splay Cases  If the current node q has no grandfather but it has father p => only one single rotation –two symmetric cases: L, R q a bc q ab c p p

E.G.M. PetrakisTrees in Main Memory31 gp q p q a b cd p ab c d LL gp p q a bc d p q a b c d RL LL symmetric of RR RL symmetric of RL  If p has also grandfather qp => 4 cases

E.G.M. PetrakisTrees in Main Memory32 1 aq 8 j i 2 7 h 6 g 3 c 4 d 5 e f current node 1 αq 8 j i 2 7 h 6 g c 4 d 5 e f 3 RR LL

E.G.M. PetrakisTrees in Main Memory33 1 aq 8 j i g 3 c 4 5 e f b LR 1 αq 8 j i h 3 c 4 5 e f d g RL a, b, c … are sub-trees

E.G.M. PetrakisTrees in Main Memory34 1 a q 8j i h 3 c 4 5 e f b g d

E.G.M. PetrakisTrees in Main Memory35 Splay Performance  Splay trees adapt to unknown or changing probability distributions  Splay trees do not guarantee logarithmic cost for each access  AVL trees do!  asymptotic cost close to the cost of the optimal BST for unknown probability distributions  It can be shown that the “cost of m operations on an initially empty splay tree, where n are insertions is O(mlogn) in the worst case”

E.G.M. PetrakisTrees in Main Memory36 Optimal BST  Static environment: no insertions or deletions  Keys are accessed with various frequencies  Have the most frequently accessed keys near the root  Application: a symbol table in main memory

E.G.M. PetrakisTrees in Main Memory37 Searching  Given symbols a 1 < a 2 < ….< a n and their probabilities: p 1, p 2, … p n minimize cost  Successful search  Transform unsuccessful to successful  consider new symbols E 1, E 2, … E n -  … α 1 … α 2 …α i … α i + 1 ….…α n … α n + 1 E0E0 E1E1 E2E2 EiEi EnEn E i = (α i, α i + 1 ) E 0 = (- , α 1 )E n = (α n,  )

E.G.M. PetrakisTrees in Main Memory38 anan a n- 1 a n-2 EiEiEiEi failure node a n-3 unsuccessful search for all values in E i terminates on the same failure node (in fact, one node higher) Unsuccessful Search

E.G.M. PetrakisTrees in Main Memory39 (a 1, a 2, a 3 ) = (do, if, read) p i = q i = 1/7read read read if if if do do do cost = 15/7 cost = 13/7 cost = 15/7 Optimal BST if Example

E.G.M. PetrakisTrees in Main Memory40 read if do if read do cost = 15/7

E.G.M. PetrakisTrees in Main Memory41 Search Cost  If p i is the probability to search for a i and q i is the probability to search in E i then successful search unsuccessful search

E.G.M. PetrakisTrees in Main Memory42  In a BST, a subtree has nodes that are consecutive in a sorted sequence of keys (e.g. [5,26]) Observation 1

E.G.M. PetrakisTrees in Main Memory43  If T ij is a sub-tree of an optimal BST holding keys from i to j then  T ij must be optimal among all possible BSTs that store the same keys  optimality lemma: all sub-trees of an optimal BST are also optimal Observation 2

E.G.M. PetrakisTrees in Main Memory44 Optimal BST Construction 1)Construct all possible trees: NP-hard, there are trees!! 2)Dynamic programming solves the problem in polynomial time O(n 3 )  at each step, the algorithm finds and stores the optimal tree in each range of key values  increases the size of the range at each step until the whole range is obtained

E.G.M. PetrakisTrees in Main Memory45 Example (successful search only): 1)BSTs with1 node range cost= )BSTs with2 nodesk=1-10 k=10-20 k=20-20 range 1-10range 20-40range cost= =0.7 cost= = cost= ,3.2=0.8cost= =0.5 optimal keys probabilities0,30,20,10,4 cost= =0.9 cost= =0.6 optimal

E.G.M. PetrakisTrees in Main Memory46 k=1-20 range cost= = cost=0.2+2( )= k=10-40 range cost= =1.3 cost= = cost=0.1+2( )= cost= =1.1 optimal 3)BSTs with3 nodes

E.G.M. PetrakisTrees in Main Memory47 4)BSTs with4 nodes range cost= = cost=0.2+2( )+3 0.1= OPTIMAL BST cost=0.1+2( )+3 0.2=2.1 cost= =2

E.G.M. PetrakisTrees in Main Memory48 Complexity  Compute all optimal BSTs for all C ij, i,j=1,2..n  Let m=j-i: number of keys in range C ij  n-m-1 C ij ’s must be computed  The one with the minimum cost must be found, this takes O(m(n-m-1)) time  For all C ij ’s it takes  There is a better O(n 2 ) algorithm by Knuth  There is also a O(n) greedy algorithm

E.G.M. PetrakisTrees in Main Memory49 Optimal BSTs  High probability keys should be near the root  But, the value of a key is also a factor  It may not be desirable to put the smallest or largest key close to the root => this may result in skinny trees (e.g., lists)