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. DataGrip:Error encountered when performing Introspect schema xxx 错误的解决方法

    datagrip的问题,转载自: https://www.cnblogs.com/geb515/p/7995249.html 把Introspect using JDBC _metadata打上勾 然 ...

  2. Django实现自动发布(2视图-服务管理)

    通常页面要能对资源进行增删改查,对应http的 POST.DELETE.UPDATE.GET 页面显示使用了layui,而layui的表格有自己的数据获取方式,所以我们的视图要做一些调整,不使用后端渲 ...

  3. linux下使用clamav排查病毒

    clamav wget http://www.clamav.net/downloads/production/clamav-0.102.0.tar.gz ### Installyum -y insta ...

  4. 超实用!手把手教你如何用MSF进行后渗透测试!

    在对目标进行渗透测试的时候,通常情况下,我们首先获得的是一台web服务器的webshell或者反弹shell,如果权限比较低,则需要进行权限提升:后续需要对系统进行全面的分析,搞清楚系统的用途:如果目 ...

  5. epool与select有什么区别

    select在一个进程中打开的最大fd是有限制的,由FD_SETSIZE设置,默认值是2048.不过 epoll则没有这个限制,它所支持的fd上限是最大可以打开文件的数目,这个数字一般远大于2048, ...

  6. 微信支付:URL未注册问题

    起因:一个项目已经做好了,微信支付也调通的,域名 www.xxxx.com ,某天客户需要换域名,改为weixin.xxxx.com, 原先的www转向客户自己的官网,结果换了之后,发现微信支付出错: ...

  7. android分渠道打包,监测日活量统计(基于友盟SDK)

    客服说要看App日活,让加个统计功能. (我们技术部已经混到客服部都能直接提需求的地步) 首先接入友盟统计的SDK,在项目外层的build.gradle中添加依赖'https://dl.bintray ...

  8. GIT删除本地tag和远程tag

    以tag test为例,这个tag已经同步到远程,但是现在发现了一个问题,需要撤回该tag,git命令如下: 删除本地tag: git tag -d test 删除远程tag: git push or ...

  9. axios 设置headers token

    axios({ method:"put", url:"....", data:{"action":"refreshToken&qu ...

  10. svn"重新定位"提示版本库uuid不匹配

    svn"重新定位"提示版本库 uuid不匹配: 版本库 "https://wolfcome110/svn/andon" 的 uuid是 "d52648 ...