Assignment – Binary Search Trees Pt. 2

advertisement

ICS 4U1 – Unit 2

Data Structures

Name: _________________________

Binary Trees – Part 2

Last time, we talked about the insertion method of binary search trees. Deletion is a bit more tricky and must be thought about in cases. Before we consider that, let’s look at one more important piece of terminology.

A leaf node is a node in the tree with no children. For example, in the tree below, the leaf nodes are 3 and 9.

1

3 6

9

Case 1: Deleting a leaf node

Deleting a leaf node is easy – we simply tell the parent node that its child is now null. For example, deleting 9 in the tree above requires us to set 6’s right child to null.

Case 2: Deleting a node with one subtree

Deleting a node with one subtree is also simple. We simply make the parent point to the subtree below (convince yourself that this maintains the search property).

For example, deleting 6 in the tree above requires us to set 1 to now point to 9 as its right child.

3

1

6

9

Delete(6)

3

1

9

ICS 4U1 – Unit 2

Data Structures

Name: _________________________

Case 3: Deleting a node with two subtrees

Deleting a node with two subtrees is more complex. For instance, deleting 1 in the original tree separates the entire structure.

1

BAD! We don’t have

1

3 6

9

Delete(1) 3 6

9

We need a way of replacing the node rather than removing it.

Looking back at the original tree, consider that whatever replaces the root of the tree must be larger than everything in the left subtree and smaller than everything in the right subtree.

This suggests two possible candidates as our new roots:

1) Choose the smallest element in the right subtree

OR

2) Choose the largest element in the left subtree

Again, stop and convince yourself that this maintains the search property.

For example, in the previous binary search tree, we can replace 1 with 6:

1 6

3 6

9

Delete(1)

3 6

9

Now we have two instances of 6! To get around this, we delete 6 from the subtree. Since 6 has only one child, this allows us to apply Case 2. However, there are trees where we’d have to apply Case 3 over and over in a recursive fashion. But that’s for you to think about…

ICS 4U1 – Unit 2

Data Structures

Name: _________________________

Assignment – Binary Search Trees Pt. 2

due Wednesday, March 25 th at the beginning of class

You will be adding deletion to your binary search tree implementation. In particular, you must implement the following method:

// Deletes any node containing the indicate value.

// If no such node exists, nothing is done! public void delete(int n);

Hints:

- Remember that you can create references to Node variables for the purposes of iterating through a tree. e.g. Node currentNode;

- You can save some serious time by first checking if the tree contains the given integer.

- Do you find yourself doing something repetitive (e.g. finding the smallest node in a right subtree or largest in a left subtree)? Feel free to implement private helper methods to make your code reusable. e.g. private Node findSmallest(Node r)

{

// Code to find and return the node with smallest value

// in the right subtree rooted at r.

}

- On the topic of helper methods, you may find it useful to make a helper method as follows: private void delete(Node r, int n)

{

// Finds and deletes the node containing n

// from the subtree rooted at r.

}

This will be especially useful for Case 3 where you need to call Delete on smaller and

smaller subtrees rooted at children within the tree.

- Remember that this code might be useful for you down the line. Like an epic ultimate

Frisbee play, make it reusable, well-documented, and beautiful.

Download