For Friday Finish Weiss, chapter 4 No homework Program 2 due - - PowerPoint PPT Presentation

for friday
SMART_READER_LITE
LIVE PREVIEW

For Friday Finish Weiss, chapter 4 No homework Program 2 due - - PowerPoint PPT Presentation

For Friday Finish Weiss, chapter 4 No homework Program 2 due Programming Assignment 2 Any questions? Binary Search Tree Operations Search Time Complexity Insert Time Complexity Deletion Of a leaf Of a


slide-1
SLIDE 1

For Friday

  • Finish Weiss, chapter 4
  • No homework
  • Program 2 due
slide-2
SLIDE 2

Programming Assignment 2

  • Any questions?
slide-3
SLIDE 3

Binary Search Tree Operations

  • Search

– Time Complexity

  • Insert

– Time Complexity

  • Deletion

– Of a leaf – Of a node with one non-empty subtree – Of a node with two non-empty subtrees – Time Complexity

  • Output Ordered List
  • FindMin
  • Find Max
slide-4
SLIDE 4

Problem With Binary Trees

  • Basic operations are O(h)
  • What is the height?
  • Best case --
  • Worst case --
slide-5
SLIDE 5

Solution

  • How we can solve the performance

problems of binary search trees?

slide-6
SLIDE 6

AVL Search Trees

  • An AVL tree is a binary tree such that:

– the difference between the heights of the left subtree and the right subtree is no more than

  • ne

– the left subtree and the right subtree are AVL trees

  • An AVL search tree is simply a binary

search tree which is also an AVL tree

slide-7
SLIDE 7

Maintaining the AVL Properties

  • Each node has a balance factor associated

with it -- 0, 1, or -1

  • If the nodes subtrees have equal heights, the

balance factor is 0

  • If the right subtree has greater height (by 1)

the balance factor is -1

  • If the left subtree has greater height (by 1)

the balance factor is 1

slide-8
SLIDE 8

Binary Search Tree Operations

  • Search

– Time Complexity

  • Insertion

– May have to rotate – Time Complexity

  • Deletion

– May have to rotate up to log(n) times – Time Complexity

slide-9
SLIDE 9

Splay Trees

  • Interested in the cost of a sequence of

search operations rather than the cost of a single search.

  • We want to make sure that the amortized

cost of M search operations is M log N.

slide-10
SLIDE 10

Basic Idea

  • When we find a node, we’re going to rotate

it to the top in a way that helps to balance the tree if it is currently unbalanced.

slide-11
SLIDE 11

Cases

  • Found node has no grandparent: rotate node

and root

  • Found node has a grandparent:

– zig-zig case (parent is same side of grandparent that node is of root): rotate node and grandparent – zig-zag case (node’s value is in-between value

  • f parent and grandparent): do a standard AVL

double rotation

slide-12
SLIDE 12

Comparison

  • Splay trees and AVL trees