1、首先,须要一个节点对象的类。这些对象包括数据。数据代表存储的内容,并且还有指向节点的两个子节点的引用

class Node {
public int iData;
public double dData;
public Node leftChild;
public Node rightChild;
public void displayNode() {
System.out.print("{");
System.out.print(iData);
System.out.print(",");
System.out.print(dData);
System.out.print("}");
}
}

2、插入一个节点

从根開始查找一个对应的节点,它将是新节点的父节点。

当父节点找到了,新节点就能够连接到它的左子节点或右子节点处。这取决于新节点的值是比父节点的值大还是小。

以下是insert()方法代码:

public void insert(int id, double dd) {
Node newNode = new Node();
newNode.iData = id;
newNode.dData = dd;
if(root == null)
root = newNode;
else {
Node current = root;
Node parent;
while(true) {
parent = current;
if(id < current.iData) {
current = current.leftChild;
if(current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if(current == null) {
parent.rightChild = newNode;
return;
}
}
} // end while
} // end else
}

这里用一个新的变量parent(current的父节点),来存储遇到的最后一个不是null的节点。必须这样做,由于current在查找的过程中会变成null,才干发现它查过的上一个节点没有一个相应的子节点。

假设不存储parent。就会失去插入新节点的位置。

3、查找一个节点

public Node find(int key) {
// 如果树非空
Node current = root;
while(current.iData != key) {
if(key < current.iData)
current = current.leftChild;
else
current = current.rightChild;
if(current == null)
return null;
}
return current;
}

找到节点:假设while循环不满足条件,从循环的末端退出来。current的iData字段和key相等,这就找到了该节点。

找不到节点:假设current等于null。在查找序列中找不到下一个子节点,到达序列的末端而没有找到要找的节点。表明了它不存在。返回nulll来指出这个情况。

4、遍历树(前序遍历。中序遍历,后序遍历)

/**
* 前序遍历
* @param localRoot
*/
public void preOrder(Node localRoot) {
if(localRoot != null) {
System.out.print(localRoot.iData+" ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
} /**
* 中序遍历
* @param localRoot
*/
public void inOrder(Node localRoot) {
if(localRoot != null) {
preOrder(localRoot.leftChild);
System.out.print(localRoot.iData+" ");
preOrder(localRoot.rightChild);
}
} /**
* 后序遍历
* @param localRoot
*/
public void postOrder(Node localRoot) {
if(localRoot != null) {
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
System.out.print(localRoot.iData+" ");
}
}

遍历树的最简单方法使用递归的方法。

用递归的方法遍历整棵树要用一个节点作为參数。初始化这个节点是根。比如中序遍历仅仅须要做三件事:

1)、调用自身来遍历节点的左子树

2)、訪问这个节点

3)、调用自身来遍历节点的右子树。

5、查找最大值和最小值

/**
* 求树中的最小值
* @return
*/
public Node minimum() {
Node current;
current = root;
Node last = null;
while(current != null) {
last = current;
current = current.leftChild;
}
return last;
} /**
* 求树中的最大值
* @return
*/
public Node maxmum() {
Node current;
current = root;
Node last = null;
while(current != null) {
last = current;
current = current.rightChild;
}
return last;
}

下面是完整測试代码:

package binTree;

