| | |
Binary Search tree help in Java 98% complete
![]() |
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.
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.
java Syntax (Toggle Plain Text)
public class BinarySearchTree { private class node { int data; node leftchild; node rightchild; } public BinarySearchTree() { //create an empty Binary Search Tree } //node root = new node; root=null; } public boolean empty() { // return true if the tree is empty, otherwise return false } if ( root == null ){ return true; }else{ return false; } } public void Insert(int x) {//insert a value x } //search for a location to insert node p=root; node q=null; while(p!=null){ q=p; if(x == p.data){ return; } if(x < p.data){ p=p.leftchild; }else{ p=p.rightchild; } } // create the new node p=new node(); p.data = x; p.leftchild = p.rightchild = null; //attaching the new node to the tree if(root==null){ root = p; }else if(x < q.data){ q.leftchild=p; }else{ q.rightchild = p; } return; } public void Delete(int x) {//if value x is in the tree, remove x } //make node for q and p node q = new node(); node p = new node(); q=p=root; while(q==null) { if(x==p.data) break; q=p; if(x < p.data) p = q.leftchild; else p=p.rightchild; } if(p!=null){ p=p.rightchild; } } public void Display() {// Display the data values from smallest to largest } Display (root); } public void Display(node p) { if(p!= null){ Display(p.leftchild); System.out.println(p.data); Display(p.rightchild); } } private node root;//pointer to the root node } ----------------MAIN------------------------------------------------ public class BinarySeachtreeMain{ public static void main(String[] arg) { BinarySearchTree X = new BinarySearchTree(); X.Insert(50); X.Insert(10); X.Insert(20); X.Insert(80); X.Insert(60); X.Insert(30); X.Display(); X.Delete (20); System.out.println ("Tree after deleting 20:"); X.Display(); X.Delete(50); System.out.println ("Tree after deleting 50:"); X.Display(); if(!X.empty()) System.out.println("Tree is not empty."); } }
•
•
Join Date: Sep 2008
Posts: 1,574
Reputation:
Solved Threads: 199
http://www.csee.umbc.edu/courses/und.../Trees/BST.pdf
Read those lecture notes. They literally have the remove method in them. So assuming the rest of your code is correct, look at the remove method in those notes and compared it to your delete method. Once you figure out whats different, try to figure out why your code is wrong. If you can't, point out the sections of your code where its different from the correct remove() method and ask us for some help from there. Good luck
Read those lecture notes. They literally have the remove method in them. So assuming the rest of your code is correct, look at the remove method in those notes and compared it to your delete method. Once you figure out whats different, try to figure out why your code is wrong. If you can't, point out the sections of your code where its different from the correct remove() method and ask us for some help from there. Good luck
![]() |
Similar Threads
- Binary Search Tree (Java)
- algorithm problem with recursion (Java)
Other Threads in the Java Forum
- Previous Thread: Please help, tracing app.
- Next Thread: can anyone find my mistake in TrainingZone.java
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






