View Single Post
Join Date: Aug 2007
Posts: 47
Reputation: Dio1080 is an unknown quantity at this point 
Solved Threads: 0
Dio1080's Avatar
Dio1080 Dio1080 is offline Offline
Light Poster

Binary Search tree help in Java 98% complete

 
0
  #1
Oct 31st, 2008
Ok, the code is almost done and I need help finishing up my delete function(between lines 51 to 71), I can't seem to figure out how to delete the 50 and the 20. Here is my code. BTW i'll list the output first. Thanks
Sample output
10 20 30 50 60 80
10 20 30 50 60 80
Tree after deleting 20:
10 30 50 60 80
Tree after deleting 50:
10 30 60 80
Tree is not empty.
  1. public class BinarySearchTree
  2. {
  3.  
  4. private class node
  5. {
  6. int data;
  7. node leftchild;
  8. node rightchild;
  9. }
  10.  
  11. public BinarySearchTree()
  12. { //create an empty Binary Search Tree }
  13. //node root = new node;
  14. root=null;
  15.  
  16. }
  17.  
  18.  
  19. public boolean empty()
  20. { // return true if the tree is empty, otherwise return false }
  21. if ( root == null ){
  22. return true;
  23. }else{
  24. return false;
  25. }
  26.  
  27. }
  28. public void Insert(int x)
  29. {//insert a value x }
  30. //search for a location to insert
  31. node p=root;
  32. node q=null;
  33. while(p!=null){
  34. q=p;
  35. if(x == p.data){ return;
  36. }
  37. if(x < p.data){ p=p.leftchild;
  38. }else{ p=p.rightchild;
  39. }
  40. }
  41. // create the new node
  42. p=new node();
  43. p.data = x;
  44. p.leftchild = p.rightchild = null;
  45. //attaching the new node to the tree
  46. if(root==null){ root = p;
  47. }else if(x < q.data){ q.leftchild=p;
  48. }else{ q.rightchild = p;
  49. }
  50. return;
  51. }
  52. public void Delete(int x)
  53. {//if value x is in the tree, remove x }
  54.  
  55. //make node for q and p
  56. node q = new node();
  57. node p = new node();
  58. q=p=root;
  59.  
  60. while(q==null)
  61. {
  62. if(x==p.data)
  63. break;
  64. q=p;
  65. if(x < p.data)
  66. p = q.leftchild;
  67. else
  68. p=p.rightchild;
  69. }
  70. if(p!=null){
  71. p=p.rightchild;
  72. }
  73. }
  74.  
  75. public void Display()
  76. {// Display the data values from smallest to largest }
  77. Display (root);
  78. }
  79. public void Display(node p)
  80. {
  81. if(p!= null){
  82. Display(p.leftchild);
  83. System.out.println(p.data);
  84. Display(p.rightchild);
  85. }
  86. }
  87.  
  88.  
  89. private node root;//pointer to the root node
  90. }
  91.  
  92. ----------------MAIN------------------------------------------------
  93.  
  94. public class BinarySeachtreeMain{
  95. public static void main(String[] arg)
  96. {
  97. BinarySearchTree X = new BinarySearchTree();
  98.  
  99. X.Insert(50);
  100. X.Insert(10);
  101. X.Insert(20);
  102. X.Insert(80);
  103. X.Insert(60);
  104. X.Insert(30);
  105.  
  106. X.Display();
  107.  
  108.  
  109.  
  110.  
  111. X.Delete (20);
  112. System.out.println ("Tree after deleting 20:");
  113. X.Display();
  114. X.Delete(50);
  115. System.out.println ("Tree after deleting 50:");
  116. X.Display();
  117.  
  118. if(!X.empty())
  119. System.out.println("Tree is not empty.");
  120. }
  121. }
Reply With Quote