class Node {
private int data;
// 其他数据
private int otherData;
private Node left;
private Node right; public Node(int data, int otherData) {
this.data = data;
this.otherData = otherData;
} public int getData() {
return data;
} public void setData(int data) {
this.data = data;
} public int getOtherData() {
return otherData;
} public void setOtherData(int otherData) {
this.otherData = otherData;
} public Node getLeft() {
return left;
} public void setLeft(Node left) {
this.left = left;
} public Node getRight() {
return right;
} public void setRight(Node right) {
this.right = right;
} // 显示方法
public void display() {
System.out.println(data + "," + otherData);
} } class Tree {
private Node root; /**
* 插入节点
*
* @param keyData
* @param otherData
*/
public void insert(int keyData, int otherData) {
Node newNode = new Node(keyData, otherData); // 如果说没有节点
if (root == null) {
root = newNode;
} else {
Node current = root;
Node parent;
while (true) {
parent = current;
if (keyData < current.getData()) {
current = current.getLeft();
if (current == null) {
parent.setLeft(newNode);
return;
}
} else {
current = current.getRight();
if (current == null) {
parent.setRight(newNode);
return;
}
}
}
}
} /**
* 查找节点
*
* @param keyData
* @return
*/
public Node find(int keyData) {
Node current = root;
while (current.getData() != keyData) {
if (keyData < current.getData()) {
current = current.getLeft();
} else {
current = current.getRight();
}
if (current == null) {
return null;
}
}
return current;
} /**
* 修改方法
*
* @param keyData
* 根据keyData查找节点
* @param newOtherData
* 节点新值
*/
public void change(int keyData, int newOtherData) {
Node findNode = find(keyData);
findNode.setOtherData(newOtherData);
} // 先序遍历
public void preOrder(Node node) {
if (node != null) {
node.display();
preOrder(node.getLeft());
preOrder(node.getRight());
}
} // 中序遍历
public void inOrder(Node node) {
if (node != null) {
inOrder(node.getLeft());
node.display();
inOrder(node.getRight());
}
} // 后序遍历
public void endOrder(Node node) {
if (node != null) {
endOrder(node.getLeft());
endOrder(node.getRight());
node.display();
}
} public Node getRoot() {
return root;
} } /**
* 测试
* @author sun
*
*/
public class MyTree {
public static void main(String[] args) {
Tree tree = new Tree();
tree.insert(10, 20);
tree.insert(30, 49);
tree.insert(15, 42);
tree.insert(25, 30);
tree.insert(40, 45);
tree.insert(90, 90);
tree.insert(150, 150);
tree.insert(130, 130);
tree.insert(80, 82);
tree.preOrder(tree.getRoot());
}
}

  

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

  1. 二叉树JAVA实现

    为了克服对树结构编程的畏惧感和神秘感,下定决心将二叉树的大部分操作实现一遍,并希望能够掌握二叉树编程的一些常用技术和技巧.关于编程实现中的心得和总结,敬请期待!~ [1]  数据结构和表示: 二叉树的 ...

  2. 剑指Offer:面试题23——从上往下打印二叉树(java实现)

    问题描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 按照层次遍历的方法,使用队列辅助. 1.将根结点加入队列. 2.循环出队,打印当前元素,若该结点有左子树,则将其加入队列,若 ...

  3. 剑指offer【04】- 重建二叉树(java)

    题目:重建二叉树 考点:树 题目描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6, ...

  4. 二叉树 Java 实现 前序遍历 中序遍历 后序遍历 层级遍历 获取叶节点 宽度 ,高度,队列实现二叉树遍历 求二叉树的最大距离

    数据结构中一直对二叉树不是很了解,今天趁着这个时间整理一下 许多实际问题抽象出来的数据结构往往是二叉树的形式,即使是一般的树也能简单地转换为二叉树,而且二叉树的存储结构及其算法都较为简单,因此二叉树显 ...

  5. 数据结构之二叉树java实现

    二叉树是一种非线性数据结构,属于树结构,最大的特点就是度为2,也就是每个节点只有一个左子树和一个右子树.二叉树的操作主要为创建,先序遍历,中序遍历,后序遍历.还有层次遍历.遍历有两种方式,一是采用递归 ...

  6. 算法笔记_189:历届试题 横向打印二叉树(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 二叉树可以用于排序.其原理很简单:对于一个排序二叉树添加新节点时,先与根节点比较,若小则交给左子树继续处理,否则交给右子树. 当遇到空子树 ...

  7. 非递归遍历二叉树Java版的实现代码(没写层次遍历)

    直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...

  8. 22.从上往下打印二叉树 Java

    题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 解题思路 就是二叉树的层序遍历.借助一个队列就可以实现.使用两个队列一个存放节点,一个存放值.先将根节点加入到队列中,然后遍历队列中的 ...

  9. 前序遍历构造已知二叉树(Java)

    public BiNode createBiTree() { Scanner input = new Scanner(System.in); int k = input.nextInt(); if(k ...

  10. 非递归遍历二叉树Java实现

    2018-10-03 20:16:53 非递归遍历二叉树是使用堆栈来进行保存,个人推荐使用双while结构,完全按照遍历顺序来进行堆栈的操作,当然在前序和后序的遍历过程中还有其他的压栈流程. 一.Bi ...

随机推荐

  1. javascript基础-事件1

    原理 事件分两种.第一种浏览器事件,由浏览器抛出事件,它是人机交互的基础:第二种自定义事件,由程序员抛出事件,它是模拟事件流程.两者都是为了完成数据的传递. 浏览器事件 机制 冒泡和捕获两种机制.因I ...

  2. HashMap集合

    HashMap集合特点(用法与特点类似于HashSet集合): 1.无序,不允许重复(无序指元素顺序与添加顺序不一致): 2.底层数据结构是哈希表 3.HashMap内部对"键"用 ...

  3. 详解npm的模块安装机制 --社会我npm哥,好用话不多

      依赖树表面的逻辑结构与依赖树真实的物理结构 依赖树表面的逻辑结构与依赖树真实的物理结构并不一定相同! 这里要先提到两个命令:tree -d(linux)和npm ls(npm) 在一个npm项目下 ...

  4. JavaScript设计模式_02_策略模式

    在程序设计中,我们常常遇到这种情况,要实现某一个功能我们有很多种算法可以实现.这些算法灵活多样,而且可以随意互相替换.这种解决方案就是所谓的策略模式. /* * pre:策略模式 * 示例:公司计算奖 ...

  5. dedecms列表页调用子栏目列表,织梦首页调用栏目的子栏目标签代码

    dedecms列表页调用子栏目列表,织梦首页调用栏目的子栏目标签代码. dedecms列表页调用子栏目列表标签: {dede:channelartlist type='sun' }<a href ...

  6. [Leetcode] Binary search--275 H-Index

    Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...

  7. JSP手动注入 全

    检测可否注入 http://****.house.sina.com.cn/publics/detail.jsp?id=7674 and 1=1 (正常页面) http://****.house.sin ...

  8. Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)

    例子: 定义泛型Store package javabased; public interface Store<T> { } 两个实现类StringStore,IntegerStore p ...

  9. python每天一个小练习-强壮的密码

    强壮的密码 题目来源 checkio 需求 斯蒂芬和索菲亚对于一切都使用简单的密码,忘记了安全性.请你帮助尼古拉开发一个密码安全检查模块 如果密码的长度大于或等于10个符号,至少有一个数字,一个大写字 ...

  10. vue-cli创建自己的项目

    vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目,GitHub地址是:https://github.com/vuejs/vue-cli 一. ...