树——二叉树的遍历之递归实现
来源:互联网 发布:深圳易佰网络怎么样 编辑:程序博客网 时间:2024/05/21 18:43
不多说,直接看图和代码
要遍历的二叉树
package com.lyj.test;public class Test { /** * @param args */ public static void main(String[] args) { // 初始化节点 BTNode n1 = new BTNode(15); BTNode n2 = new BTNode(4); BTNode n3 = new BTNode(20); BTNode n4 = new BTNode(1); BTNode n5 = new BTNode(16); BTNode n6 = new BTNode(25); // n1左右子树 n1.setLeftChild(n2); n1.setRightChild(n3); // n2左子树 n2.setLeftChild(n4); // n3左右子树 n3.setLeftChild(n5); n3.setRightChild(n6); /* * 各种遍历 */ Traversal traversal = new Traversal(); // traversal.preorder(n1); // traversal.inorder(n1); traversal.postorder(n1); }}/* * 结点 */class BTNode { private int value; private BTNode leftChild, rightChild; public BTNode() { value = 0; leftChild = null; rightChild = null; } public BTNode(int value, BTNode leftChild, BTNode rightChild) { this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } public BTNode(int value) { this(value, null, null); } public BTNode getLeftChild() { return leftChild; } public void setLeftChild(BTNode leftChild) { this.leftChild = leftChild; } public BTNode getRightChild() { return rightChild; } public void setRightChild(BTNode rightChild) { this.rightChild = rightChild; } public int getValue() { return value; }}/* * 遍历 */class Traversal { // 前序遍历 public void preorder(BTNode rootNode) { if (rootNode != null) { System.out.print(rootNode.getValue() + " "); preorder(rootNode.getLeftChild()); preorder(rootNode.getRightChild()); } } // 中序遍历 public void inorder(BTNode rootNode) { if (rootNode != null) { inorder(rootNode.getLeftChild()); System.out.print(rootNode.getValue() + " "); inorder(rootNode.getRightChild()); } } // 后序遍历 public void postorder(BTNode rootNode) { if (rootNode != null) { postorder(rootNode.getLeftChild()); postorder(rootNode.getRightChild()); System.out.print(rootNode.getValue() + " "); } }}
- 树——二叉树的遍历之递归实现
- 树——二叉树遍历之非递归实现
- 遍历二叉树——递归实现
- 二叉树(2)——遍历的非递归实现
- 二叉树的非递归遍历——java实现
- 二叉树的基本实现和遍历——递归
- 二叉树遍历之非递归实现
- 非递归实现二叉树的遍历
- 二叉树遍历的递归实现图解
- 二叉树的遍历递归实现
- 非递归实现二叉树的遍历
- 二叉树遍历的非递归实现
- 二叉树遍历的非递归实现
- 二叉树遍历的非递归实现
- 二叉树的创建遍历 递归实现
- 二叉树的非递归遍历实现
- 二叉树遍历的非递归实现
- 非递归实现二叉树的遍历
- NSIS ODBC Driver 安装及新建(例MySQL ODBC Driver 3.5.1)
- Intent打电话
- 《我的求职经历》——摘自传智播客学员的日志
- Visio中激活一个或多个图层操作
- 1 如何制定详尽的SEO计划
- 树——二叉树的遍历之递归实现
- oracle wait event virtual circuit status
- 软件测试职业道德和工作责任
- 判断一个字符串是不是纯数字字符串
- poj 1815(最大流来求割点)
- Intent的uri
- 伟大的HfutEngine
- 使用HTML5实现多文件上传
- 第二章 7 HTML5 表单属性