Balanced BST

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

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. AVL Trees. - PowerPoint PPT Presentation

Text of Balanced BST

  • Balanced BST Balanced BSTs guarantee O(logN) performance at all timesthe height or left and right sub-trees are about the samesimple BST are O(N) in the worst case Categories of BSTsAVL, SPLAY trees: dynamic environmentoptimal trees: static environment

    Trees in Main Memory

  • AVL TreesAVL (Adelson, Lelskii, Landis): the height of the left and right subtrees differ by at most 1the same for every subtreeNumber of comparisons for membership operationsbest case: completely balanced worst case: 1.44 log(N+2)expected case: logN + .25
  • AVL Trees : completely balanced sub-tree/ : the left sub-tree is 1 higher \ : the right sub-tree is 1 higher

    Trees in Main Memory

  • AVL Trees

    Trees in Main Memory

  • Non AVL Trees// : the left sub-tree is 2 higher \\ : the right sub-tree is 2 highercritical node

    Trees in Main Memory

  • Single Right RotationsInsertions or deletions may result in non AVL trees => apply rotations to balance the tree

    Trees in Main Memory

  • Single Left Rotations

    Trees in Main Memory

  • 42421single right rotation21 insert 148insert 948single left rotation84499

    Trees in Main Memory

  • Double Left RotationComposition of two single rotations (one right and one left rotation)

    Trees in Main Memory

  • Example of Double Left Rotationinsert 6

    Trees in Main Memory

  • Double Right RotationComposition of two single rotations (one left and one right rotation)

    Trees in Main Memory

  • Insertion (deletion) in AVLFollow the search path to verify that the key is not already there Insert (delete) the key Retreat along the search path and check the balance factor Rebalance if necessary (see next)

    Trees in Main Memory

  • RebalancingFor 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 nodeOnly the path from the critical node to the leaf has to be rebalanced!!Rotation is applied only at the critical node!

    Trees in Main Memory

  • Rebalancing (cont.)The balance factor of the critical node determines what rotation is to take placesingle or doubleIf the child and the grand child (inserted node) of the critical node are on the same direction (both /) => single rotationElse => double rotationRebalance similarly if coming up from the right sub-tree (opposite signs)

    Trees in Main Memory

  • PerformancePerformance 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)
  • Worst case AVLSparse tree => each sub-tree has minimum number of nodes

    Trees in Main Memory

  • Fibonacci TreesTh: tree of height hTh has two sub-trees, one with height h-1 and one with height h-2else it wouldnt have minimum number of nodesT0 is the empty sub-tree (also Fibonacci)T1 is the sub-tree with 1 node (also Fibonacci)

    Trees in Main Memory

  • Fibonacci Trees (cont.)Average heightNh number of nodes of ThNh = Nh-1 + Nh-2 + 1N0 = 1N1 = 2

    From which h

  • More Examplessingle rotation

    Trees in Main Memory

  • Examples (cont.) double rotation

    Trees in Main Memory

  • Examples (cont.) double rotation

    Trees in Main Memory

  • Examples (cont.)

    Trees in Main Memory

  • Examples (cont.)5single rotation

    Trees in Main Memory

  • Examples (cont.)5double rotation

    Trees in Main Memory

  • General Deletions

    Trees in Main Memory

  • General Deletions (cont.)

    Trees in Main Memory

  • General Deletions (cont.)

    Trees in Main Memory

  • Self Organizing SearchSplay Trees: adapt to query patternsmove to root (front): whenever a node is accessed move it to the root using rotationsequivalent to move-to-front in arrayscurrent nodeinsertions: inserted nodedeletions: father of deleted node or null if this is the rootmembership: the last accessed node

    Trees in Main Memory

  • Trees in Main Memory

  • Splay CasesIf the current node q has no grandfather but it has father p => only one single rotationtwo symmetric cases: L, R

    Trees in Main Memory

  • If p has also grandfather qp => 4 cases

    Trees in Main Memory

  • Trees in Main Memory

  • Trees in Main Memory

  • Trees in Main Memory

  • Splay PerformanceSplay trees adapt to unknown or changing probability distributionsSplay trees do not guarantee logarithmic cost for each accessAVL trees do!asymptotic cost close to the cost of the optimal BST for unknown probability distributionsIt 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

    Trees in Main Memory

  • Optimal BSTStatic environment: no insertions or deletionsKeys are accessed with various frequenciesHave the most frequently accessed keys near the rootApplication: a symbol table in main memory

    Trees in Main Memory

  • Searching Given symbols a1 < a2 < .< an and theirprobabilities: p1, p2, pn minimize costSuccessful searchTransform unsuccessful to successful consider new symbols E1, E2, En

    Trees in Main Memory

  • Unsuccessful Search

    Trees in Main Memory

  • Example

    Trees in Main Memory

  • Trees in Main Memory

  • Search CostIf pi is the probability to search for ai and qi is the probability to search in Ei then

    Trees in Main Memory

  • Observation 1In a BST, a subtree has nodes that are consecutive in a sorted sequence of keys (e.g. [5,26])

    Trees in Main Memory

  • Observation 2If Tij is a sub-tree of an optimal BST holding keys from i to j then Tij must be optimal among all possible BSTs that store the same keysoptimality lemma: all sub-trees of an optimal BST are also optimal

    Trees in Main Memory

  • Optimal BST ConstructionConstruct all possible trees: NP-hard, there are trees!!Dynamic programming solves the problem in polynomial time O(n3) at each step, the algorithm finds and stores the optimal tree in each range of key valuesincreases the size of the range at each step until the whole range is obtained

    Trees in Main Memory

  • Example (successful search only):BSTs with1 noderange 1102040cost= 0.30.20.10.4BSTs with2 nodesk=1-10 k=10-20 k=20-20range 1-10range 20-40range 10-2011010202040cost=0.3 1+0.2 2=0.7cost=0.2+0.1 2=0.410140202010cost=0.2 1+0,3.2=0.8cost=0.1+0.2 2=0.5optimalkeys1102040probabilities0,30,20,10,4cost=0.1+0.8=0.9cost=0.4+0.2=0.6optimaloptimal

    Trees in Main Memory

  • k=1-20range 1-2012010cost=0.1+2 0.3+3 0.2=1.312010cost=0.2+2(0.3+0.1)=111020k=10-40range 10-40104020cost=0.2+2 0.4+3 0.1=1.3cost=0.3+2 0.2+3 0.1=1201040cost=0.1+2(0.2+0.4)=1.3401020cost=0.4+2 0.2+30.1=1.1optimalBSTs with3 nodes

    Trees in Main Memory

  • BSTs with4 nodesrange 1-401401020cost=0.3+2 0.4+3 0.2+4 0.1=2.11014020cost=0.2+2(0.3+0.4)+3 0.1=1.920204040111010OPTIMAL BSTcost=0.1+2(0.3+0.4)+3 0.2=2.1cost=0.4+2 0.3+3 0.2+4 0.1=2

    Trees in Main Memory

  • ComplexityCompute all optimal BSTs for all Cij, i,j=1,2..nLet m=j-i: number of keys in range Cijn-m-1 Cijs must be computed The one with the minimum cost must be found, this takes O(m(n-m-1)) timeFor all Cijs it takesThere is a better O(n2) algorithm by Knuth There is also a O(n) greedy algorithm

    Trees in Main Memory

  • Optimal BSTsHigh probability keys should be near the rootBut, 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)

    Trees in Main Memory