二叉树的递归,非递归遍历,深度优先遍历,广度优先遍历
2016-07-04 00:00
671 查看
摘要: 二叉树的递归,非递归遍历,深度优先遍历,广度优先遍历
结果:
二叉树的递归先序遍历
A B D E H I C F J G P
二叉树的非递归先序遍历
A B D E H I C F J G P
二叉树的递归中序遍历
D B H E I A F J C P G
二叉树的非递归中序遍历
D B H E I A F J C P G
二叉树的递归后序遍历
D H I E B J F P G C A
二叉树的非递归后序遍历
D H I E B J F P G C A
数的深度优先遍历
A B D E H I C F J G P
数的广度优先遍历
A B C D E F G H I J P
/** * 二叉树的定义和遍历 */ public class BinaryTree { /** * 打印节点的信息 */ public void printNode(TreeNode<String> node){ System.out.print(node.getData()+" "); } /** * 递归先序遍历二叉树 */ public void preIterator(TreeNode<String> node){ this.printNode(node); if (node.getLefNode() != null) { this.preIterator(node.getLefNode()); } if (node.getRigNode() != null) { this.preIterator(node.getRigNode()); } } /** * 递归中序遍历二叉树 */ public void midIterator(TreeNode<String> node){ if (node.getLefNode() != null) { midIterator(node.getLefNode()); } this.printNode(node); if (node.getRigNode() != null) { midIterator(node.getRigNode()); } } /** * 递归后续遍历二叉树 */ public void lasIterator(TreeNode<String> node){ if (node.getLefNode() != null) { this.lasIterator(node.getLefNode()); } if (node.getRigNode() != null) { this.lasIterator(node.getRigNode()); } this.printNode(node); } /** * 非递归先序遍历二叉树 * @return */ public void noDiGuiPreIterator(TreeNode<String> node) { Stack<TreeNode<String>> stack = new Stack<>(); TreeNode<String> p = node; while (p != null || stack.size() > 0) { while(p != null){ //压入所有的左节点,压入前访问它。左节点压入完后pop访问右节点。 this.printNode(p); stack.push(p); p = p.getLefNode(); } if(stack.size() > 0){ p = stack.pop(); p = p.getRigNode(); } } } /** * 非递归中序遍历二叉树 * @return */ public void noDiGuiMidIterator(TreeNode<String> node){ Stack<TreeNode<String>> stack = new Stack<>(); TreeNode<String> p = node; while (p != null || stack.size() > 0) { while(p != null){ stack.push(p); p = p.getLefNode(); } if(stack.size() > 0){ p = stack.pop(); this.printNode(p); p = p.getRigNode(); } } } /** * 非递归后续遍历二叉树 * @return */ public void noDiGuiLasIterator(TreeNode<String> node){ Stack<TreeNode<String>> stack = new Stack<>(); TreeNode<String> p = node; TreeNode<String> prev = node; while (p != null || stack.size() > 0) { while (p != null) { stack.push(p); p = p.getLefNode(); } if (stack.size() > 0) { TreeNode<String> temp = stack.peek().getRigNode(); if (temp == null || temp == prev) { p = stack.pop(); this.printNode(p); prev = p; p = null; } else { p = temp; } } } } /** * 不仅是二叉树,其他的多叉数也适合 * 深度优先遍历 */ public void depthOrderTraversal(TreeNode<String> node){ Stack<TreeNode<String>> stack=new Stack<>(); if (node != null){ stack.push(node); } while(stack.isEmpty()==false){ TreeNode<String> p = stack.pop(); this.printNode(p); //栈是后进先出,因为先遍历左节点,所有左节点要后添加,即先添加右节点。 if(p.getRigNode() != null){ stack.push(p.getRigNode()); } if(p.getLefNode() != null){ stack.push(p.getLefNode()); } } System.out.print("\n"); } /** * 不仅适合二叉树,也适合其他的多叉树 * 广度优先遍历 */ public void levelOrderTraversal(TreeNode<String> node){ Queue<TreeNode<String>> queue=new ArrayDeque<>(); queue.add(node); while(queue.isEmpty()==false){ TreeNode<String> p = queue.remove(); this.printNode(p); //队列是先进先出,所有先放左子树,先遍历左边的节点 if(p.getLefNode() != null){ queue.add(p.getLefNode()); } if(p.getRigNode() != null){ queue.add(p.getRigNode()); } } } // 初始化二叉树 public TreeNode<String> init() { TreeNode<String> D = new TreeNode<String>("D", null, null); TreeNode<String> H = new TreeNode<String>("H", null, null); TreeNode<String> I = new TreeNode<String>("I", null, null); TreeNode<String> J = new TreeNode<String>("J", null, null); TreeNode<String> P = new TreeNode<String>("P", null, null); TreeNode<String> G = new TreeNode<String>("G", P, null); TreeNode<String> F = new TreeNode<String>("F", null, J); TreeNode<String> E = new TreeNode<String>("E", H, I); TreeNode<String> B = new TreeNode<String>("B", D, E); TreeNode<String> C = new TreeNode<String>("C", F, G); TreeNode<String> A = new TreeNode<String>("A", B, C); return A; } /** * 测试 * @param args */ public static void main(String[] args) { BinaryTree binaryTree = new BinaryTree(); TreeNode<String> node = binaryTree.init(); System.out.println("二叉树的递归先序遍历"); binaryTree.preIterator(node); System.out.println("\n二叉树的非递归先序遍历"); binaryTree.noDiGuiPreIterator(node); System.out.println("\n二叉树的递归中序遍历"); binaryTree.midIterator(node); System.out.println("\n二叉树的非递归中序遍历"); binaryTree.noDiGuiMidIterator(node); System.out.println("\n二叉树的递归后序遍历"); binaryTree.lasIterator(node); System.out.println("\n二叉树的非递归后序遍历"); binaryTree.noDiGuiLasIterator(node); System.out.println("\n数的深度优先遍历"); binaryTree.depthOrderTraversal(node); System.out.println("\n数的广度优先遍历"); binaryTree.levelOrderTraversal(node); } } /** * 定义节点 */ class TreeNode<T>{ private T data; private TreeNode<T> lefNode; private TreeNode<T> rigNode; public TreeNode(T data, TreeNode<T> lefNode, TreeNode<T> rigNode) { super(); this.data = data; this.lefNode = lefNode; this.rigNode = rigNode; } public T getData() { return data; } public void setData(T data) { this.data = data; } public TreeNode<T> getLefNode() { return lefNode; } public void setLefNode(TreeNode<T> lefNode) { this.lefNode = lefNode; } public TreeNode<T> getRigNode() { return rigNode; } public void setRigNode(TreeNode<T> rigNode) { this.rigNode = rigNode; } }
结果:
二叉树的递归先序遍历
A B D E H I C F J G P
二叉树的非递归先序遍历
A B D E H I C F J G P
二叉树的递归中序遍历
D B H E I A F J C P G
二叉树的非递归中序遍历
D B H E I A F J C P G
二叉树的递归后序遍历
D H I E B J F P G C A
二叉树的非递归后序遍历
D H I E B J F P G C A
数的深度优先遍历
A B D E H I C F J G P
数的广度优先遍历
A B C D E F G H I J P
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- 一波二叉树遍历问题的C++解答实例分享
- 举例讲解C语言程序中对二叉树数据结构的各种遍历方式
- C++非递归队列实现二叉树的广度优先遍历
- PHP实现的线索二叉树及二叉树遍历方法详解
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- C#非递归先序遍历二叉树实例
- C++将二叉树转为双向链表及判断两个链表是否相交
- C++非递归建立二叉树实例
- C语言实现找出二叉树中某个值的所有路径的方法
- C++实现二叉树遍历序列的求解方法
- C语言实现二叉树遍历的迭代算法
- C++实现查找二叉树中和为某一值的所有路径的示例
- 用C语言判断一个二叉树是否为另一个的子结构
- C++实现二叉树非递归遍历方法实例总结
- C++二叉树结构的建立与基本操作
- 深入遍历二叉树的各种操作详解(非递归遍历)
- 平衡二叉树