class Node {
public int iData;
public double dData;
public Node leftChild;
public Node rightChild;
public void displayNode() {
System.out.print("{");
System.out.print(iData);
System.out.print(",");
System.out.print(dData);
System.out.print("}");
}
} class Tree {
private Node root;
public Tree() {
root = null;
}
/**
* 查找节点
* @param key
* @return
*/
public Node find(int key) {
// 如果树非空
Node current = root;
while(current.iData != key) {
if(key < current.iData)
current = current.leftChild;
else
current = current.rightChild;
if(current == null)
return null;
}
return current;
}
/**
* 插入节点
* @param id
* @param dd
*/
public void insert(int id, double dd) {
Node newNode = new Node();
newNode.iData = id;
newNode.dData = dd;
if(root == null)
root = newNode;
else {
Node current = root;
Node parent;
while(true) {
parent = current;
if(id < current.iData) {
current = current.leftChild;
if(current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if(current == null) {
parent.rightChild = newNode;
return;
}
}
} // end while
} // end else
}
/**
* 前序遍历
* @param localRoot
*/
public void preOrder(Node localRoot) {
if(localRoot != null) {
System.out.print(localRoot.iData+" ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
} /**
* 中序遍历
* @param localRoot
*/
public void inOrder(Node localRoot) {
if(localRoot != null) {
preOrder(localRoot.leftChild);
System.out.print(localRoot.iData+" ");
preOrder(localRoot.rightChild);
}
} /**
* 后序遍历
* @param localRoot
*/
public void postOrder(Node localRoot) {
if(localRoot != null) {
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
System.out.print(localRoot.iData+" ");
}
} /**
* 求树中的最小值
* @return
*/
public Node minimum() {
Node current;
current = root;
Node last = null;
while(current != null) {
last = current;
current = current.leftChild;
}
return last;
} /**
* 求树中的最大值
* @return
*/
public Node maxmum() {
Node current;
current = root;
Node last = null;
while(current != null) {
last = current;
current = current.rightChild;
}
return last;
}
} public class TreeApp {
public static void main(String[] args) {
Tree theTree = new Tree();
/**
* 50
* / \
* 25 75
* / \ \
* 12 37 87
* / \ \
* 30 43 93
* \ \
* 33 97
*/
theTree.insert(50, 1.5);
theTree.insert(25, 1.2);
theTree.insert(75, 1.7);
theTree.insert(12, 1.5);
theTree.insert(37, 1.2);
theTree.insert(43, 1.7);
theTree.insert(30, 1.5);
theTree.insert(33, 1.2);
theTree.insert(87, 1.7);
theTree.insert(93, 1.5);
theTree.insert(97, 1.5);
System.out.println("插入完成~"); //找到root节点
Node nodeRoot = theTree.find(50);
// 中序遍历
theTree.inOrder(nodeRoot);
System.out.println();
// 求最小值
System.out.println("mini:"+ theTree.minimum().iData);
// 求最大值
System.out.println("max:"+ theTree.maxmum().iData); }
}

Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作的更多相关文章

  1. Java实现二叉搜索树的插入、删除

    前置知识 二叉树的结构 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() { } TreeNode( ...

  2. Java实现二叉搜索树

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11406176.html 尝试一下用Java实现二叉搜索树/二叉查找树,记录自己的学习历程. 1 ...

  3. 【Java】 剑指offer(33) 二叉搜索树的后序遍历序列

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如 ...

  4. 剑指Offer:面试题24——二叉搜索树的后序遍历序列(java实现)

    问题描述: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 思路: 1.首先后序遍历的结果是[(左子 ...

  5. 剑指Offer-23.二叉搜索树的后序遍历序列(C++/Java)

    题目: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 分析: 二叉树的后序遍历也就是先访问左子树,再访问右 ...

  6. 剑指offer23:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。输出Yes OR No。

    1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 2 思路和方法 二叉搜索树:二叉查找树(Bin ...

  7. P140、面试题24:二叉搜索树的后序遍历序列

    题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 测试用例: 1)功能测试(输入的后序遍历的序列 ...

  8. [PHP]算法- 判断是否为二叉搜索树的后序遍历序列的PHP实现

    二叉搜索树的后序遍历序列: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 思路: 1.后序遍历是 左右中 ...

  9. 二叉搜索树的后序遍历序列(python)

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. # -*- coding:utf-8 -*- cl ...

随机推荐

  1. selenium3 + python - action_chains源码分析

    ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互.这对于做更复杂的动作非常有用,比如悬停和 ...

  2. 一款超好用的第三方评论插件--Gitalk

    一,使用Gitalk的背景: 1.最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我 ...

  3. promise 小抄

    catch的用法 我们知道Promise对象除了then方法,还有一个catch方法,它是做什么用的呢?其实它和then的第二个参数一样,用来指定reject的回调,用法是这样: getNumber( ...

  4. 【洛谷2469/BZOJ1927】[SDOI2010]星际竞速(费用流/最小路径覆盖)

    题目: 洛谷2469 分析: 把题目翻译成人话:给一个带边权的DAG,求一个路径覆盖方案使路径边权总和最小.从点\(i\)开始的路径需要额外加上\(A_i\)的权值. 回xian忆chang一xue下 ...

  5. 最新省市区划分码code

    爬取国家统计局省市区code 提供php爬取脚本以及json和sql https://github.com/zzDylan/area-code 觉得好用给个star,3q

  6. java中String类为什么要设计成final?

    1 将方法或类声明为final主要目的是:确保它们不会在子类中改变语义.String类是final类,这意味着不允许任何人定义String的子类. String基本约定中最重要的一条是immutabl ...

  7. windows ping 某个网段,不能运行指定的软件

    windows ping 某个网段,不能运行指定的软件 :begin @echo OFF color 0a Title Net Test Tool by:HRuinger Mode con cols= ...

  8. HDU_1085_Holding Bin-Laden Captive!_母函数

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. sql日期提取

    --插入数据修改不行:必须提供学号 insert into Student(生日类型) values('阳历') --把月份提取出来 显示两位数 select DATENAME(month,getda ...

  10. VBA中Option的四种用法

    1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...