import java.util.LinkedList;
import java.util.Stack; public class BinarySearchTree1<E extends Comparable <? super E>>{ private static class BinaryNode<E> { E element;
BinaryNode<E> left;
BinaryNode<E> right; BinaryNode(E theElement) {
this(theElement, null, null);
} BinaryNode(E theElement, BinaryNode<E> lt, BinaryNode<E> rt) {
element = theElement;
left = lt;
right = rt;
} } private BinaryNode<E>root;
public void insert(E x){
root = insert(x,root);
} private BinaryNode<E> insert(E x, BinaryNode<E> t){ if (t == null){
return new BinaryNode<>(x,null,null);
}
int compareResult = x.compareTo(t.element);
if (compareResult < 0){
t.left = insert(x,t.left);
}else if (compareResult > 0){
t.right = insert(x,t.right);
}else {
;
}
return t; } // 前序遍历递归
public void PreOrder(BinaryNode<E> Node){
if (Node != null){
System.out.print(Node.element + " ");
PreOrder(Node.left);
PreOrder(Node.right);
}
}
// 前序遍历非递归
public void preOrder(BinaryNode<E> Node){
if (Node == null){
return;
}
Stack<BinaryNode<E>> s = new Stack<>();
while (Node != null || !s.empty()) {
if (Node != null) {
System.out.print(Node.element + " ");
s.push(Node);
Node = Node.left;
} else {
Node = s.pop();
Node = Node.right;
}
}
}
// 中序遍历递归
public void MidOrder(BinaryNode<E> Node){
if (Node != null){
MidOrder(Node.left);
System.out.print(Node.element + " ");
MidOrder(Node.right);
}
}
// 中序遍历非递归
public void midOrder(BinaryNode<E> Node){
if (Node == null){
return;
}
Stack<BinaryNode<E>> s = new Stack<>();
while (Node != null || !s.empty()){
if (Node != null) {
s.push(Node);
Node = Node.left;
}else {
Node = s.pop();
System.out.print(Node.element + " ");
Node = Node.right;
}
}
}
// 后序遍历递归
public void PosOrder(BinaryNode<E> Node){
if (Node != null){
PosOrder(Node.left);
PosOrder(Node.right);
System.out.print(Node.element + " ");
}
}
// 后序遍历非递归
public void posOrder(BinaryNode<E> Node){
if (Node == null){
return;
}
Stack<BinaryNode<E>> s = new Stack<>();
BinaryNode<E> NowNode;
BinaryNode<E> BefNode;
NowNode = Node;
BefNode = Node;
//把NowNode移到左子树下边
while (NowNode != null){
s.push(NowNode);
NowNode = NowNode.left;
}
while (s != null){
NowNode = s.pop();
//无右子树或右子树已被访问
if (NowNode.right != null && NowNode.right != BefNode){
s.push(NowNode);
NowNode = NowNode.left;
if (NowNode != null){
s.push(NowNode);
NowNode = NowNode.left;
}
}else {
System.out.print(NowNode.element + " ");
BefNode = NowNode;
}
}
}
// 层序遍历队列
public void levelOrder(BinaryNode<E> Node){
LinkedList<BinaryNode<E>> list = new LinkedList<>();
list.add(Node);
while (!list.isEmpty()){
Node = list.poll();
System.out.print(Node.element + " ");
if (Node.left != null){
list.offer(Node.left);
}
if (Node.right != null){
list.offer(Node.right);
}
} } // 树的深度
public int Depth(BinaryNode<E> Node){ if (Node == null){
return 0;
}
int l = Depth(Node.left);
int r = Depth(Node.right);
if (l > r){
return l+1;
}else {
return r+1;
} } // 桉树状打印二叉树
public void PrintTree(BinaryNode<E> Node,int high){
if (Node == null){
return;
}
PrintTree(Node.right,high+1);
for (int i = 0 ; i < high ; i++) {
System.out.print(" ");
}
System.out.println(Node.element + " ");
PrintTree(Node.left,high+1);
} public static void main(String[] args) {
int [] input = {4,2,6,1,3,5,7,8,10};
BinarySearchTree1<Integer> tree = new BinarySearchTree1<>();
for (int i = 0 ; i < input.length ; i++){
tree.insert(input[i]);
}
System.out.print("递归前序遍历: ");
tree.PreOrder(tree.root);
System.out.println();
System.out.print("非递归前序遍历:");
tree.preOrder(tree.root);
System.out.println();
System.out.print("递归中序遍历: ");
tree.MidOrder(tree.root);
System.out.println();
System.out.print("非递归中序遍历:");
tree.midOrder(tree.root);
System.out.println();
System.out.print("递归后序遍历: ");
tree.PosOrder(tree.root);
System.out.println();
// System.out.print("非递归后序遍历:");
// tree.posOrder(tree.root);
// System.out.println();
System.out.print("队列层序遍历: ");
tree.levelOrder(tree.root);
System.out.println();
tree.PrintTree(tree.root,tree.Depth(tree.root));
} }

