binary search tree remove
play

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:


  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

  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

  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 other element. Hmmm... 3 / 15

  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

  5. Remove a node from a Binary Search Tree 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; } 5 / 15

  6. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key 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; } 6 / 15

  7. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key toDelete 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; } 7 / 15

  8. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key toDelete 0 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; } 8 / 15

  9. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key toDelete 0 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; } 9 / 15

  10. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key toDelete 0 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; } 10 / 15

  11. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key toDelete 0 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; } 11 / 15

  12. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); key toDelete 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 succ handle = handle->left; k2 } else { // Two children 0 Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; } 12 / 15

  13. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); k2 toDelete 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 succ handle = handle->left; k2 } else { // Two children 0 Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; } 13 / 15

  14. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); k2 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 succ handle = handle->left; k2 } else { // Two children toDelete 0 Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; } 14 / 15

  15. Remove a node from a Binary Search Tree handle void remove(KType key, Node *& root) { Node *& handle= pfind(key, root); k2 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 succ handle = handle->left; k2 } else { // Two children toDelete 0 Node *& succ = findMin(handle->right); handle->key = succ->key; toDelete = succ; succ = succ->right; // succ has no left child } delete toDelete; } 15 / 15

Recommend


More recommend