Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Java Programming II Lecture 28 BST Implementation.

Similar presentations


Presentation on theme: "CSC 205 Java Programming II Lecture 28 BST Implementation."— Presentation transcript:

1 CSC 205 Java Programming II Lecture 28 BST Implementation

2 Recap: BST Operations Insertion Tree is empty  insert as the root node (done) Tree is not empty  Compare search key with root node Insert into left/right subtree Notice: the tree may be empty

3 Deletion Find the node that contains the search key The node is Null  throw exception Is a leaf  delete Has a child  delete node and handle the child Has two children  replace the node with its inorder successor and then delete inorder successor Notice: the inorder successor may have one (right) child

4 BST Implementation – KeyedItem public abstract class KeyedItem { private Comparable searchKey; public KeyedItem (Comparable key) { searchKey = key; } public Comparable getKey(key) { return searchKey; } public class Person extends KeyedItem { private Fullname name; private String phone; private Address address; public Person(String id, Fullname name, String phone, Address address) { super(id); this.name = name; this.phone = phone; this.address = address; }

5 BST Implementation – insertion public void insert(KeyedItem newItem) { root = insertItem(root, newItem); } // end insert protected TreeNode insertItem(TreeNode tNode, KeyedItem newItem) { TreeNode newSubtree; if (tNode == null) { // position of insertion found; insert // after leaf create a new node tNode = new TreeNode(newItem, null, null); return tNode; } // end if KeyedItem nodeItem = (KeyedItem)tNode.getItem(); //to be continued Public interface

6 BST Implementation – insertion // search for the insertion position if (newItem.getKey(). compareTo(nodeItem.getKey()) < 0) { // search the left subtree newSubtree = insertItem(tNode.getLeft(), newItem); tNode.setLeft(newSubtree); return tNode; } else { // search the right subtree newSubtree = insertItem(tNode.getRight(), newItem); tNode.setRight(newSubtree); return tNode; } // end if } // end insertItem

7 BST Implementation – deletion public void delete(Comparable searchKey) throws TreeException { root = deleteItem(root, searchKey); } // end delete public void delete(KeyedItem item) throws TreeException { root = deleteItem(root, item.getKey()); } // end delete Public interface protected TreeNode deleteItem(TreeNode tNode, Comparable searchKey) { TreeNode newSubtree; if (tNode == null) { throw new TreeException("TreeException: " + "Item not found"); }

8 BST Implementation – deletion else { KeyedItem nodeItem = (KeyedItem)tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey()) == 0) { // item is in the root of some subtree tNode = deleteNode(tNode); // delete the item } // else search for the item else if (searchKey.compareTo(nodeItem.getKey()) < 0) { // search the left subtree newSubtree = deleteItem(tNode.getLeft(), searchKey); tNode.setLeft(newSubtree); } else { // search the right subtree...... } // end if return tNode; } // end deleteItem

9 BST Implementation – deletion protected TreeNode deleteNode(TreeNode tNode) { KeyedItem replacementItem; // test for a leaf if ( (tNode.getLeft() == null) && (tNode.getRight() == null) ) { return null; } // end if leaf // test for no left child else if (tNode.getLeft() == null) { return tNode.getRight(); } // end if no left child // test for no right child......

10 BST Implementation – deletion // there are two children: // retrieve and delete the inorder successor else { replacementItem = findLeftmost(tNode.getRight()); tNode.setItem(replacementItem); tNode.setRight(deleteLeftmost(tNode.getRight())); return tNode; } // end if } // end deleteNode


Download ppt "CSC 205 Java Programming II Lecture 28 BST Implementation."

Similar presentations


Ads by Google