Java实现树的遍历以及打印(递归,非递归)的更多相关文章

  1. Reverse Linked List 递归非递归实现

    单链表反转--递归非递归实现 Java接口: ListNode reverseList(ListNode head) 非递归的实现 有2种,参考 头结点插入法 就地反转 递归的实现 1) Divide ...

  2. 【数据结构】——搜索二叉树的插入,查找和删除(递归&非递归)

    一.搜索二叉树的插入,查找,删除 简单说说搜索二叉树概念: 二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树 若它的左子树不为空,则左子树上所有节点的值都小于根节点的值 若它的右 ...

  3. 树的广度优先遍历和深度优先遍历(递归非递归、Java实现)

    在编程生活中,我们总会遇见树性结构,这几天刚好需要对树形结构操作,就记录下自己的操作方式以及过程.现在假设有一颗这样树,(是不是二叉树都没关系,原理都是一样的) 1.广度优先遍历 英文缩写为BFS即B ...

  4. Java实现二叉树的创建、递归/非递归遍历

    近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...

  5. 二叉树的递归,非递归遍历(java)

    import java.util.Stack; import java.util.HashMap; public class BinTree { private char date; private ...

  6. 二叉树总结—建树和4种遍历方式(递归&&非递归)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/27967155 今天总结一下二叉树.要考离散了 ...

  7. 二叉树的先序、中序以及后序遍历(递归 && 非递归)

    树节点定义: class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } 递归建立二 ...

  8. 二叉树的递归,非递归遍历(C++)

    二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的.对于二叉树,有前序.中序以及后序三种遍历方法.因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易 ...

  9. 递归/非递归----python深度遍历二叉树(前序遍历,中序遍历,后序遍历)

    递归代码:递归实现很简单 '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...

随机推荐

  1. 【技术博客】基于JsPlumb和JQuery-UI的流程图的保存和再生成

    开发组在开发过程中,都不可避免地遇到了一些困难或问题,但都最终想出办法克服了.我们认为这样的经验是有必要记录下来的,因此就有了[技术博客]. 基于JsPlumb和JQuery-UI的流程图的保存和再生 ...

  2. [技术博客]使用CDN加快网站访问速度

    [技术博客]使用CDN加快网站访问速度 2s : most users are willing to wait 10s : the limit for keeping the user's atten ...

  3. Java NIO 文件通道使用

    读取一个文件的内容,然后写入另外一个文件 public class NioTest4 { public static void main(String[] args) throws Exception ...

  4. win10-mysql卸载干净

    本文介绍,在Windows10系统下,如何彻底删除卸载MySQL 1.停止MySQL服务 开始——所有应用——Windows管理工具——服务,将MySQL服务停止. 2.卸载mysql server ...

  5. maven本地仓库已经有了所需的jar包,为什么还要去请求远程仓库

    问题 IDEA 中的maven 项目,一个jar包一直导入不进来,reimport 无效.从另一仓库把这个jar包拷贝到当前仓库,还是无效.mvn clean install -e U 发现加载这个j ...

  6. Python向excel中写入数据的方法 方法简单

    最近做了一项工作需要把处理的数据写入到Excel表格中进行保存,所以在此就简单介绍使用Python如何把数据保存到excel表格中. 数据导入之前需要安装 xlwt依赖包,安装的方法就很简单,直接 p ...

  7. 案例:执行 JavaScript 语句

    隐藏百度图片 # coding=utf-8 from selenium import webdriver driver = webdriver.PhantomJS(executable_path=r' ...

  8. 最好用的Redis Desktop Manager 0.9.3 版本下载

    因为Redis Desktop Manager作者在 0.9.4 版本之后选择对所有的安装包收费,不再提供安装包下载,但是源码依旧公开. github 上有 redis destop manager ...

  9. java html实体转义

    public static void main(String[] args) { String str = " -<->-&-"-&apos;" ...

  10. 深入浅出依赖注入容器——Autofac

    1.写在前面 相信大家对IOC和DI都耳熟能详,它们在项目里面带来的便利大家也都知道,微软新出的.NetCore也大量采用了这种手法. 如今.NetCore也是大势所趋了,基本上以.Net为技术主导的 ...