abouttr3

Untitled

May 1st, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. public class BinarySearchTree extends BinaryTree
  2. {
  3.     public BinarySearchTree()
  4.     //default constructor
  5.     //postcondition:root=null
  6.     {
  7.         //Write the method definition here
  8.     }
  9.     public Boolean search(int searchItem)
  10.     //method to determine whether searchItem is in binary search
  11.     //tree
  12.     //postcondition: A node is created and inserted in the binary
  13.     //tree
  14.     {
  15.         return search(searchItem,root);
  16.     }
  17.     public Boolean search(int searchItem,BinaryTreeNode root){
  18.         if(root==null){
  19.             return false;
  20.         }
  21.         if(searchItem==root.data){
  22.             return true;
  23.         }
  24.         else if(searchItem<root.data){
  25.             return search(searchItem,root.left);
  26.         }
  27.         else{
  28.             return search(searchItem,root.right);
  29.         }
  30.     }
  31.     public void insert(int searchItem)
  32.     //method to determine whether searchItem is in binary search
  33.     //tree
  34.     //post condition: A node is created and inserted in the binary
  35.     //tree
  36.     {
  37.         if(search(searchItem)){
  38.             System.err.println("Item alredy exist");
  39.         }
  40.         else{
  41.             insertNode(searchItem);
  42.         }
  43.     }
  44.     public void deleteNode(int deleteItem)
  45.     //method to delete deleteItem from the binary tree.
  46.     //postcondition: if a node with the same info as deleteItem is    
  47.     //found, it is deleted from the binary tree.
  48.     {
  49.         deleteNode(deleteItem,root);
  50.     }
  51.     private  BinaryTreeNode deleteNode(int deleteItem, BinaryTreeNode root){
  52.        
  53.         if(root==null){
  54.             return null;
  55.         }
  56.         if(deleteItem<root.data){
  57.             root.left=deleteNode(deleteItem,root.left);
  58.         }
  59.         else if(deleteItem>root.data){
  60.             root.right=deleteNode(deleteItem,root.right);
  61.         }
  62.         else{
  63.            
  64.             if(root.left==null&&root.right==null){
  65.                 root=null;
  66.             }
  67.             else if(root.left==null){
  68.                 root=root.right;
  69.             }
  70.             else if(root.right==null){
  71.                 root=root.left;
  72.             }
  73.             else{
  74.  
  75.                 BinaryTreeNode leftMost=root.right;
  76.                 if(leftMost.left==null){
  77.                     root.data=leftMost.data;
  78.                     root.right = leftMost.right;
  79.                 }
  80.                 else{
  81.                     BinaryTreeNode current=root;
  82.                     while(leftMost.left!=null){
  83.                         current=leftMost;
  84.                         leftMost=leftMost.left;
  85.                     }
  86.                     root.data=leftMost.data;
  87.                     current.left = leftMost.right;
  88.                 }
  89.                
  90.             }
  91.            
  92.         }
  93.         return root;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment