二叉树的递归遍历比较简单,这里就不聊了。今天主要聊聊二叉树的非递归遍历,主要借助于“栈”后进先出的特性来保存节点的顺序,先序遍历和中序遍历相对来说比较简单,重点理解后序遍历。
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二叉树内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
希望与广大网友互动??
点此进行留言吧!