day17 - COP 3503 Computer Science II CLASS NOTES - DAY #17...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3503 – Computer Science II CLASS NOTES - DAY #17 The previous day’s notes introduced the special case of the binary tree known as the binary search tree (BST). As mentioned before general search trees are called M-way search trees and are beyond the scope of this course. A binary search tree is a special case of an m-way search tree defined as follows: Insertion into a BST In the previous day’s notes we saw that insertion into a BST is a straight forward procedure which basically involves searching for the value to be inserted, knowing that it is presently not in the tree. The search will always be unsuccessful and will terminate on a leaf node (actual termination will occur on the null child reference at this leaf node). Once the “dead-end” is found, the insertion is straightforward. Deletion in a BST The complexity of the deletion operation is dependent upon the position of the node to be deleted in the tree. It is a far more difficult operation to delete a node which is the root of two non-null subtrees than it is to delete a leaf node. The complexity of the deletion operation is proportional to the number of children of the node to be deleted. 1. The node is a leaf. This is the trivial case in which the appropriate reference of the parent is set to null. An example is shown below. Day 17 - 1 More Details On The Operations On Binary Search Trees Binary Search Tree A binary search tree T is a finite set of keys. Either the set is empty, T = ; or the set consists of a root r and exactly two binary search trees T L and T R , T = {r, T L , T R } , such that the following properties are satisfied: 1. All keys contained in left subtree, T L , are less than r . 2. All keys contained in right subtree, T R , are greater than r. 5 1 6 7 3 2 4 5 1 6 7 3 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A BST with a leaf node The BST with the leaf node marked for deletion deleted. A BST remains 2. The node has one child. This case is also not too complicated, but is more so than case 1 above. The parent’s reference to the node is reset to refer to the deleted node’s child. This has the effect of lifting up the deleted node’s children by one level in the tree. (All great-great-…-grandchildren will lose one great from their kinship designations.) An example is shown below. A BST with an internal node having only one child marked to be deleted The marked internal node has only a right subtree so the parent of the deleted node will now reference the deleted node’s child Note that it makes no difference if the node to be deleted has only a left or a right child. The previous example illustrated the case when the only child was a right child. The next example illustrates the case when the only child is a left child. Day 17 - 2 5 1 6 7 3 2 5 6 7 3 2
Background image of page 2
Initial BST with the node to be deleted shown in green. Its only child is a left child The BST after the deletion has occurred 3. The last case of deletion from a BST is the most difficult to handle. There is no one-step operation that can be performed since the parent’s right or left reference cannot refer to both node’s children at the same time.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 16

day17 - COP 3503 Computer Science II CLASS NOTES - DAY #17...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online