您的位置:首页 > 编程语言 > Java开发

二叉树的遍历问题-----Java实现(中序、前序、后序、遍历)

2016-04-22 10:53 639 查看
二叉树又称为二叉查找树。

特点:1)LeftChid(data)<Root(data),左子树的值<根的值;

    2)RightChild(data)>Root(data),右子树的值>根的值;

            3)左右子树仍然是二叉树

首先,创建一个二叉树结点的类:

class Node{
public int data;
public Node left,right;
public Node(int data){
this.data=data;
this.left=null;
this.right=null;
}
}


然后,创建二叉树并实现二叉树的遍历(中序、前序、后序、层次):

package com.study;

import java.util.LinkedList;
import java.util.Queue;

/*******************************************************************
* 功能描述:
* 创建信息:jtm 2016-4-22
* 修改信息:
********************************************************************/

public class BinaryTree{
private Node root;
//构造器中初始化变量
public BinaryTree(){
root =null;
}
//将data插入到二叉排序树中
public void insert(int data){
Node newNode=new Node(data);//用于插入的结点
if(root==null){
root=newNode;
}else{
Node current=root;
Node parent;
while(true){//寻找插入的位置
parent=current;
if(data<current.data){
current=current.left;
if(current==null){
parent.left=newNode;
return;
}
}else{
current =current.right;
if(current==null){
parent.right=newNode;
return;
}
}
}
}
}
//将数值输入构建的二叉树中
public void buildTree(int[] data){
for(int i=0;i<data.length;i++){
insert(data[i]);
}
}
//遍历方法:中序,前序,后序

//中序
public void middleOrder(Node localroot){
if(localroot!=null){
middleOrder(localroot.left);
System.out.print(localroot.data+" ");
middleOrder(localroot.right);
}

}
public void middleOrder(){
this.middleOrder(root);
}
//前序
public void PreOrder(Node localroot){
if(localroot!=null){
System.out.print(localroot.data+" ");
PreOrder(localroot.left);
PreOrder(localroot.right);
}
}
public void PreOrder(){
this.PreOrder(root);
}
//后序
public void PostOrder(Node localroot){
if(localroot!=null){
PostOrder(localroot.left);
PostOrder(localroot.right);
System.out.print(localroot.data+" ");
}
}
public void PostOrder(){
this.PostOrder(root);
}
public void layerOrder(){//遍历二叉查找树
if(this.root==null){
return;
}
Queue<Node> q=new LinkedList<Node>();
q.add(this.root);
while(!q.isEmpty()){
Node n=q.poll();
System.out.print(n.data+" ");
if(n.left!=null){
q.add(n.left);
}
if(n.right!=null){
q.add(n.right);
}
}
}
public static void main(String[] args){
int[] data={2,8,7,4,9,3,1,6,7,5};
BinaryTree tree=new BinaryTree();
tree.buildTree(data);
System.out.print("中序");
tree.middleOrder();
System.out.println();
System.out.print("前序");
tree.PreOrder();
System.out.println();
System.out.print("后序");
tree.PostOrder();
System.out.println();
System.out.print("层次");
tree.layerOrder();
}
}


输出结果:

中序1 2 3 4 5 6 7 7 8 9 

前序2 1 8 7 4 3 6 5 7 9 

后序1 3 5 6 4 7 7 9 8 2 

层次2 1 8 7 9 4 7 3 6 5 

验证一下:

                               2

                            /      \

                         1          8

                                   /     \

                                7         9

                              /   \        

                           4      7   

                          /   \

                       3     6

                             /

                           5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 二叉树 遍历