1 / 8

# Binary Search Trees Deletion - PowerPoint PPT Presentation

Binary Search Trees Deletion. COP 3502. Binary Search Tree - Deletion. Let’s consider several cases for Deletion: Deleting a Leaf node Deleting a node with 1 child Deleting a node with 2 children For all cases we have to identify the parent. 6. 5. 10. 13. 9. 3. 8. 12. 1. 4. 7.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Binary Search Trees Deletion' - golda

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Binary Search Trees Deletion

COP 3502

• Let’s consider several cases for Deletion:

• Deleting a Leaf node

• Deleting a node with 1 child

• Deleting a node with 2 children

• For all cases we have to

identify the parent.

6

5

10

13

9

3

8

12

1

4

7

9

11

13

parent

6

• Deleting a leaf node

• Let’s say we want to delete 3

• Find the parent of 3

• which is 5

• Then what?

• free(parent->left);

• parent->left = NULL;

• or if it’s a right leaf node:

• free(parent->right);

• parent->right= NULL;

5

9

3

• Deleting node with 1 child

• Let’s say we want to delete 5

• We need to find the parent

• AND the sole child

• Then connect them

• temp = parent->left;

• child = temp->left;

• parent->left = child;

• free(temp);

parent

6

temp

5

9

child

3

7

12

1

4

6

• Deleting a node with 2 children:

• In order to maintain the BST property , we can replace the deleted node with either:

• Max in the left subtree

• OR Min in the right subtree

5

10

9

3

8

12

1

4

7

9

11

13

• In aiding in Deletion, we will want several auxiliary functions:

• node* parent(node* root)

• Finds the parent of a given node in a binary tree.

• node* minVal(node* root), node* maxVal(node* root)

• Finds the minimum (or maximum) value in a given binary tree.

• intisLeaf(node* root)

• Determines if a node is a leaf node or not.

• inthasOnlyLeftChild(node* root),

inthasOnlyRightChild(node* root)

• Determines if a node ONLY has a left (or right) child or not.

• node* findNode(node* root)

• Returns a pointer to a node in a given tree that stores a particular value.

node* delete(node* root, int value) {

node *delnode, *parent;

// Get a pointer to the node to delete.

delnode = findNode(root, value);

// Get the parent of this node.

parent = parent(root, delnode);

// Case 1: the node to delete is a leaf node.

if (isLeaf(delnode)) {...}

// Case 2: node to be deleted only has a left child.

if (hasOnlyLeftChild(delnode)) {...}

// Case 3: node to be deleted only has a right child.

if (hasOnlyRightChild(delnode)) {...}

// Case 4: the node has 2 children

else {...}

}

• Fill in the code together in class for the Delete function.