二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度计算
输入格式:如 abd###ce##f##*
package tree;
//二叉树的二叉链表实现
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class BTree<AnyType> {
BTNode rootNode=new BTNode();
class BTNode<AnyType>{
char data;
BTNode<AnyType> leftChildNode;
BTNode<AnyType> rightChildNode;
public BTNode(){
data=0;
leftChildNode=rightChildNode=null;
}
public BTNode(char data){
this.data=data;
leftChildNode=rightChildNode=null;
}
public BTNode(char data,BTNode leftChildNode,BTNode rightChildNode){
this.data=data;
leftChildNode=leftChildNode;
rightChildNode=rightChildNode;
}
}
//先序创建二叉树
char d[]=new char[100];
int i=0;
public BTNode creatBTree(){
BTNode node=null;
if(d[i]!='*'){
if(d[i]=='#'){
node=null;
i++;
}
else{
node=new BTNode(d[i]);
i++;
node.leftChildNode=creatBTree();
node.rightChildNode=creatBTree();
}
}
return node;
}
//先序递归遍历
public void preOrder(BTNode<AnyType> t){
if(t!=null){
System.out.print(t.data);
preOrder(t.leftChildNode);
preOrder(t.rightChildNode);
}
}
//先序非递归遍历
public void preStackOrder(BTNode t){
Stack s=new Stack();
BTNode p=t;
while(p!=null&&s.isEmpty()!=true){
if(p!=null){
System.out.print(p.data);
s.push(p);
p=p.leftChildNode;
}
if(s.isEmpty()){
s.pop();
p=p.rightChildNode;
}
}
}
//中序递归遍历
public void inOrder(BTNode t){
if(t!=null){
inOrder(t.leftChildNode);
System.out.print(t.data);
inOrder(t.rightChildNode);
}
}
//中序非递归遍历
public void inStackOrder(BTNode t){
Stack<BTNode> s=new Stack<BTNode>();
BTNode p=t;
while(p!=null&&s.isEmpty()){
if(p!=null){
s.push(p);
p=p.leftChildNode;
}
if(s.isEmpty()!=true){
p=s.pop();
System.out.print(p.data);
p=p.rightChildNode;
}
}
}
//后序递归遍历
public void postOrder(BTNode t){
if(t!=null){
postOrder(t.leftChildNode);
postOrder(t.rightChildNode);
System.out.print(t.data);
}
}
//后序非递归遍历
public void postStackOrder(BTNode t){
Stack<BTNode> s=new Stack<BTNode>();
Stack<Integer> ss=new Stack<Integer>();
Integer i=new Integer(1);
BTNode p=t;
BTNode q=t;
while(p!=null||s.isEmpty()!=true){
while(p!=null){
s.push(p);
ss.push(new Integer(0));
p=p.leftChildNode;
}
while(s.isEmpty()!=true&&ss.peek().equals(i)){
ss.pop();
q=s.pop();
System.out.print(q.data);
}
if(s.isEmpty()!=true){
ss.pop();
ss.push(i);
p=s.peek();
p=p.rightChildNode;
}
}
}
//层次非递归遍历
public void levelQueueOrder(BTNode t){
Queue<BTNode> q=new LinkedList<BTNode>();
q.add(t);
while(q.isEmpty()!=true){
BTNode step=q.remove();
System.out.print(step.data);
if(step.leftChildNode!=null){
q.add(step.leftChildNode);
}
if(step.rightChildNode!=null){
q.add(step.rightChildNode);
}
}
}
//计算二叉树深度
public int depth(BTNode t){
int leftDepth,rightDepth;
if(t==null)
return 0;
leftDepth=depth(t.leftChildNode);
rightDepth=depth(t.rightChildNode);
return Math.max(leftDepth,rightDepth)+1;
}
//叶子结点个数
int num=0;
public int leaf(BTNode t){
if(t!=null){
if(t.leftChildNode==null&&t.rightChildNode==null)
num++;
leaf(t.leftChildNode);
leaf(t.rightChildNode);
}
return num;
}
public static void main(String[] args) {
BTree bt=new BTree();
Scanner sc=new Scanner(System.in);
String a=sc.next();
char c[]=a.toCharArray();
for(int i=0;i<c.length;i++){
bt.d[i]=c[i];
}
bt.rootNode=bt.creatBTree();
System.out.println("先序遍历");
bt.preOrder(bt.rootNode);
System.out.println();
System.out.println("中序遍历");
bt.inOrder(bt.rootNode);
System.out.println();
System.out.println("后序遍历");
bt.postOrder(bt.rootNode);
System.out.println();
System.out.println("后序非递归遍历");
bt.postStackOrder(bt.rootNode);
System.out.println("层次遍历");
bt.levelQueueOrder(bt.rootNode);
System.out.println();
System.out.println("二叉树深度");
System.out.println(bt.depth(bt.rootNode));
System.out.println("叶子结点个数");
System.out.println(bt.leaf(bt.rootNode));
}
}
分享到:
相关推荐
二叉树先序、中序、后序遍历非递归算法,简述了二叉树的基本算法。
用C++写的二叉树先序遍历、中序遍历和后序遍历非递归算法
用C++写的,包括二叉树的构建,二叉树的先序遍历、中序遍历和后序遍历非递归算法。
二叉树先序、中序、后序三种遍历的非递归算法
二叉树的递归遍历,中序遍历,先序遍历,后序遍历,通过学习二叉树的遍历,可以让我们更紧一步掌握数据的遍历
二叉树的先序,中序,后序递归,非递归遍历和广度优先遍历。此程序是用C语言写的。
二叉树先序、中序、后序遍历(递归、非递归算法) 其中自己已经开发了栈!
已知二叉树的中序和先序遍历可以唯一确定后序遍历、已知中序和后序遍历可以唯一确定先序遍历,但已知先序和后序,却不一定能唯一确定中序遍历。现要求根据输入的中序遍历结果及先序遍历结果,要求输出其后序遍历结果...
VC++实现二叉树先序创建,然后中序、先序、后序遍历,适合初学参考。
数据结构中关于二叉树的遍历,非递归算法数上未给出
二叉树的几种操作,包括递归先序建立二叉树、先序遍历、中序遍历、后序遍历、非递归的各种遍历、计算叶子节点数目和所有节点数目,使用队列实现二叉树的层次遍历.zip
[问题描述] 建立二叉树,并输出二叉树的先序,中序和后序遍历序列,以及二叉树的叶子数。 [基本要求] 要求根据读取的元素建立二叉树,能输出各种遍历。 [实现提示] 可通过输入带空格的前序序列建立二叉链表。
c语言,二叉树的先序,中序,后序遍历以及叶子结点数目
数据结构试验报告用先序中序建立二叉树后序遍历非递归.pdf
C语言,数据结构课程,知道中序和后序遍历,画二叉树和写出前序遍历。
从键盘输入二叉树的各结点... 2 )分别实现先序、中序、后序递归遍历二叉树 3 )输出二叉树的高度 4 )输出二叉树的按层次遍历序列 5 )输出二叉树的先序非递归遍历下的结点访问次序 6 )以菜单方式运行
数据结构C++二叉链表的先序遍历、中序遍历和后序遍历实现
二叉树的先序中序后序遍历 有递归与非递归两中做法
(1)输入字符序列,建立二叉链表 (2)中序遍历二叉树:递归 (3)中序遍历二叉树:非递归 (3)二叉树高度