Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作
什么也不说了,直接上代码。
首先是节点类,大家都懂得
/**
* 二叉树的节点类
*
* @author HeYufan
*
* @param <T>
*/
class Node<T extends Comparable<? super T>>
{
/**
* 节点储存的值
*/
private T data;
/**
* 左子节点
*/
private Node<T> leftNode;
/**
* 右子节点
*/
private Node<T> rightNode; public Node()
{
this(null);
} public Node(T data)
{
this.data = data;
this.leftNode = null;
this.rightNode = null;
} /**
* @return data
*/
public T getData()
{
return data;
} /**
* @param data
* 要设置的 data
*/
public void setData(T data)
{
this.data = data;
} /**
* @return leftNode
*/
public Node<T> getLeftNode()
{
return leftNode;
} /**
* @param leftNode
* 要设置的 leftNode
*/
public void setLeftNode(Node<T> leftNode)
{
this.leftNode = leftNode;
} /**
* @return rightNode
*/
public Node<T> getRightNode()
{
return rightNode;
} /**
* @param rightNode
* 要设置的 rightNode
*/
public void setRightNode(Node<T> rightNode)
{
this.rightNode = rightNode;
} }
然后是二叉搜索树的实现
/**
*
*/
package dataStructure.tree.binarytree; import java.util.LinkedList;
import java.util.Queue; /**
* 二叉排序树
*
* @author HeYufan
*
*/
public class BinaryTree<T extends Comparable<? super T>>
{
// 二叉树的根节点
private Node<T> root;
// 二叉树的节点总数
private int size; public BinaryTree()
{
this.root = null;
this.size = 0;
} // 根据一个节点生成二叉树(该节点作为根节点)
public BinaryTree(Node<T> root)
{
this.root = root;
this.size = 1;
} /**
* 插入
*
* @param value
* 要插入二叉树的值
*/
public void insert(T value)
{
root = insert(this.root, value);
} /**
* 二叉树的节点总数
*
* @return
*/
public int size()
{
return this.size;
} /**
* 二叉树是否为空树
*
* @return
*/
public boolean isEmpty()
{
return this.size == 0;
} /**
* 检测二叉树的节点是否包含value
*
* @param value
* @return
*/
public boolean isContain(T value)
{
return isContain(root, value);
} /**
* 递归比较node节点的值与value
*
* @param node
* @param value
* @return
*/
private boolean isContain(Node<T> node, T value)
{
// 如果指定节点为空,说明当前二叉树不包含value,返回false
if(node == null)
{
return false;
} // node节点的值与value比较的结果
int result = node.getData().compareTo(value);
// 如果result等于0,说明node节点的值与value值相同,说明二叉搜索树包含value,返回true
if(result == 0)
{
return true;
}
// 如果result小于0,说明node节点的值小于value,继续递归比较node的右子节点与value
else if(result < 0)
{
return isContain(node.getRightNode(), value);
}
// 如果result大于0,说明node节点的值大于value,继续递归比较node的左子节点与value
else
{
return isContain(node.getLeftNode(), value);
}
} /**
* 获取二叉树中的最小值
*
* @return
*/
public T getMin()
{
// 如果二叉树为空,则返回null
if(isEmpty())
{
return null;
}
return getMin(root).getData();
} /**
* 根据二叉搜索树的性质,我们可以知道一颗二叉搜索树的最小值一定是这棵树最左边的节点
*
* @param node
* 要搜索的根节点
* @return
*/
private Node<T> getMin(Node<T> node)
{
Node<T> leftNode = node.getLeftNode();
// 如果当前节点的左节点为空,说明这个节点的值就是这棵树最小的值
if(leftNode == null)
{
return node;
}
else
{
// 否则,递归遍历当前节点的左子树
return getMin(leftNode);
}
} /**
* 获取二叉树中的最大值(不采用递归方式,递归方式类似getMin)
*
* @return
*/
public T getMax()
{
// 如果当前二叉树为空,返回null
if(isEmpty())
{
return null;
}
Node<T> rightNode = this.root;
// 判断当前节点(从root节点开始)是否有右子节点
// 如果没有,说明当前节点的值就是该二叉搜索树的做大值,当前循环结束
// 否则,让rightNode指向当前节点的右子节点,继续while循环
while (rightNode.getRightNode() != null)
{
rightNode = rightNode.getRightNode();
}
return rightNode.getData();
} /**
* 将值value插入到以node为根节点的二叉树下
*
* @param node
* 目标子树的根节点
* @param value
* 要插入的值
* @return 这里返回的node其实就是更新后的根节点
*/
private Node<T> insert(Node<T> node, T value)
{
// 如果要插入的目标根节点为null,则这个节点就是要插入值的地方
if(node == null)
{
node = new Node<T>(value);
this.size++;
return node;
}
// 0表示value等于node的值;-1表示value小于node的值;1表示value大于node的值
int result = value.compareTo(node.getData());
if(result < 0)
{
// 当value小于node的值,则将value插入到node节点的左子树中,此时value的目标节点就是node.getLeftNode()
node.setLeftNode(insert(node.getLeftNode(), value));
}
else if(result > 0)
{
// 当value大于node的值,则将value插入到node节点的右子树中,此时value的目标节点就是node.getRightNode()
node.setRightNode(insert(node.getRightNode(), value));
}
else
{
// 如果result = 0,说明插入的值已经存在了,可以选择更新值或者什么也不做
}
return node;
} /**
* 先序遍历
*/
public void preorder()
{
preorder(root);
} /**
* 后序遍历
*/
public void postorder()
{
postorder(root);
} /**
* 中序遍历
*/
public void inorder()
{
inorder(root);
} /**
* 层序遍历
*/
public void level()
{
if(root == null)
return;
Queue<Node<T>> queue = new LinkedList<Node<T>>();
queue.add(root);
Node<T> node = null;
while (queue.size() != 0)
{
node = queue.poll();
System.out.println(node.getData());
if(node.getLeftNode() != null)
queue.add(node.getLeftNode());
if(node.getRightNode() != null)
queue.add(node.getRightNode());
}
} /**
* 先序遍历:遍历以node为根节点的子树 先打印节点的值,再遍历节点的左子树,最后遍历节点的右子树
*
* @param node
* 要遍历的子树的根节点
*/
private void preorder(Node<T> node)
{
if(node == null)
{
return;
}
else
{
// 打印节点的值
System.out.println(node.getData());
// 递归遍历该节点的左子树
preorder(node.getLeftNode());
// 递归遍历该节点的右子树
preorder(node.getRightNode());
}
} /**
* 后序遍历:遍历以node为根节点的子树;先遍历节点的左子树,再遍历节点的右子树,最后打印节点的值
*
* @param node
* 要遍历的子树的根节点
*/
private void postorder(Node<T> node)
{
if(node == null)
{
return;
}
else
{
// 递归遍历该节点的左子树
postorder(node.getLeftNode());
// 递归遍历该节点的右子树
postorder(node.getRightNode());
// 打印节点的值
System.out.println(node.getData());
}
} /**
* 中序遍历:遍历以node为根节点的子树;先遍历左子树,再打印节点的值,最后遍历右子树
*
* @param node
*/
private void inorder(Node<T> node)
{
if(node == null)
{
return;
}
else
{
// 递归遍历该节点的左子树
inorder(node.getLeftNode());
// 打印节点的值
System.out.println(node.getData());
// 递归遍历该节点的右子树
inorder(node.getRightNode());
}
}
}
测试:
/**
*
*/
package dataStructure.test; import dataStructure.tree.binarytree.BinaryTree; /**
* @author Administrator
*
*/
public class TestBinaryTree
{
public static void main(String[] args)
{
BinaryTree<Integer> tree = new BinaryTree<Integer>();
tree.insert(5);
tree.insert(3);
tree.insert(2);
tree.insert(7);
tree.insert(8);
tree.insert(4);
tree.insert(1);
tree.insert(4);
tree.insert(1);
System.out.println("是否包含1:" + tree.isContain(1));
System.out.println("是否包含10:" + tree.isContain(10));
System.out.println("树的节点数:" + tree.size());
System.out.println("树的最大值:" + tree.getMax());
System.out.println("树的最小值:" + tree.getMin());
System.out.println("先序遍历:");
tree.preorder();
System.out.println("后序遍历:");
tree.postorder();
System.out.println("中序遍历:");
tree.inorder();
System.out.println("层序遍历:");
tree.level(); }
}
树的结构:

