源程序如下:
//二叉树查找 插入 显示
class Node{
int iData;
double fData;
Node leftChild;
Node rightChild;
public void displayNode(){
//
System.out.println("{");
System.out.println(iData);
System.out.println(",");
System.out.println(fData);
System.out.println("}");
}
}//end Nodeclass Tree{
private Node root;//
//public Node root;//
//public void find(int key){//不可以用void
public Node find(int key){
//
Node current =root;
while(current.iData !=key){
if(key<current.iData)
current =current.leftChild;
else current =current.rightChild;
if (current ==null)
return null;
}
return current;
}
public void insert(int id,double dd){
//
Node newNode =new Node();
newNode.iData =id;
newNode.fData =dd;
if(root ==null) root =newNode;
else {
Node current =root;
Node parent;
while(true){
parent =current;
if(id <current.iData){//go left
current =current.leftChild;
if (current ==null){//if end of the line
parent.leftChild=newNode;
return;
}
}//end go left
//}//end while
else{ //go right
current =current.rightChild;
if(current ==null){//if end of the line
parent.rightChild=newNode;
return;
}//end if end of the line
}//end go right
}//
}
}//end insert
public void delete(int id){
//public boolean delete(int key){
//code too more
}
//}
///*
private void preOrder(Node localRoot){//递归先续遍历 ????
if (localRoot!=null){
System.out.println(localRoot.fData+"");//打印出先序的树出来???
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
//*/
}//end Treeclass TreeApp{
public static void main(String[] args){
Tree theTree =new Tree();
System.out.println("start insert {50,1.5},{25,1.7},{75,1.9}");
theTree.insert(50,1.5);
theTree.insert(25,1.7);
theTree.insert(75,1.9);
//theTree.preOrder(25);//????怎么给先序赋值??
//Tree found =theTree.find(25);
Node found =theTree.find(25);
//Node found = theTree.find(25);
if(found !=null) {
System.out.println("found the node with key 25 is:");
found.displayNode();
System.out.println("\n");}
else
System.out.println("could not found the node with key 25 is:");
}//end main
}//end TreeApp我怎么把输入的或固定的数据按照先序便利 打印出来??怎么赋值初始化,谢谢??
//二叉树查找 插入 显示
class Node{
int iData;
double fData;
Node leftChild;
Node rightChild;
public void displayNode(){
//
System.out.println("{");
System.out.println(iData);
System.out.println(",");
System.out.println(fData);
System.out.println("}");
}
}//end Nodeclass Tree{
private Node root;//
//public Node root;//
//public void find(int key){//不可以用void
public Node find(int key){
//
Node current =root;
while(current.iData !=key){
if(key<current.iData)
current =current.leftChild;
else current =current.rightChild;
if (current ==null)
return null;
}
return current;
}
public void insert(int id,double dd){
//
Node newNode =new Node();
newNode.iData =id;
newNode.fData =dd;
if(root ==null) root =newNode;
else {
Node current =root;
Node parent;
while(true){
parent =current;
if(id <current.iData){//go left
current =current.leftChild;
if (current ==null){//if end of the line
parent.leftChild=newNode;
return;
}
}//end go left
//}//end while
else{ //go right
current =current.rightChild;
if(current ==null){//if end of the line
parent.rightChild=newNode;
return;
}//end if end of the line
}//end go right
}//
}
}//end insert
public void delete(int id){
//public boolean delete(int key){
//code too more
}
//}
///*
private void preOrder(Node localRoot){//递归先续遍历 ????
if (localRoot!=null){
System.out.println(localRoot.fData+"");//打印出先序的树出来???
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
//*/
}//end Treeclass TreeApp{
public static void main(String[] args){
Tree theTree =new Tree();
System.out.println("start insert {50,1.5},{25,1.7},{75,1.9}");
theTree.insert(50,1.5);
theTree.insert(25,1.7);
theTree.insert(75,1.9);
//theTree.preOrder(25);//????怎么给先序赋值??
//Tree found =theTree.find(25);
Node found =theTree.find(25);
//Node found = theTree.find(25);
if(found !=null) {
System.out.println("found the node with key 25 is:");
found.displayNode();
System.out.println("\n");}
else
System.out.println("could not found the node with key 25 is:");
}//end main
}//end TreeApp我怎么把输入的或固定的数据按照先序便利 打印出来??怎么赋值初始化,谢谢??
{
if(node != null)
{
System.out.println(node.iData+" "+node.fData);
inOrder(node.leftChild);
inOrder(node.rightChild);
}
}
调用的时候把root给这个方法做参数就ok
root={{50,1.5},{25,1.7},{75,1.9}};//??
theTree.preOrder(root);// ??
theTree.preOrder(root);