Binary Search Tree Deletion and Heap Implementation, Slides of Data Structures and Algorithms

Notes from a computer science lecture on binary search tree deletion and heap implementation. It covers the different cases to consider when removing nodes from a binary search tree, including empty trees, nodes not in the tree, leaf nodes, nodes with one child, and nodes with two children. The document also discusses the implementation of a heap using an array and the basic heap operations of findmax, deletemax, and insert.

Typology: Slides

2012/2013

Uploaded on 09/09/2013

zaid
zaid 🇮🇳

4.5

(2)

59 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
10/21/10%
1%
CPSC 223
Algorithms & Data Abstract Structures
Lecture 15: !
Binary Search Tree Deletion (cont.), !
Treesort, and Heaps!
Today …
Assignments!
Assignment 6 due (don’t forget about the destructor!)!
Assignment 7!
Binary Search Tree Deletion (cont.)!
Treesort !!!!!!!!!![pp. 568-569]!
Heaps (if time) !!!!!!!![Sect. 11.2]!
CPSC%223%**%Fall%2010% 2%
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Binary Search Tree Deletion and Heap Implementation and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 15:

Binary Search Tree Deletion (cont.),

Treesort, and Heaps

Today …

• Assignments

  • Assignment 6 due (don’t forget about the destructor!)
  • Assignment 7

• Binary Search Tree Deletion (cont.)

• Treesort [pp. 568-569]

• Heaps (if time) [Sect. 11.2]

CPSC 223 -­‐-­‐ Fall 2010 2

CPSC 223 -­‐-­‐ Fall 2010 3

Binary Search Tree Deletion

Removing (Deleting) Nodes

  • Removing nodes is where things get tricky …
  • If we could restrict remove to just leaf nodes it

would be easy

  • For general removal, we need to consider some

special cases …

CPSC 223 -­‐-­‐ Fall 2010 4

Removing Nodes

  • Hardest Case : Remove a node with two children
    • Both children cannot be “adopted” by the parent CPSC 223 -­‐-­‐ Fall 2010 7 D B A C D A C

Remove B

Removing Nodes

  • To remove a node with two children
    • one solution is to not delete the node …
    • and instead replace the item in the node with another node’s item CPSC 223 -­‐-­‐ Fall 2010 8 D B A C D ? A C Remove B

Removing Nodes

Which item do we then use as a replacement?

  • There is more than one we can choose …
  • Either the left-most node of the right subtree
  • or the right-most node of the left subtree CPSC 223 -­‐-­‐ Fall 2010 9 D B A C D ? A C Remove B D C A Here, the right subtree does not have a left-most node

Removing Nodes

Which item do we then use as a replacement?

  • There is more than one we can choose …
  • Either the left-most node of the right subtree
  • or the right-most node of the left subtree CPSC 223 -­‐-­‐ Fall 2010 10 Remove B E B A D C F A more typical example right child le+-­‐most node E ? A D C F E C A D F

Removing Nodes

  • Remove the inorder successor …
    • It will either be a leaf or have one right child (why?)
    • Now we simply remove it … CPSC 223 -­‐-­‐ Fall 2010 13 E B G A C F H ? B G A C E H F Oops … E has a right child! copy item

Removing Nodes

Should we use recursion or iteration to find the inorder

successor?

  • Typically use iteration
  • By looping until the left most node is found
  • Recall : Navigating a path is easy using iteration (loops) CPSC 223 -­‐-­‐ Fall 2010 14

Exercise: Removing Nodes

  • Form groups of 2
    • Your job is to write down the steps for remove void remove(const Key& key)
    • Remove should delete the first occurrence of the key
    • Only need to consider the case where the node to remove has two children
    • But consider both sub-cases: (a) when the inorder successor has a child, and (b) when it does not CPSC 223 -­‐-­‐ Fall 2010 15 CPSC 223 -­‐-­‐ Fall 2010 16

Treesort

CPSC 223 -­‐-­‐ Fall 2010 19

Heaps

