Java数据结构——二叉树



前序遍历——根 左 右
中序遍历——左 根 右
后序遍历——左 右 根
//=================================================
// File Name : BinaryTree
//------------------------------------------------------------------------------
// Author : Common //类名:Stack_BinaryTree
//属性:
//方法:
class Stack_BinaryTree{
private int maxSize; //栈的长度
private Node[] stackArray; //创建栈的数组的引用
private int top; //创建栈顶的引用 public Stack_BinaryTree(int s) { //构造函数
this.maxSize = s;
stackArray = new Node[maxSize]; //创建对象
top = -1; //栈顶等于-1
} public void push(Node j){ //入栈操作
stackArray[++top] = j; //先把top=-1自加成0,再入栈
} public Node pop(){
return stackArray[top--]; //弹出当前栈顶的元素后,再自减
} public Node peek(){
return stackArray[top]; //返回当前栈顶的元素
} public boolean isEmpty(){ //栈顶为-1,即栈为空
return (top == -1);
} public boolean isFull(){ //栈顶为maxSize-1,即栈为满
return (top == maxSize-1);
} } //类名:Node
//属性:
//方法:
class Node{
int iData;
double fData;
Node leftChild;
Node rightChild; public void display(){
System.out.println('{');
System.out.println(iData);
System.out.println(',');
System.out.println(fData);
System.out.println('}');
}
} //类名:Tree
//属性:
//方法:
class Tree{
public Node root; public Tree(){
root = null;
} 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;
} public void insert(int id,double dd){ //插入新的节点
Node newNode = new Node();
newNode.iData = id;
newNode.fData = dd;
if(root == null){
root = newNode;
}else{
Node current = root;
Node parent;
while(true){
parent = current;
if(newNode.iData<current.iData){ //如果插入的节点的数据小于当前节点的数据
current = current.leftChild;
if(current == null){
parent.leftChild = newNode; //把父亲节点的左孩子设为新节点
return;
}
}else{ //如果插入的节点的数据大于当前节点的数据
current = current.rightChild;
if(current == null){
parent.rightChild = newNode; //把父亲节点的右孩子设为新节点
return;
}
}
}
}
} public void displayTree(){ //显示整个二叉树
Stack_BinaryTree globalStack = new Stack_BinaryTree(128); //用来放置每一层的二叉树
globalStack.push(root); //入栈
int nBlanks = 32;
boolean isRowEmpty = false;
System.out.println(".............................................");
while(isRowEmpty==false){
Stack_BinaryTree localStack = new Stack_BinaryTree(128); //用来放置下一层的二叉树
isRowEmpty = true;
for(int j=0;j<nBlanks;j++){
System.out.print(' ');
} while(globalStack.isEmpty()==false){ //当globalStack不为空,就一直出栈
Node temp = (Node)globalStack.pop(); //temp等于globalStack出栈的节点
if(temp!=null){
System.out.print(temp.iData); //当当前的节点不为空的时候,输出节点的值
localStack.push(temp.leftChild); //入栈globalStack的左孩子到下一层
localStack.push(temp.rightChild); //入栈globalStack的右孩子到下一层
if(temp.leftChild != null || temp.rightChild != null){
isRowEmpty = false; //只要有一个子节点不为空,就把isRowEmpty置为false
}
}
else{ //否则输出--,并把下一层置为空
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for(int j=0;j<nBlanks*2-2;j++){
System.out.print(' ');
}
} System.out.println();
nBlanks /= 2; //输出的空格数减半
while(localStack.isEmpty() == false){
globalStack.push(localStack.pop()); //还原本来的层
}
}
System.out.println(".............................................");
} public void preOrder(Node localRoot){ //前序遍历
if(localRoot != null){
System.out.print(localRoot.iData+" ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
} public void inOrder(Node localRoot){ //中序遍历
if(localRoot != null){
inOrder(localRoot.leftChild);
System.out.print(localRoot.iData+" ");
inOrder(localRoot.rightChild);
}
} public void postOrder(Node localRoot){ //后序遍历
if(localRoot != null){
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
System.out.print(localRoot.iData+" ");
}
} public Node minimum(){ //找到最小的节点
Node current;
Node last = null;
current = root;
while(current != null){
last = current;
current = current.leftChild;
}
return last;
} public Node maxmum(){ //找到最大的节点
Node current;
Node last = null;
current = root;
while(current != null){
last = current;
current = current.rightChild;
}
return last;
} public boolean delete(int key){
Node current = root;
Node parent = root;
boolean isLeftChild = true; while(current.iData != key){//查找要删除的节点,并把其置为current,如果没有返回null
parent = current;
if(key<current.iData){
current = current.leftChild;
isLeftChild = true; //当前current节点的parent节点有左孩子节点
}else{
current = current.rightChild;
isLeftChild = false; //当前current节点的parent节点没有左孩子节点
}
if(current == null){
return false;
}
}
//进入以下的时候,说明current已经匹配到要删除的节点
//如果匹配的current节点没有孩子节点
if(current.leftChild == null && current.rightChild == null){
if(current == root){ //如果这个节点就是根节点
root = null;
}else if(isLeftChild){
parent.leftChild = null; //父节点断开左孩子节点
}else{
parent.rightChild = null; //父节点断开右孩子节点
}
}
//如果current没有右孩子,则要把左子树上移
else if(current.rightChild == null){
if(current == root){ //如果是根节点
root = current.leftChild;
}else if(isLeftChild){ //如果current节点是左孩子,则把current的左孩子放到parent的左孩子位置
parent.leftChild = current.leftChild;
}else{ //如果current节点是右孩子,则把current的左孩子放到parent的右孩子位置
parent.rightChild = current.leftChild;
}
}
//如果current没有左孩子,则要把右子树上移
else if(current.leftChild == null){
if(current == root){ //如果是根节点
root = current.rightChild;
}else if(isLeftChild){ //如果current节点是左孩子,则把current的右孩子放到parent的左孩子位置
parent.leftChild = current.rightChild;
}else{ //如果current节点是右孩子,则把current的右孩子放到parent的右孩子位置
parent.rightChild = current.rightChild;
}
}
//如果current有左右孩子
else{
Node successor = getSuccessor(current);
if(current == root){ //如果要删除的节点是根节点
root = successor;
}else if(isLeftChild){ //如果要删除的节点是左孩子
parent.leftChild = successor;
}else{ //如果要删除的节点是右孩子
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;//把最小的值的节点连接到要删除节点的左子树上
}
return true;
} private Node getSuccessor(Node delNode){
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.rightChild;
while(current != null){ //循环,直到返回右子树中最小的值
successorParent = successor;
successor = current;
current = current.leftChild;
}
if(successor != delNode.rightChild){
successorParent.leftChild = successor.rightChild; //把最小的值的右孩子放到该最小值的位置
successor.rightChild = delNode.rightChild; //把最小的值连接到要删除的节点的右子树上
}
return successor;
} } //主类
//Function : BinaryTree
public class BinaryTree { public static void main(String[] args) throws Exception{
// TODO 自动生成的方法存根
int value;
Tree theTree = new Tree();
theTree.insert(50, 1.5);
theTree.insert(25, 1.4);
theTree.insert(75, 1.3);
theTree.insert(12, 1.6);
theTree.insert(37, 1.7);
//theTree.insert(43, 1.2);
theTree.insert(60, 1.1);
theTree.insert(85, 1.1);
theTree.insert(77, 1.1);
theTree.displayTree(); System.out.println("查找节点的值:"+theTree.find(77).iData); theTree.preOrder(theTree.root);
System.out.println();
theTree.inOrder(theTree.root);
System.out.println();
theTree.postOrder(theTree.root);
System.out.println();
System.out.println("最小的节点:"+theTree.minimum().iData);
System.out.println("最大的节点:"+theTree.maxmum().iData); theTree.delete(75);
theTree.displayTree();
} }
Java数据结构——二叉树的更多相关文章
- (2)Java数据结构--二叉树 -和排序算法实现
=== 注释:此人博客对很多个数据结构类都有讲解-并加以实例 Java API —— ArrayList类 & Vector类 & LinkList类Java API —— BigDe ...
- Java数据结构——二叉树 增加、删除、查询
//二叉树系统 public class BinarySystem { public static void main(String[] args) { BinaryDomain root = nul ...
- java数据结构——二叉树(BinaryTree)
前面我们已经学习了一些线性结构的数据结构和算法,接下来我们开始学习非线性结构的内容. 二叉树 前面显示增.删.查.遍历方法,完整代码在最后面. /** * 为什么我们要学习树结构. * 1.有序数组插 ...
- Java数据结构——二叉树的遍历(汇总)
二叉树的遍历分为深度优先遍历(DFS)和广度优先遍历(BFS) DFS遍历主要有: 前序遍历 中序遍历 后序遍历 一.递归实现DFSNode.java: public class Node { pri ...
- java数据结构(二叉树)
Node节点: public class Node { public long data; public String sData; public Node leftChild; public Nod ...
- Java数据结构——二叉树节点的增删改查、获取深度及最大最小值
一.查找最大值 // 查找最大值 public static Node maxNode() { Node node = root; Node maxNode = node; while (node ! ...
- Java数据结构和算法(四)赫夫曼树
Java数据结构和算法(四)赫夫曼树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 赫夫曼树又称为最优二叉树,赫夫曼树的一个 ...
- Java数据结构之树和二叉树(2)
从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...
- Java数据结构之树和二叉树
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
随机推荐
- jax-ws开发总结
服务端开发步骤: 1.定义SEI,即java中的接口 2.定义SEI的实现类,使用@webservice注解标记它是一个webservice服务类 3.发布服务 客户端开发步骤:使用jdk的servi ...
- 简单Matrix 的方法说明记录
查找资料加上自己理解 ,简单说明Android中Matrix怎么用(新手有错误的地方,希望指正,主要自己记录学习用的) Matrix包含一个3 X 3的矩阵,专门用于图像变换匹配. Matrix提供 ...
- 【BZOJ 2555】SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 一个字符串在原串中的出现次数就是这个字符串对应后缀自动机上的状态的\(|Right|\),要求 ...
- Oracle 同时删除多张表
今天想要将Oracle数据库中 有规律命令的表删除掉,好想一次性干掉--不过没成功--所以退而求其次 先查询想要干掉的表,并且拼接成sql 语句 select 'drop table ' ||tabl ...
- java.io.FileNotFoundException: D:\xxx\yyy (拒绝访问。)问题
File file=new File(fileAllName); FileWriter fw=new FileWriter(file); 在Java的 FileWriter 方法时 系统抛出了异常 j ...
- java.net.URL请求远程文件下载
1:浏览器请求下载 public void listStockcodeUplaod(HttpServletRequest req, HttpServletResponse res) throws Ex ...
- JAVA对文件类型的校验
通常,在WEB系统中,上传文件时都需要做文件的类型校验,大致有如下几种方法: 1. 通过后缀名,如exe,jpg,bmp,rar,zip等等. 2. 通过读取文件,获取文件的Content-type来 ...
- CAlayer层的属性
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- oracle数据库开启的时候 是先开监听还是先开主服务,关数据库的时候呢???
启动的时候无所谓先后,关闭的话 1.首先是关闭监听(让远程客户端无法再连进来):2.发出一个系统检查点,让数据文件和控制文件的系统修改号统一:(alter system checkpoint;)3.s ...
- 【BZOJ-2937】建造酿酒厂 前缀和 + 展环为链 + 乱搞
2937: [Poi2000]建造酿酒厂 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 70 Solved: 24[Submit][Status][D ...