java树的非递归遍历_java二叉树的非递归遍历

二叉树的递归遍历比较简单,这里就不聊了。今天主要聊聊二叉树的非递归遍历,主要借助于“栈”后进先出的特性来保存节点的顺序,先序遍历和中序遍历相对来说比较简单,重点理解后序遍历。

1. 先看看节点类型:

//二叉树的节点类型

private class node{

int data; //节点值

node leftchild; //左孩子

node rightchild; //右孩子

public node(int data) {

this.data=data;

}

}

2.先序遍历。

非递归先序遍历的思路如下:

1.先将根节点入栈

2.访问根节点

3.如果根节点存在右孩子,则将右孩子入栈

4.如果根节点存在左孩子,则将左孩子入栈(注意:一定是右孩子先入栈,然后左孩子入栈)

5.重复2-4

public void preorder(node root) {

if(root==null) {

system.out.println("空树");

return;

}

node tmp=root;

stack s=new stack();

s.push(tmp); //根节点入栈

while(!s.empty()) {

//1.访问根节点

node p=s.pop();

system.out.print(p.data+" ");

//2.如果根节点存在右孩子,则将右孩子入栈

if(p.rightchild!=null) {

s.push(p.rightchild);

}

//3.如果根节点存在左孩子,则将左孩子入栈

if(p.leftchild!=null) {

s.push(p.leftchild);

}

}

system.out.println();

}

3.中序遍历。

非递归中序遍历的思路如下:

1.先将根节点入栈

2.将当前节点的所有左孩子入栈,直到左孩子为空

3.访问栈顶元素,如果栈顶元素存在右孩子,则继续第2步

4.重复第2、3步,直到栈为空并且所有的节点都被访问

public void inorder(node root) {

if(root==null) {

system.out.println("空树");

return;

}

node tmp=root;

stack s=new stack();

while(tmp!=null || !s.empty()) {

//1.将根节点入栈

//2.将所有左孩子入栈

while(tmp!=null) {

s.push(tmp);

tmp=tmp.leftchild;

}

//3.访问栈顶元素

tmp=s.pop();

system.out.print(tmp.data+" ");

//4.如果栈顶元素存在右孩子,则将右孩子赋值给tmp,也就是将右孩子入栈

if(tmp.rightchild!=null) {

tmp=tmp.rightchild;

}

//否则,将tmp置为null,表示下次要访问的是栈顶元素

else {

tmp=null;

}

}

system.out.println();

}

4.后序遍历。

后续遍历的非递归实现思路:

1.根节点入栈

2.将根节点的左子树入栈,直到最左,没有左孩子为止

3.得到栈顶元素的值,先不访问,判断栈顶元素是否存在右孩子,如果存在并且没有被访问,则将右孩子入栈,否则,就访问栈顶元素

public void postorder(node root) {

if(root==null) {

system.out.println("空树");

return;

}

node tmp=root; //当前节点

node prev=null; //上一次访问的节点

stack s=new stack();

while(tmp!=null || !s.empty()) {

//1.将根节点及其左孩子入栈

while(tmp!=null) {

s.push(tmp);

tmp=tmp.leftchild;

}

if(!s.empty()) {

//2.获取栈顶元素值

tmp=s.peek();

//3.没有右孩子,或者右孩子已经被访问过

if(tmp.rightchild==null || tmp.rightchild==prev) {

//则可以访问栈顶元素

tmp=s.pop();

system.out.print(tmp.data+" ");

//标记上一次访问的节点

prev=tmp;

tmp=null;

}

//4.存在没有被访问的右孩子

else {

tmp=tmp.rightchild;

}

}

}

system.out.println();

}

利用非递归算法来搜索二叉树中的某个元素java

层序遍历

可以利用层序遍历来解决这个问题

代码

boolean searchusinglevelorder(binarytreenode root,int data){

binarytreenode temp;

llqueue q = new llqueue();

if(root == null)

return false;

q.enqueue(root);

while(q.isnotempty()){

temp = q.dequeue();

if(data == root.getdata())

return true;

if(temp.getleft() != null)

q.enqueue(temp.getleft());

if(temp.getright() != null)

q.enqueue(temp.getright());

}

q.deletequeue();

return false;

}