输出结果:
是否包含1:true
是否包含10:false
树的节点数:7
树的最大值:8
树的最小值:1
先序遍历:
5
3
2
1
4
7
8
后序遍历:
1
2
4
3
8
7
5
中序遍历:
1
2
3
4
5
7
8
层序遍历:
5
3
7
2
4
8
1
Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作的更多相关文章
- Java实现二叉搜索树
原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11406176.html 尝试一下用Java实现二叉搜索树/二叉查找树,记录自己的学习历程. 1 ...
- Java实现二叉搜索树的插入、删除
前置知识 二叉树的结构 public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() { } TreeNode( ...
- Java数据结构——二叉搜索树
定义二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若 ...
- 【剑指offer】面试题24:二叉搜索树的兴许前序遍历序列
分析: 前序: 根 左 右 后序: 左 由 根 二叉搜索树: 左 < 根 < 右 那么这就非常明显了. def ifpost(postArray, start, end): #one or ...
- Java创建二叉搜索树,实现搜索,插入,删除操作
Java实现的二叉搜索树,并实现对该树的搜索,插入,删除操作(合并删除,复制删除) 首先我们要有一个编码的思路,大致如下: 1.查找:根据二叉搜索树的数据特点,我们可以根据节点的值得比较来实现查找,查 ...
- L2-004. 这是二叉搜索树吗?(前序转后序递归)
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
- Java实现二叉搜索树及相关操作
package com.tree; import com.tree.BitNode; /** * * 二叉搜索树:一个节点的左子节点的关键字小于这个节点.右子节点的关键字大于或等于这个父节点 * * ...
- 剑指offer-面试题54-二叉搜索树的第k大节点-中序遍历
/* 题目: 求二叉搜索树的第k大节点. */ /* 思路: 中序遍历. */ #include<iostream> #include<cstring> #include< ...
- Java对二叉搜索树进行插入、查找、遍历、最大值和最小值的操作
1.首先,须要一个节点对象的类.这些对象包括数据.数据代表存储的内容,并且还有指向节点的两个子节点的引用 class Node { public int iData; public double dD ...
随机推荐
- Delphi中的异常处理(10种异常来源、处理、精确处理)
一.异常的来源 在Delphi应用程序中,下列的情况都比较有可能产生异常. 1.文件处理 2.内存分配 3.windows资源 4.运行时创建对象和窗体 5.硬件和操作系统冲突 6.网络问题 7.数据 ...
- POJ 2533 Longest Ordered Subsequence - from lanshui_Yang
题目大意:求一个数列的最长上升子序列(严格上升). 解题思路: 方法一:O(n^2) dp[i]:表示处理到第i个位置,序列的最长上升子序列末尾为i的长度: a[]数组存储原序列 dp[i] = ma ...
- Linux系统编程(15)——shell脚本语法
Shell字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似. 单引号 s ...
- 2015第10周四-CSS小结
这两天做前台页面发现个人在CSS前端方法很多基础知识都忘了,晚上又搜索学习了下,把相关内容摘录总结. CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. selector {declara ...
- VS2008生成的程序无法在其它电脑上运行,提示系统无法执行指定的程序
经过一番查找,最给力的参考是 http://www.cnblogs.com/visoeclipse/archive/2010/02/27/1674866.html ------------------ ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- vi常用命令笔记
1.Vi 删除全部内容,删除某行到结尾,删除某段内容 (1)转到文件指定行 nG (2)删除所有内容(先用G转到文件尾) ,使用: $G :1,.d (3)删除第9行到第200行的内容(先用200G转 ...
- 【转】Linux内核调试方法总结
目录[-] 一 调试前的准备 二 内核中的bug 三 内核调试配置选项 1 内核配置 2 调试原子操作 四 引发bug并打印信息 1 BUG()和BUG_ON() 2 dump_sta ...
- Python常用模块 (2) (loging、configparser、json、pickle、subprocess)
logging 简单应用 将日志打印到屏幕 import logging logging.debug('debug message') logging.info('info message') log ...
- wxpython StatuBar 带进度条的状态栏
# -*- coding: utf- -*- import wx class customStatusBar(wx.StatusBar): def __init__(self, parent): wx ...