Docsity

Advanced Tree Structures: Binary Search Trees and AVL Trees, Slides of Java Programming

Toggle sidebar
bg1
Advanced Tree Structures
CalinC urescu, some material from Goodrich & Tamas sia
Advanced Tree Structures
Calin Curescu, some material from Goodrich & Tamassia
12 pages
TDDC32 lecture 3, 2006
Advanced Tree Structures
CalinC urescu, some material from Goodrich & Tamas sia
2of 12
TDDC32 lecture 3, 2006
Binary Search Trees
•Root
– First node in a tree
•Grade
– Of a node is the number of children
– Of a tree is the largest grade of any node
– Of a binary tree is 2
•Leaf (or external)
– A node that has no children
•Inner node (or internal)
– A node that is not a leaf
•Level
– distance (# of traversed nodes) from root
– Level of root = 0
•Height
– longest distance to a leaf
– Height of a leaf = 0
Advanced Tree Structures
CalinC urescu, some material from Goodrich & Tamas sia
3of 12
TDDC32 lecture 3, 2006
BST continued
•Traversing
– Preorder
– Inorder
– Postorder
•Complexity of Insertion, Deletion
– Average case
– Worst case
•Sorting
– What kind of traversal do I need?
O(log(n))
O(n) – real problem!!!
Advanced Tree Structures
CalinC urescu, some material from Goodrich & Tamas sia
4of 12
TDDC32 lecture 3, 2006
AVL Trees
•AVL – Adelson-Velskii and Landis
– the 2 guys that invented them
•What is a balanced tree?
– Node balance
• difference of the number of nodes in left and right subtree
is at most 1
– Height balance
• difference of height of left and right subtree is at most 1
•AVL Tree – height-balanced binary tree
Advanced Tree Structures
CalinC urescu, some material from Goodrich & Tamas sia
5of 12
TDDC32 lecture 3, 2006
Example
•Height of the nodes
– Given by the number outside the circle in the graph above
•Note about the course book: They use empty nodes at the lea ves
to make it easier for programming.
– Increases the height with 1
88
44
17 78
32 50
48 62
2
4
1
1
2
3
1
1
Advanced Tree Structures
CalinC urescu, some material from Goodrich & Tamas sia
6of 12
TDDC32 lecture 3, 2006
What is the maximum height for n nodes?
•Fibonacci Tree (worst balanced AVL)
–F
0is the empty tree, F1is the single node
–F
n= Fn-1 + • + Fn-2
– At most 44% larger height compared to a perfectly balanced
tree with the same number of nodes
•The height of an AVL tree storing n keys is O(log n)
pf3

Partial preview of the text

Download Advanced Tree Structures: Binary Search Trees and AVL Trees and more Slides Java Programming in PDF only on Docsity!

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia

Advanced Tree Structures

Calin Curescu, some material from Goodrich & Tamassia

TDDC32 lecture 3, 200612 pages Advanced Tree StructuresCalin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 3, 20062 of 12

Binary Search Trees

  • Root
    • First node in a tree
  • Grade
    • Of a node is the number of children
    • Of a tree is the largest grade of any node
    • Of a binary tree is 2
  • Leaf (or external)
    • A node that has no children
  • Inner node (or internal)
    • A node that is not a leaf
  • Level
    • distance (# of traversed nodes) from root
    • Level of root = 0
  • Height
    • longest distance to a leaf
    • Height of a leaf = 0

Advanced Tree StructuresCalin Curescu, some material from Goodrich & Tamassia 3 of 12 TDDC32 lecture 3, 2006

BST continued

  • Traversing
    • Preorder
    • Inorder
    • Postorder
  • Complexity of Insertion, Deletion
    • Average case
    • Worst case
  • Sorting
    • What kind of traversal do I need?

O(log(n))

O(n) – real problem!!!

Advanced Tree StructuresCalin Curescu, some material from Goodrich & Tamassia 4 of 12 TDDC32 lecture 3, 2006

AVL Trees

  • AVL – Adelson-Velskii and Landis
    • the 2 guys that invented them
  • What is a balanced tree?
    • Node balance
      • difference of the number of nodes in left and right subtree is at most 1
    • Height balance
      • difference of height of left and right subtree is at most 1
  • AVL Tree – height-balanced binary tree

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia

5 of 12 TDDC32 lecture 3, 2006

Example

  • Height of the nodes
    • Given by the number outside the circle in the graph above
  • Note about the course book: They use empty nodes at the leaves to make it easier for programming. - Increases the height with 1

88

44

17 78

32 50

48 62

2

4

1

1

2

3

1

1

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia

6 of 12 TDDC32 lecture 3, 2006

What is the maximum height for n nodes?

  • Fibonacci Tree (worst balanced AVL)
    • F 0 is the empty tree, F 1 is the single node
    • F (^) n = F (^) n-1 + • + F (^) n-
    • At most 44% larger height compared to a perfectly balanced tree with the same number of nodes
  • The height of an AVL tree storing n keys is O(log n)

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 3, 20067 of 12

Proving max height

  • Let us bound n(h): the minimum number of internal nodes of an AVL tree of height h.
  • We easily see that n(1) = 1 and n(2) = 2
  • For n > 2, an AVL tree of height h contains the root node, one AVL subtree of height n-1 and another of height n-2.
  • That is, n(h) = 1 + n(h-1) + n(h-2)
  • Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So n(h) > 2n(h-2), n(h) > 4n(h-4), … (by induction), Æ n(h) > 2i^ n(h-2i)
  • Solving the base case we get: n(h) > 2 h/2-
  • Taking logarithms: h < 2log n(h) +
  • Thus the height of an AVL tree is O(log n)

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia TDDC32 lecture 3, 20068 of 12

Insertion and Deletion

  • Insertion & Deletion
    • 1st^ step – like for every other binary tree
      • May cause the tree to become imbalanced
    • 2nd^ step – rebalance tree – performing rotations

Advanced Tree StructuresCalin Curescu, some material from Goodrich & Tamassia 9 of 12 TDDC32 lecture 3, 2006

Rebalancing

  • Definitions:
    • Let w be the new inserted node or the parent of a deleted node
    • Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height
    • Let a, b, c be the inorder (sorted) listing of x, y, z
  • Mechanism
    • Set b as the root node of the subtree (instead of z)
    • Set a as the left child, c as the right child of b
    • Redistribute the 4 subtrees of x, y, and z to a and c in the correct order - The subtrees are defined by the children of x,y,z other than x and y

Advanced Tree StructuresCalin Curescu, some material from Goodrich & Tamassia 10 of 12 TDDC32 lecture 3, 2006

Single rotations

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia

11 of 12 TDDC32 lecture 3, 2006

Double rotations

Advanced Tree Structures Calin Curescu, some material from Goodrich & Tamassia

12 of 12 TDDC32 lecture 3, 2006

Insertion vs. Deletion

  • Insertion
    • If tree imbalanced one rotation locally and globally balances the tree
  • Deletion
    • If tree imbalanced, one rotation only locally balances the tree, and we have to check all the ancestors of the deleted tree
    • Tip : to know how to do the rebalancing in the case of a deletion, regard it as an insertion in the other subtree of the node that becomes imbalanced