java递归、非递归实现二叉树遍历

最近找工作做笔试题发现很重要,就自己写了一点,和大家分享

import java.util.stack;

import java.util.hashmap;

public class bintree {

private char date;

private bintree lchild;

private bintree rchild;

public bintree(char c) {

date = c;

}

// 先序遍历递归

public static void preorder(bintree t) {

if (t == null) {

return;

}

system.out.print(t.date);

preorder(t.lchild);

preorder(t.rchild);

}

// 中序遍历递归

public static void inorder(bintree t) {

if (t == null) {

return;

}

inorder(t.lchild);

system.out.print(t.date);

inorder(t.rchild);

}

// 后序遍历递归

public static void postorder(bintree t) {

if (t == null) {

return;

}

postorder(t.lchild);

postorder(t.rchild);

system.out.print(t.date);

}

// 先序遍历非递归

public static void preorder2(bintree t) {

stack s = new stack();

while (t != null || !s.empty()) {

while (t != null) {

system.out.print(t.date);

s.push(t);

t = t.lchild;

}

if (!s.empty()) {

t = s.pop();

t = t.rchild;

}

}

}

// 中序遍历非递归

public static void inorder2(bintree t) {

stack s = new stack();

while (t != null || !s.empty()) {

while (t != null) {

s.push(t);

t = t.lchild;

}

if (!s.empty()) {

t = s.pop();

system.out.print(t.date);

t = t.rchild;

}

}

}

// 后序遍历非递归

public static void postorder2(bintree t) {

stack s = new stack();

stack s2 = new stack();

integer i = new integer(1);

while (t != null || !s.empty()) {

while (t != null) {

s.push(t);

s2.push(new integer(0));

t = t.lchild;

}

while (!s.empty() && s2.peek().equals(i)) {

s2.pop();

system.out.print(s.pop().date);

}

if (!s.empty()) {

s2.pop();

s2.push(new integer(1));

t = s.peek();

t = t.rchild;

}

}

}

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.lchild = b2;

b1.rchild = b3;

b2.lchild = b4;

b2.rchild = b5;

bintree.preorder(b1);

system.out.println();

bintree.preorder2(b1);

system.out.println();

bintree.inorder(b1);

system.out.println();

bintree.inorder2(b1);

system.out.println();

bintree.postorder(b1);

system.out.println();

bintree.postorder2(b1);

}

}

到此这篇关于java二叉树的非递归遍历的文章就介绍到这了,更多相关java二叉树内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java二叉树非递归遍历可以通过使用栈数据结构来实现。首先,我们创建一个空的栈,将根节点入栈。然后,我们进入一个循环,直到栈为空为止。在每一次循环中,我们弹出栈顶元素,并将其访问。接下来,如果该节点有右子节点,则将右子节点入栈。如果该节点有左子节点,则将左子节点入栈。由于栈是先进后出的数据结构,所以我们先入栈右子节点,再入栈左子节点,以确保在遍历过程中先访问左子节点。这样就能够实现二叉树非递归遍历。 以下是一个示例代码实现二叉树非递归中序遍历: ```java public void inorderTraversal(Node root) { if (root == null) { return; } Stack<Node> stack = new Stack<>(); Node current = root; while (current != null || !stack.isEmpty()) { while (current != null) { stack.push(current); current = current.leftChild; } current = stack.pop(); System.out.print(current.data + " "); // 访问节点 current = current.rightChild; } } ``` 在这个示例代码中,我们首先判断当前节点是否为空或者栈是否为空,如果不满足则进入循环。在循环内部,我们首先将当前节点及其所有左子节点入栈,直到当前节点为空。然后,我们弹出栈顶节点并访问该节点。最后,将当前节点更新为其右子节点,并继续下一次循环。 通过这种方式,我们可以实现二叉树非递归中序遍历。你可以根据需要修改代码实现其他类型的非递归遍历,比如前序遍历和后序遍历。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [用Python实现二叉树二叉树非递归遍历及绘制的例子](https://download.csdn.net/download/weixin_38618784/14869891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [java实现二叉树非递归遍历](https://blog.csdn.net/weixin_41826973/article/details/105555647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值