// Binary Search Tree // public class BTree1 { // The application-specific data to be stored in the node public String StId; public String StName; public String StAdress; // The data-structure-specific data for each node private BTree1 left = null; private BTree1 right = null; // These instantiated only once in program static BTree1 root = null; // Used to keep track of top of tree static BTree1 temp = null; static BTree1 ptemp = null; // This constructor method is used to add a new node into the tree. // public BTree1(String id, String name) { // App-spec part // // this.StId = new String(id); // this.StName = new String(name); this.StId = id; this.StName = name; System.out.println("Inserting a node "+this.StId); // Data structure part // this.left = null; this.right = null; // Find out where new node is to go and insert it // if(root == null) { // There's no tree yet root = this; } else { // there is a tree temp = root; // start at the root while (temp != null) { // descend tree to find parent for ptemp = temp; // new node if(this.StId.compareTo(temp.StId) < 0) temp = temp.left; else temp = temp.right; } if(this.StId.compareTo(ptemp.StId) < 0) ptemp.left = this; else ptemp.right = this; } } // Useful method for debugging // print out all nodes in the tree // in sorted order, "inorder traversal" // // Hint: Should take about 10 or 20 lines of code // ... if you think you need more then your logic // is probably more complicated than it needs to be. // public void InOrderTraverse(BTree1 node) { if (node == null) { System.out.println("****** Reached a null reference"); return; } else { System.out.println("****** Reached node "+node.StId); InOrderTraverse(node.left); System.out.println(node.StId); // Write out the ID field InOrderTraverse(node.right); } } public void PreOrderTraverse(BTree1 node) { if (node == null) { System.out.println("****** Reached a null reference"); return; } else { System.out.println("****** Reached node "+node.StId); System.out.println(node.StId); // Write out the ID field PreOrderTraverse(node.left); PreOrderTraverse(node.right); } } public void PostOrderTraverse(BTree1 node) { if (node == null) { System.out.println("****** Reached a null reference"); return; } else { System.out.println("****** Reached node "+node.StId); PostOrderTraverse(node.left); PostOrderTraverse(node.right); System.out.println(node.StId); // Write out the ID field } } // Print out the data from a node if it exists // public void Search(String id) { int val; temp = root; while (temp != null) { // descend tree to find parent for if((val = id.compareTo(temp.StId)) == 0) { System.out.println("Student ID "+id+" is in the tree!"); System.out.println("Student name is "+temp.StName); temp = null; // to get out of loop } else if (val < 0) temp = temp.left; else temp = temp.right; // else it must be greater } } public static void main(String Args[]){ new BTree1("85025390", "Frank"); new BTree1("98329522", "Olwyn"); // Would be better to create node via constructor and insert // it into tree via a separate inssert method. new BTree1("84025379", "Fergal"); new BTree1("95025390", "Fiona"); System.out.println("****** Doing an InOrder Traverse"); root.InOrderTraverse(root); root.Search("95025390"); // System.out.println("****** Doing an PreOrder Traverse"); //root.PreOrderTraverse(root); //System.out.println("****** Doing an PostOrder Traverse"); //root.PostOrderTraverse(root); } // Tutorial : Make this program nicer, including a recursive search } 别人的算法,我没有运行过的。试下吧。
http://topic.csdn.net/u/20091021/10/ae208f69-f97a-45b0-ba83-119b76f1318a.html
// Binary Search Tree
//
public class BTree1 { // The application-specific data to be stored in the node public String StId;
public String StName;
public String StAdress; // The data-structure-specific data for each node private BTree1 left = null;
private BTree1 right = null; // These instantiated only once in program static BTree1 root = null; // Used to keep track of top of tree
static BTree1 temp = null;
static BTree1 ptemp = null; // This constructor method is used to add a new node into the tree.
//
public BTree1(String id, String name) { // App-spec part
//
// this.StId = new String(id);
// this.StName = new String(name); this.StId = id;
this.StName = name; System.out.println("Inserting a node "+this.StId); // Data structure part
//
this.left = null;
this.right = null; // Find out where new node is to go and insert it
//
if(root == null) { // There's no tree yet
root = this;
}
else { // there is a tree
temp = root; // start at the root
while (temp != null) { // descend tree to find parent for
ptemp = temp; // new node
if(this.StId.compareTo(temp.StId) < 0) temp = temp.left;
else temp = temp.right;
}
if(this.StId.compareTo(ptemp.StId) < 0) ptemp.left = this;
else ptemp.right = this;
}
} // Useful method for debugging
// print out all nodes in the tree
// in sorted order, "inorder traversal"
//
// Hint: Should take about 10 or 20 lines of code
// ... if you think you need more then your logic
// is probably more complicated than it needs to be.
//
public void InOrderTraverse(BTree1 node) {
if (node == null) {
System.out.println("****** Reached a null reference");
return;
}
else {
System.out.println("****** Reached node "+node.StId);
InOrderTraverse(node.left);
System.out.println(node.StId); // Write out the ID field
InOrderTraverse(node.right);
}
} public void PreOrderTraverse(BTree1 node) { if (node == null) {
System.out.println("****** Reached a null reference");
return;
}
else {
System.out.println("****** Reached node "+node.StId);
System.out.println(node.StId); // Write out the ID field
PreOrderTraverse(node.left);
PreOrderTraverse(node.right);
}
} public void PostOrderTraverse(BTree1 node) { if (node == null) {
System.out.println("****** Reached a null reference");
return;
}
else {
System.out.println("****** Reached node "+node.StId);
PostOrderTraverse(node.left);
PostOrderTraverse(node.right);
System.out.println(node.StId); // Write out the ID field
}
} // Print out the data from a node if it exists
//
public void Search(String id) {
int val; temp = root;
while (temp != null) { // descend tree to find parent for if((val = id.compareTo(temp.StId)) == 0) {
System.out.println("Student ID "+id+" is in the tree!");
System.out.println("Student name is "+temp.StName);
temp = null; // to get out of loop
}
else if (val < 0) temp = temp.left;
else temp = temp.right; // else it must be greater
}
}
public static void main(String Args[]){ new BTree1("85025390", "Frank");
new BTree1("98329522", "Olwyn"); // Would be better to create node via constructor and insert
// it into tree via a separate inssert method. new BTree1("84025379", "Fergal");
new BTree1("95025390", "Fiona"); System.out.println("****** Doing an InOrder Traverse");
root.InOrderTraverse(root); root.Search("95025390"); // System.out.println("****** Doing an PreOrder Traverse");
//root.PreOrderTraverse(root); //System.out.println("****** Doing an PostOrder Traverse");
//root.PostOrderTraverse(root);
} // Tutorial : Make this program nicer, including a recursive search
}
别人的算法,我没有运行过的。试下吧。
http://www.cnblogs.com/aspxphpjsprb/archive/2007/12/03/980415.html
代码很规范,建议楼主看一下。