JAVA递归、非递归遍历二叉树
2024-09-02 08:36:51
前序遍历:
1.访问根节点
2.前序遍历左子树
3.前序遍历右子树
中序遍历:
1.中序遍历左子树
2.访问根节点
3.中序遍历右子树
后序遍历:
1.后序遍历左子树
2.后序遍历右子树
3.访问根节点
---------------------
package design; import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Stack; public class BinTree {
char data;
BinTree leftChild;
BinTree rightChild;
public BinTree(char c) {
data = c;
}
public static void preSearch(BinTree root){
if(root !=null){
System.out.print(root.data);
preSearch(root.leftChild);
preSearch(root.rightChild);
}
}
public static void midSearch(BinTree root){
if(root !=null){
midSearch(root.leftChild);
System.out.print(root.data);
midSearch(root.rightChild);
}else{
return;
}
}
public static void postSearch(BinTree root){
if(root !=null){
postSearch(root.leftChild);
postSearch(root.rightChild);
System.out.print(root.data);
}
}
// 先序遍历非递归
public static void preOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
while(root !=null || !s.empty()){
while(root!=null){
System.out.print(root.data);
s.push(root);
root = root.leftChild;
}
if(!s.empty()){
root = s.pop();
root = root.rightChild;
}
}
}
// 中序遍历非递归
public static void midOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
while(root!=null || !s.empty()){
while(root!=null){
s.push(root);
root = root.leftChild;
}
if(!s.empty()){
root =s.pop();
System.out.print(root.data);
root = root.rightChild;
}
}
}
// 后序遍历非递归
public static void postOrder(BinTree root){
Stack<BinTree> s = new Stack<BinTree>();
Stack<Integer> s2 = new Stack<Integer>();
Integer i = new Integer();
while(root!=null || !s.empty()){
while(root!=null){
s.push(root);
s2.push(new Integer());
root = root.leftChild;
}
while(!s.empty() && s2.peek().equals(i)){
s2.pop();
System.out.print(s.pop().data);
}
if(!s.empty()){
s2.pop();
s2.push(new Integer());
root =s.peek();
root = root.rightChild;
}
}
}
//计算二叉树的深度
public static int level(BinTree root){
if(root == null){
return ;
}
return level(root.leftChild)+>level(root.rightChild)+?level(root.leftChild)+:level(root.rightChild)+; }
//层序遍历二叉树
public static void levelTrav(BinTree root) {
if (root == null)
return;
Queue<BinTree> q = new ArrayDeque<BinTree>();
q.add(root);
BinTree cur;
while (!q.isEmpty()) {
cur = q.peek();
System.out.print(cur.data + " ");
if (cur.leftChild != null)
q.add(cur.leftChild);
if (cur.rightChild != null)
q.add(cur.rightChild);
q.poll();
}
}
public static void main(String[] args) {
BinTree b1 = new BinTree('a');
BinTree b2 = new BinTree('b');
BinTree b3 = new BinTree('c');
BinTree b4 = new BinTree('d');
BinTree b5 = new BinTree('e'); /**
* a
* / \
* b c
* / \
* d e
*/
b1.leftChild = b2;
b1.rightChild = b3;
b2.leftChild = b4;
b2.rightChild = b5; BinTree.preSearch(b1);
System.out.println();
BinTree.preOrder(b1);
System.out.println("========================");
BinTree.midSearch(b1);
System.out.println("");
BinTree.midOrder(b1);
System.out.println("========================");
BinTree.postSearch(b1);
System.out.println();
BinTree.postOrder(b1);
System.out.println("========================");
System.out.println(BinTree.level(b1));
System.out.println("========================");
BinTree.levelTrav(b1);
}
}
最新文章
- 常见博客API
- 不使用border-radius,实现一个可复用的高度和宽度都自适应的圆角矩形
- 20151225jquery学习笔记---选项卡UI
- Asp.Net MVC Ajax
- JVM基础和调优(六)
- 开发该选择Blocks还是Delegates(转)
- crm管理系统
- 安装CentOS7精简版后的配置工作
- UGUI背包系统
- 第2章 Java基本语法(上): 变量与运算符
- 自动化测试中CSS SELECTOR选择器的一些写法
- 【转】全Javascript的Web开发架构:MEAN和Yeoman【译】
- 转载 vsftpd安装
- Android studio jni
- js delete删除对象属性,delete删除不了变量及原型链中的变量
- ab网站压力测试命令的参数、输出结果的中文注解
- vue.js指令v-model实现方法
- 在where子句中经常使用的运算符
- html-文本处理
- base64文件上传的问题