Heaps

  • A heap is another ADT
  • In the text …
    • Heaps are primarily introduced as a way to implement priority queues
    • Also used for heapsort
  • We will primarily discuss heaps for heapsort
  • But they are also a nice segue into balanced trees … CPSC 223 -­‐-­‐ Fall 2010 20

The Heap ADT

  • A heap is very similar to a binary search tree
    • In fact, we are only going to talk about binary heaps …
    • There are a number of variants however
  • Unlike a binary search tree, a heap is always a

complete tree!

  • How does it do this?
    • By relaxing the ordering constraint
    • … children of nodes have smaller values (max-heap)
    • thus, the root always has the max value CPSC 223 -­‐-­‐ Fall 2010 21 A min heap is just the opposite

The Heap ADT

A (binary) “ heap ” is a complete binary tree such that:

  • Every parent node has a larger search key value than its children (for max-heap)
  • Trivially, an empty tree is a heap CPSC 223 -­‐-­‐ Fall 2010 22 10 9 8 5 2 4 This is a heap 15 6 8 7 2 4 This is a not a heap … why? 8 3 6 1 4 This is a not a heap … why?

Array-Based Heap Implementation

  • We assume that Heap has data members: Entry items [MAXITEMS]; // array of heap items int size ; // number of items in the heap CPSC 223 -­‐-­‐ Fall 2010 25 10 9 8 5 2 4 10 9 8 5 2 4 0 1 2 3 4 5 6 we may have empty slots at the end … items = size = 6

Array-Based Heap Implementation

Entry Heap:: findMax () { if(! isEmpty ()) return items[0]; else … handle empty case … } CPSC 223 -­‐-­‐ Fall 2010 26 10 9 8 5 2 4 10 9 8 5 2 4 0 1 2 3 4 5 6 we may have empty slots at the end … items = size = 6

What is the worst case

Lme complexity of findMax?

Heap Implementation

Deleting the max key

1. Remove root

2. Promote last item to root (becomes a “ semiheap ”)

3. “ Trickle down ” the new root

a). If root’s search key smaller than largest child b). Then exchange (swap) them c). Trickle down new subtree root CPSC 223 -­‐-­‐ Fall 2010 27

Heap Implementation

Deleting the max key

CPSC 223 -­‐-­‐ Fall 2010 28 10 9 8 5 2 4 Delete 10 (root) 4 9 8 5 2 Promote last item to root 9 4 8 5 2 Trickle down 4 9 5 8 4 2 Trickle down 4 again

Array-Based Heap Implementation

Cost of deletion

• How many comparisons and swaps?

• Worst case is when we trickle down to a leaf node

  • There are O (log n ) such trickle-down steps … why?

• At each step we have

  • 2 (item) comparisons and 1 swap
  • Therefore, each trickle-down step is O (1)

• Deletion is worst case O (log n )

CPSC 223 -­‐-­‐ Fall 2010 31

Heap Implementation

Inserting a new item (… opposite of delete)

1. Add new item as leaf (maintain complete tree)

2. “ Trickle up ” the new root

a). If item’s search key is greater than root’s search key b). Then exchange (swap) them c). Trickle up new root CPSC 223 -­‐-­‐ Fall 2010 32 9 6 7 5 2 4 Insert 10 10 9 6 10 5 2 4 Trickle up 7 10 6 9 5 2 4 Trickle up 7

Array-Based Heap Implementation

void Heap:: insert (const Entry& newItem) { if(size < MAXITEMS – 1) { int loc = size; int parent = (loc – 1)/2; items[loc] = newItem; while(parent >= 0 && items[parent] < newItem) { swap (items[parent], items[loc]); loc = parent; parent = (loc – 1)/ } size++; } } CPSC 223 -­‐-­‐ Fall 2010 33

Array-Based Heap Implementation

Cost of insertion

  • How many comparisons and swaps?
  • The worst case is when we trickle up to the root
    • There are O (log n ) such trickle-up steps … why?
  • At each step we have
    • 1 (item) comparisons and 1 swap
    • Therefore, each trickle-up step is O (1)
  • Insertion is worst case O (log n ) CPSC 223 -­‐-­‐ Fall 2010 34