您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JAVA语言实现二叉树的层次遍历的非递归算法及递归算法

2024/7/4 17:47:08发布31次查看
/** 二叉树节点 */ public class btnode { private char key; private btnode left, right; public btnode(char key) { this(key, null, null); } public btnode(char key, btnode left, btnode right) { this.key = key; this.left = left; this.right = right; } public char getkey() { return key; } public void setkey(char key) { this.key = key; } public btnode getleft() { return left; } public void setleft(btnode left) { this.left = left; } public btnode getright() { return right; } public void setright(btnode right) { this.right = right; } } /** 二叉树遍历 */ public class bintree { protected btnode root; public bintree(btnode root) { this.root = root; } public btnode getroot() { return root; } /** 构造树 */ public static btnode init() { btnode a = new btnode('a'); btnode b = new btnode('b', null, a); btnode c = new btnode('c'); btnode d = new btnode('d', b, c); btnode e = new btnode('e'); btnode f = new btnode('f', e, null); btnode g = new btnode('g', null, f); btnode h = new btnode('h', d, g); return h;// root } /** 访问节点 */ public static void visit(btnode p) { system.out.print(p.getkey() + ); } /** 递归实现前序遍历 */ protected static void preorder(btnode p) { if (p != null) { visit(p); preorder(p.getleft()); preorder(p.getright()); } } /** 递归实现中序遍历 */ protected static void inorder(btnode p) { if (p != null) { inorder(p.getleft()); visit(p); inorder(p.getright()); } } /** 递归实现后序遍历 */ protected static void postorder(btnode p) { if (p != null) { postorder(p.getleft()); postorder(p.getright()); visit(p); } } /** 非递归实现前序遍历 */ protected static void iterativepreorder(btnode p) { stack stack = new stack(); if (p != null) { stack.push(p); while (!stack.empty()) { p = stack.pop(); visit(p); if (p.getright() != null) stack.push(p.getright()); if (p.getleft() != null) stack.push(p.getleft()); } } } /** 非递归实现后序遍历 */ protected static void iterativepostorder(btnode p) { btnode q = p; stack stack = new stack(); while (p != null) { // 左子树入栈 for (; p.getleft() != null; p = p.getleft()) stack.push(p); // 当前节点无右子或右子已经输出 while (p != null && (p.getright() == null || p.getright() == q)) { visit(p); q = p;// 记录上一个已输出节点 if (stack.empty()) return; p = stack.pop(); } // 处理右子 stack.push(p); p = p.getright(); } } /** 非递归实现中序遍历 */ protected static void iterativeinorder(btnode p) { stack stack = new stack(); while (p != null) { while (p != null) { if (p.getright() != null) stack.push(p.getright());// 当前节点右子入栈 stack.push(p);// 当前节点入栈 p = p.getleft(); } p = stack.pop(); while (!stack.empty() && p.getright() == null) { visit(p); p = stack.pop(); } visit(p); if (!stack.empty()) p = stack.pop(); else p = null; } } public static void main(string[] args) { bintree tree = new bintree(init()); system.out.print( pre-order:); preorder(tree.getroot()); system.out.println(); system.out.print( in-order:); inorder(tree.getroot()); system.out.println(); system.out.print(post-order:); postorder(tree.getroot()); system.out.println(); system.out.print( pre-order:); iterativepreorder(tree.getroot()); system.out.println(); system.out.print( in-order:); iterativeinorder(tree.getroot()); system.out.println(); system.out.print(post-order:); iterativepostorder(tree.getroot()); system.out.println(); } }
/*遍历是对树的一种最基本的运算,所谓遍历二叉树,就是按一定的规则和顺序走遍二叉树的所有结点,使每一个结点都被访问一次,而且只被访问一次。由于二叉树是非线性结构,因此,树的遍历实质上是将二叉树的各个结点转换成为一个线性序列来表示。 设l、d、r分别表示遍历左子树、访问根结点和遍历右子树, 则对一棵二叉树的遍历有三种情况:dlr(称为先根次序遍历),ldr(称为中根次序遍历),lrd (称为后根次序遍历)。 (1)先序遍历 访问根;按先序遍历左子树;按先序遍历右子树 (2)中序遍历 按中序遍历左子树;访问根;按中序遍历右子树 (3)后序遍历 按后序遍历左子树;按后序遍历右子树;访问根 (4)层次遍历 即按照层次访问,通常用队列来做。访问根,访问子女,再访问子女的子女(越往后的层次越低)(两个子女的级别相同) */
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product