给你一个用数组做的的例子:
package com.suanfa;import ConsoleReader.*;
class BNTreeArray
{
public static int MaxSize=16;
public static int[] TreeData=new int[MaxSize]; //存放数据
public static int[] RightNode=new int[MaxSize]; //存放对应的右接点的数组序号
public static int[] LeftNode=new int[MaxSize];//存放对应的左接点的数组序号 public BNTreeArray()
{
int i;
for(i=0;i<MaxSize;i++)
{
TreeData[i]=0;
RightNode[i]=-1;
LeftNode[i]=-1;
}
}
public void Create(int Data)
{
int i;
int Level=0;
int Position=0;
for(i=0;TreeData[i]!=0;i++)
{
TreeData[i]=Data;
}
while(true)
{
if(Data>TreeData[Level])
{
if(RightNode[Level]!=-1)
{
Level=RightNode[Level];
}
else
{
Position=-1;
break;
}
}
else
{
if(LeftNode[Level]!=-1)
{
Level=LeftNode[Level];
}
else
{
Position=1;
break;
}
}
}
if(Position==1)
{
LeftNode[Level]=i;
}
else
{
RightNode[Level]=i;
}
}
//先序遍历
public static void PreOrder(int Pointer)
{
if(Pointer!=-1)
{
System.out.println(TreeData[Pointer]);
PreOrder(LeftNode[Pointer]);
PreOrder(RightNode[Pointer]);
}
}
}
public class tree02 {
public static void main(String[] args)
{
int i;
int Index=1;
int Data;
BNTreeArray BNTree =new BNTreeArray();
System.out.println("please input the elements of binary tree(Exit for 0)");
ConsoleReader console=new ConsoleReader(System.in);
System.out.print("Data"+Index+":");
Data=console.readInt();
BNTree.TreeData[0]=Data;
Index++;
while(true)
{
if(Data==0)
break;
BNTree.Create(Data);
Index++;
}
System.out.print("the preorder traversal result is [");
BNTree.PreOrder(0);
System.out.println("]");
}}
package com.suanfa;import ConsoleReader.*;
class BNTreeArray
{
public static int MaxSize=16;
public static int[] TreeData=new int[MaxSize]; //存放数据
public static int[] RightNode=new int[MaxSize]; //存放对应的右接点的数组序号
public static int[] LeftNode=new int[MaxSize];//存放对应的左接点的数组序号 public BNTreeArray()
{
int i;
for(i=0;i<MaxSize;i++)
{
TreeData[i]=0;
RightNode[i]=-1;
LeftNode[i]=-1;
}
}
public void Create(int Data)
{
int i;
int Level=0;
int Position=0;
for(i=0;TreeData[i]!=0;i++)
{
TreeData[i]=Data;
}
while(true)
{
if(Data>TreeData[Level])
{
if(RightNode[Level]!=-1)
{
Level=RightNode[Level];
}
else
{
Position=-1;
break;
}
}
else
{
if(LeftNode[Level]!=-1)
{
Level=LeftNode[Level];
}
else
{
Position=1;
break;
}
}
}
if(Position==1)
{
LeftNode[Level]=i;
}
else
{
RightNode[Level]=i;
}
}
//先序遍历
public static void PreOrder(int Pointer)
{
if(Pointer!=-1)
{
System.out.println(TreeData[Pointer]);
PreOrder(LeftNode[Pointer]);
PreOrder(RightNode[Pointer]);
}
}
}
public class tree02 {
public static void main(String[] args)
{
int i;
int Index=1;
int Data;
BNTreeArray BNTree =new BNTreeArray();
System.out.println("please input the elements of binary tree(Exit for 0)");
ConsoleReader console=new ConsoleReader(System.in);
System.out.print("Data"+Index+":");
Data=console.readInt();
BNTree.TreeData[0]=Data;
Index++;
while(true)
{
if(Data==0)
break;
BNTree.Create(Data);
Index++;
}
System.out.print("the preorder traversal result is [");
BNTree.PreOrder(0);
System.out.println("]");
}}
解决方案 »
- jsp 怎么实现获取登录计算机的用户名
- Spring中任务调度cronExpression的配置问题
- Cannot convert Unicode string to Ebcdic string 异常
- ext button 样式问题
- LOG4J如何能实现stdout为INFO,fileappender为ERROR
- Spring:SET注入和构造注入的问题.
- 求一正则表达式,朋友们帮把手啊!!!
- servlet 输出中文问题
- 大客户量的访问的JAVA Web Services
- 高分解答,为什么javac编译成功而appletviewer看结果时会出错啊???
- 大家介绍几本关于JAVA的入门书籍,多谢了
- 客户端怎么调用一个webservice???????????
import java.util.Stack;public class myTest {
private myTree tree;
/**
*二叉树的插入,参数为(关键字,数据)
*
**/
public void insert(int key, int data)
{
if(tree == null)
{
tree = new myTree();
tree.key = key;
tree.data = data;
}
else
{
myTree newTree = new myTree();
newTree.key = key;
newTree.data = data;
myTree parent = tree;
while(true)
{
if( newTree.key < parent.key)
{
if( parent.leftChild == null)
{
parent.leftChild = newTree;
return;
}
else
{
parent = parent.leftChild;
}
}
else if( newTree.key > parent.key)
{
if(parent.rightChild == null)
{
parent.rightChild = newTree;
return;
}
else
{
parent = parent.rightChild;
}
}
}
}
}
/**
* 二叉树的查找,参数为(关键字),返回值为 myTree的一个实例
*
* **/
public myTree find(int key)
{
if( tree == null ) return null;
myTree curr = new myTree();
curr.key = key;
myTree parent = tree;
while(true)
{
if( parent == null)
{
return null;
}
else if( curr.key == parent.key)
{
return parent;
}
else if( curr.key > parent.key)
{
parent = parent.rightChild;
}
else if( curr.key < parent.key)
{
parent = parent.leftChild;
}
}
}
/*
*
* 递归的二叉树中序遍历
*
*
*/
private static void midOrder(myTree tree)
{
if(tree != null )
{
midOrder(tree.leftChild);
System.out.println(tree+","+tree.key+","+tree.data);
midOrder(tree.rightChild);
}
}
/*
* 前序遍历
*/
private static void frontOrder(myTree tree)
{
if(tree != null)
{
System.out.println(""+tree.key+" , "+tree.data);
frontOrder(tree.leftChild);
frontOrder(tree.rightChild);
}
}
public static void main(String[] args)
{
System.out.println("Tree view Begin");
myTest t1 = new myTest();
t1.insert(8,25);
t1.insert(5,9);
t1.insert(58,87);
t1.insert(13,82);
t1.insert(4,8);
t1.insert(12,54);
t1.insert(53,123);
t1.insert(56,47);
t1.insert(2,75);
t1.insert(34,5);
t1.insert(6,23);
System.out.println("现在开始遍历:");
midOrder2(t1.tree);
midOrder(t1.tree);
}
}忘了哪年写的了,二叉树的递归遍历都是一样地,只是看你怎么排顺序了,呵呵