/*
* Java实现二叉树
*/
public class BinaryTree { int treeNode;
BinaryTree leftTree;
BinaryTree rightTree; public BinaryTree(int Data) {
// TODO Auto-generated constructor stub
treeNode=Data;
leftTree=null;
rightTree=null;
} public void insert(BinaryTree node,int data) {
if(data >node.treeNode){
if(node.rightTree==null){
rightTree=new BinaryTree(data);
}else{
this.insert(node.rightTree, data);
}
}else{
if (node.leftTree==null) {
leftTree=new BinaryTree(data);
}else{
this.insert(node.leftTree, data);
}
}
}
}
/*
* 对定义二叉树的,先序遍历,中序遍历,后序遍历
*/
public class BinaryTreeOrder { public static void preOrder(BinaryTree root) { // 先序遍历
if (root != null) {
System.out.print(root.treeNode + "-");
preOrder(root.leftTree);
preOrder(root.rightTree);
}
} public static void inOrder(BinaryTree root) { // 中序遍历 if (root != null) {
inOrder(root.leftTree);
System.out.print(root.treeNode + "--");
inOrder(root.rightTree);
}
} public static void postOrder(BinaryTree root) { // 后序遍历 if (root != null) {
postOrder(root.leftTree);
postOrder(root.rightTree);
System.out.print(root.treeNode + "---");
}
} public static void main(String[] args) {
int[] array = { 12, 76, 35, 22, 16, 48, 90, 46, 9, 40 };
BinaryTree root = new BinaryTree(array[0]); // 创建二叉树
for (int i = 1; i < array.length; i++) {
root.insert(root, array[i]); // 向二叉树中插入数据
}
System.out.println("先序遍历:");
preOrder(root);
System.out.println();
System.out.println("中序遍历:");
inOrder(root);
System.out.println();
System.out.println("后序遍历:");
postOrder(root);
}
}

java二叉树的实现和遍历的更多相关文章

  1. Java实现二叉树的创建和遍历操作(有更新)

    博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...

  2. JAVA递归、非递归遍历二叉树(转)

    原文链接: JAVA递归.非递归遍历二叉树 import java.util.Stack; import java.util.HashMap; public class BinTree { priva ...

  3. java 二叉树的创建 遍历

    本来说复习一下BFS和DFS,辗转就来到了二叉树...本文包括二叉树的创建和遍历 概念 数据:1 2 3 4 5 6 7生成一颗二叉树 上面的数是数据,不是位置,要区别一下数据和位置 红色的代表位置, ...

  4. 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】

    [144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

  5. Java实现 LeetCode 145 二叉树的后序遍历

    145. 二叉树的后序遍历 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成 ...

  6. Java实现 LeetCode 94 二叉树的中序遍历

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...

  7. lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历

    题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...

  8. lintcode:二叉树的中序遍历

    题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 ...

  9. K:二叉树的非递归遍历

    相关介绍:  二叉树的三种遍历方式(先序遍历,中序遍历,后序遍历)的非递归实现,虽然递归方式的实现较为简单且易于理解,但是由于递归方式的实现受其递归调用栈的深度的限制,当递归调用的深度超过限制的时候, ...

随机推荐

  1. JQuery.Ajax()的data参数类型

    假如现在有这样一个表单,是添加元素用的. <form id='addForm' action='UserAdd.action' type='post'> <label for='un ...

  2. javascript中时间的手动创建date的方式

    new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth ...

  3. JSHint配置详解

    Also available on Github JSHint配置详解 增强参数(Enforcing Options) 本类参数设为true,JSHint会产生更多告警. bitwise 禁用位运算符 ...

  4. 【SQL Server】数据库是单个用户的 无法顺利进行操作 怎么解决

    1.打开数据库 2.新建查询 ,输入以下的SQL 语句 DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM( ...

  5. python 定义实例方法

    定义实例方法 一个实例的私有属性就是以__开头的属性,无法被外部访问,那这些属性定义有什么用? 虽然私有属性无法从外部访问,但是,从类的内部是可以访问的.除了可以定义实例的属性外,还可以定义实例的方法 ...

  6. Uva 11988 Broken Keyboard

    因为要经常移动这些字符,所以采用的数据结构是链表. next[i]起到的作用大概就是类似链表里的next指针. 0.需要注意的是,判断cur == last ? 如果 是 则 last=i 1.另外一 ...

  7. Python入门之树莓派

    Linux命令行$+命令 pwd显示当前目录 ls列表 cd改变当前目录,/ sudo超级用户输入,特权来操作系统相关设置或删除文件 sudo apt-get  install  安装程序 sudo ...

  8. js:方法2. 字符串

    String.charAt()/String.charCodeAt() string.charAt(n); n:The index of the character that should be re ...

  9. constructor

    function Person(name){ this.name = name; } Person.prototype = { constructor : Person, sayName : func ...

  10. 17243 Huzi酱和他的俄罗斯套娃(贪心)

    时间限制:500MS  内存限制:65535K 提交次数:15 通过次数:4 收入:12 题型: 编程题   语言: C++;C Description Huzi酱是个非常贪玩的人,除了魔方他还喜欢各 ...