http://bbs.tarena.com.cn/archiver/tid-231693.html
http://www.ccidedu.com/art/1925/20040923/158319_1.html
public class BinaryNode {
private int value;//current value
private BinaryNode lChild;//left child
private BinaryNode rChild;//right child
public BinaryNode(int value, BinaryNode l, BinaryNode r){
this.value = value;
this.lChild = l;
this.rChild = r;
}
public BinaryNode getLChild() {
return lChild;
}
public void setLChild(BinaryNode child) {
lChild = child;
}
public BinaryNode getRChild() {
return rChild;
}
public void setRChild(BinaryNode child) {
rChild = child;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
//iterate all node.
public static void iterate(BinaryNode root){ //这个遍历方法和中序遍历一样
if(root.lChild!=null){
iterate(root.getLChild());
}
System.out.print(root.getValue() + " ");
if(root.rChild!=null){
iterate(root.getRChild());
}
}
/**
* add child to the current node to construct a tree.
* Time: O( nlog(n) )
* **/
public void addChild(int n){ //创建二叉树
if(n<value){
if(lChild!=null){
lChild.addChild(n);
}
else{
lChild = new BinaryNode(n, null, null);
}
}
else{
if(rChild!=null){
rChild.addChild(n);
}
else{
rChild = new BinaryNode(n, null, null);
}
}
}
//test case.
public static void main(String[] args){
System.out.println();
int[] arr = new int[]{23,54,1,65,9,3,100};
BinaryNode root = new BinaryNode(arr[0], null, null); //根结点
for(int i=1; i<arr.length; i++){
root.addChild(arr[i]); //构造二叉树
}
BinaryNode.iterate(root); //1 3 9 23 54 65 100
System.out.println("*****************");
root.preorder(root); //23 1 9 3 54 65 100 先序
root.inorder(root); //1 3 9 23 54 65 100 中序
root.postorder(root); //3 9 1 100 65 54 23 后序
}
public void preorder(BinaryNode p){ //先根次序遍历二叉树
if(p!=null){
System.out.print(p.value+" ");
preorder(p.lChild);
preorder(p.rChild);
}
}
public void inorder(BinaryNode p){ //中根次序遍历二叉树
if(p!=null){
inorder(p.lChild);
System.out.print(p.value+" ");
inorder(p.rChild);
}
}
public void postorder(BinaryNode p){ //后根次序遍历二叉树
if(p!=null){
postorder(p.lChild);
postorder(p.rChild);
System.out.print(p.value+" ");
}
}
}
分享到:
相关推荐
用C++写的二叉树先序遍历、中序遍历和后序遍历非递归算法
用C++写的,包括二叉树的构建,二叉树的先序遍历、中序遍历和后序遍历非递归算法。
二叉树的递归遍历,中序遍历,先序遍历,后序遍历,通过学习二叉树的遍历,可以让我们更紧一步掌握数据的遍历
数据结构课程 一般是老师布置作业 小型的代码 二叉树的遍历方法 先序、中序、后序遍历法
数据结构C++二叉链表的先序遍历、中序遍历和后序遍历实现
二叉树先序、中序、后序遍历非递归算法,简述了二叉树的基本算法。
创建二叉树,对二叉树的先序遍历,中序遍历,后序遍历~~~~~~~~~~~
二叉树先序、中序、后序三种遍历的非递归算法
数据结构二叉树链式结构的前序遍历,中序遍历,后序遍历用递归的方法,层级遍历采用队列结构
二叉树的先序中序后序遍历 有递归与非递归两中做法
二叉树先序、中序、后序遍历(递归、非递归算法) 其中自己已经开发了栈!
数据结构试验报告用先序中序建立二叉树后序遍历非递归.pdf
一个实现二叉树遍历的程序代码,有先序,中序和后序。
数据结构中二叉树的先序遍历,中序遍历,后续遍历的递归和非递归的算法
用C语言实现数据结构中二叉树的前序中序后序遍历 int main()//主函数部分 { BiTree T=NULL; int Layer=0; int LayerT=0; printf("请输入二叉树:\n"); CreatBiTree(&T);printf("你输入的二叉树为:(竖型树状...
VC++实现二叉树先序创建,然后中序、先序、后序遍历,适合初学参考。
采用二叉链表存储先序建立二叉树,非递归中序遍历二叉树算法实现
数据结构中关于二叉树的遍历,非递归算法数上未给出
已知二叉树的中序和先序遍历可以唯一确定后序遍历、已知中序和后序遍历可以唯一确定先序遍历,但已知先序和后序,却不一定能唯一确定中序遍历。现要求根据输入的中序遍历结果及先序遍历结果,要求输出其后序遍历结果...
[问题描述] 建立二叉树,并输出二叉树的先序,中序和后序遍历序列,以及二叉树的叶子数。 [基本要求] 要求根据读取的元素建立二叉树,能输出各种遍历。 [实现提示] 可通过输入带空格的前序序列建立二叉链表。