之前用c写过二叉树的遍历,现在用java来实现一下。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BinaryTreeTest{
public static void main(String args[]){
//BinaryTreeTest b = new BinaryTreeTest();
int data[]={5,3,8,9,25,16,19,14,30,7};
BinaryTree root = new BinaryTree(data[0]);
System.out.print("二叉树的中的数据: ");
for(int i=1;i<data.length;i++){
root.insertTree(root,data[i]);
System.out.print(data[i-1]+";");
}
System.out.println(data[data.length-1]);
System.out.println("先序遍历:");
xian(root);
System.out.println();
System.out.println("中序遍历:");
zhong(root);
System.out.println();
System.out.println("后序遍历");
hou(root);
System.out.println();
System.out.println("从大到小排序:");
其他(root);
}
/*
* 查询功能
*/
public static void chaxun(BinaryTreeTest b,BinaryTree root){
System.out.println("请输入数字:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
try {
s = br.readLine();
int key = Integer.parseInt(s);
if( b.searchkey(root,key)){
System.out.println("找到了:"+key);
}else{
System.out.println("没有找到:"+ key);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//其他
public static void 其他(BinaryTree root){
if(root.rightTree != null ){
其他(root.rightTree);
}
System.out.print(root.data+";");
if(root.leftTree != null){
其他(root.leftTree);
}
}
//后序遍历
public static void hou(BinaryTree root){
if(root.leftTree != null){
hou(root.leftTree);
}
if(root.rightTree != null ){
hou(root.rightTree);
}
System.out.print(root.data+";");
}
//中序遍历
public static void zhong(BinaryTree root){
if(root.leftTree != null){
zhong(root.leftTree);
}
System.out.print(root.data+";");
if(root.rightTree != null ){
zhong(root.rightTree);
}
}
//先序遍历
public static void xian(BinaryTree root){
System.out.print(root.data+";");
if(root.leftTree != null){
xian(root.leftTree);
}
if(root.rightTree != null ){
xian(root.rightTree);
}
}
public boolean searchkey(BinaryTree root, int key){
if(root == null){
return false;
}else if(root.data == key){
return true;
}else if(key >= root.data){
return searchkey(root.rightTree,key);
}
return searchkey(root.leftTree,key);
}
}
//构造树
class BinaryTree{
int data;
BinaryTree leftTree;
BinaryTree rightTree;
BinaryTree(int data){
this.data=data;
leftTree=null;
rightTree=null;
}
public void insertTree(BinaryTree root, int data){
if(data>=root.data){
if(root.rightTree==null){
root.rightTree=new BinaryTree(data);
}else{
insertTree(root.rightTree,data);
}
}else{
if(root.leftTree==null){
root.leftTree=new BinaryTree(data);
}else{
insertTree(root.leftTree,data);
}
}
}
}
上面仅仅是实现了二叉树的三种遍历,代码中有中文的方法名,反正是无聊,就不改了。就那样吧!之前课程设计代码见:http://download.csdn.net/detail/quanbove/3905964
上面的链接需要下载积分,这里直接贴出代码:
#include <stdio.h>
#include <stdlib.h>
#define num 100
#define OK 1
typedef int Status;
typedef char DataType;
typedef struct node{
DataType data;
struct node *lchild,*rchild;
}BinTNode,*BinTree;
int found;
BinTNode *p;
Status CreateBiTree(BinTree &bt)
{//1.按照先序遍历次序递建立二叉树。
//ABC@@DE@G@@F@@@ 以@代替空格。
char ch;
printf("ch=");
scanf("%c",&ch);
getchar();
if (ch==' ') bt=NULL;
else
{
bt=(BinTNode *)malloc(sizeof(BinTNode));
bt->data=ch; //生成根结点
CreateBiTree(bt->lchild); //构造左子树
CreateBiTree(bt->rchild); //构造右子树
}
return OK;
}
Status Inorder(BinTree bt)
{//二叉树非递 中序遍历算法
BinTNode *stack[num]; //定义栈数组
int top=0; //初始化栈
stack[top]=bt;
do
{
while(NULL!=stack[top])
{ //扫描根结点及其所有的左结点并入栈
top=top+1;
stack[top]=stack[top-1]->lchild;
}
top=top-1; //退栈
if(top>=0) //判断栈是否为空
{
printf("%c",stack[top]->data); //访问结点
stack[top]=stack[top]->rchild; //扫描右子树
}
}while(top>=0);
return OK;
}
BinTree NodePath(BinTree bt, BinTNode *ch)
//3.求二叉树指定结点的路径
{ typedef enum{FALSE,TRUE}boolean;
BinTNode *stack[num];//定义栈
int i,top,tag[num];
boolean find;
BinTNode *s;
find=FALSE;
top=0;
s=bt;
do
{
while(s!=NULL)
{
top++;
stack[top]=s;
tag[top]=0;
s=s->lchild;
}
if(top>0)
{
s=stack[top];
if(tag[top]==1)
{
if(s==ch)
{
for(i=1;i<=top;i++)
printf("->%c",stack[i]->data);
find=TRUE;
}
else
top--;
s=stack[top];
}//endif
if(top>0&&!find)
{
if(tag[top]!=1)
{
s=s->rchild;//扫描右子树
tag[top]=1;
}
else
s=NULL;
}//end if
} //endif
}while(!find&&top!=0);
return s;
}
void FindBT(BinTree bt,DataType x)
{
if((bt!=NULL)&&!found)
{
if(bt->data==x)
{
p=bt;
found=1;
}
else
{
FindBT(bt->lchild,x);
FindBT(bt->rchild,x);
}
}
}
BinTNode *Findx(BinTree bt,DataType x)
{
int found=0;
BinTree p=NULL;
FindBT(bt,x);
return(p);
}
int Depth(BinTree bt)
{//4.求二叉树的深度
int h,lh,rh;
if (bt==NULL) h=0;
else
{
lh=Depth(bt->lchild);
rh=Depth(bt->rchild);
if (lh>=rh) h=lh+1;
else h=rh+1;
}
return h;
}//Depth
int Leaf(BinTree bt)
//5.求二叉树的叶子结点个数
{
if (bt==NULL) return 0;
else if (bt->lchild==NULL&&bt->rchild==NULL)
return 1;
else
return Leaf(bt->lchild)+Leaf(bt->rchild);
}//Leaf
BinTNode *huhuan(BinTNode *p)
//将p指针指向的二叉树的左右子树进行互换。
{
BinTNode *stack[num];//指针类型的堆栈
int k=0;
stack[k]=NULL;
if(p!=NULL)//交换p结点的左右孩子
{
k++;
stack[k]=p->lchild;
p->lchild=p->rchild;
p->rchild=stack[k];
p->lchild=huhuan(p->lchild);
p->rchild=huhuan(p->rchild);
}
return (p);
}
void main()
{
BinTree bt;
//BinTNode *root;
char ch1;
int xz=1,sd=0,yz=0;
while(xz)
{
printf(" 建立二叉树并求指定结点路径 \n");
printf("============================\n");
printf(" 1.建立二叉树的存储结构 \n");
printf(" 2.求解二叉树的中序遍历 \n");
printf(" 3.求二叉树指定结点的路径 \n");
printf(" 4.求二叉树的深度 \n");
printf(" 5.求二叉树的叶子结点个数 \n");
printf(" 6.将二叉树左右子树交换 \n");
printf(" 0.退出系统 \n");
printf("============================\n");
printf(" 请选择:(0-6) \n");
scanf("%d",&xz);
getchar();
switch(xz)
{ // 输入:ABC@@DE@G@@F@@@ 输出: CBEGDFA
case 1:printf("输入二叉树的先序序列结点值:\n");
CreateBiTree(bt);
printf( "二叉树的链式存储结构建立完成!\n");
break;
case 2:printf("该二叉树的中序遍历序列是:\n");
Inorder(bt);
printf( "\n");
break;
case 3:printf( "请输入要求路径的结点值:\n" );
ch1=getchar();
p=NULL;
found=0;
Findx(bt,ch1);
if(p!=NULL)
NodePath(bt,p);
else
printf( "没有要求的节点! \n" );
printf( "\n" );
break;
case 4:sd=Depth(bt);
printf( "该二叉树的深度为:%d \n ",sd );
printf("\n");
break;
case 5:yz=Leaf(bt);
printf( "该二叉树的叶子节点数为:\n%d \n",yz );
printf( "\n" );
break;
case 6: bt=huhuan(bt);
printf( "该二叉树的左右结点已交换成功,其中序遍历序列是:\n" );
Inorder(bt);
printf( "\n" );
break;
}// switch
}// while
}