Binary Search Tree Remove Todays announcements: PA2 available soon - - PowerPoint PPT Presentation

binary search tree remove
SMART_READER_LITE
LIVE PREVIEW

Binary Search Tree Remove Todays announcements: PA2 available soon - - PowerPoint PPT Presentation

Binary Search Tree Remove Todays announcements: PA2 available soon Todays Plan Average height of BST Remove Warm up: Worst case time to perform find in a BST with n nodes: Worst case time to perform n inserts into an empty BST:


slide-1
SLIDE 1

Binary Search Tree Remove

Today’s announcements:

◮ PA2 available soon

Today’s Plan

◮ Average height of BST ◮ Remove

Warm up: Worst case time to perform find in a BST with n nodes: Worst case time to perform n inserts into an empty BST: Best case? Average case?

1 / 15

slide-2
SLIDE 2

Average depth in a BST

Choose a binary tree with n nodes,

  • 1. equally likely from all n node binary trees:

Average depth is Θ(√n).

  • 2. by inserting n items in random order into an empty BST:

Average depth is Θ(log n).

2 / 15

slide-3
SLIDE 3

Quicksort and inserting into an empty BST

Quicksort

  • 1. Pick an array element as the pivot
  • 2. Reorder the array so that all elements < pivot are to its left,

and all elements ≥ pivot are to its right.

  • 3. Recursively sort each partition.

The first pivot is compared with every other element. The first element inserted into an empty BST is compared to every

  • ther element.

Hmmm...

3 / 15

slide-4
SLIDE 4

Find node with minimum key in BST

Node *& findMin(Node *& root) { if( root == NULL || root->left == NULL ) return root; return findMin(root->left); }

4 / 15

slide-5
SLIDE 5

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

Remove a node from a Binary Search Tree

5 / 15

slide-6
SLIDE 6

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle

key

Remove a node from a Binary Search Tree

6 / 15

slide-7
SLIDE 7

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete

key

Remove a node from a Binary Search Tree

7 / 15

slide-8
SLIDE 8

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete

key

Remove a node from a Binary Search Tree

8 / 15

slide-9
SLIDE 9

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete

key

Remove a node from a Binary Search Tree

9 / 15

slide-10
SLIDE 10

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete

key

Remove a node from a Binary Search Tree

10 / 15

slide-11
SLIDE 11

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete

key

Remove a node from a Binary Search Tree

11 / 15

slide-12
SLIDE 12

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete succ

key k2

Remove a node from a Binary Search Tree

12 / 15

slide-13
SLIDE 13

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle toDelete succ

k2 k2

Remove a node from a Binary Search Tree

13 / 15

slide-14
SLIDE 14

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle succ

k2 k2

toDelete Remove a node from a Binary Search Tree

14 / 15

slide-15
SLIDE 15

void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); if (handle == NULL) return; Node * toDelete = handle; if (handle->left == NULL) {// Leaf or only right child handle = handle->right; } else if (handle->right == NULL) { // Only left child handle = handle->left; } else { // Two children Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; }

handle succ

k2 k2

toDelete Remove a node from a Binary Search Tree

15 / 15