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.图形或者网状结构 接下来的 ...
随机推荐
- [转]响应式WEB设计学习(1)—判断屏幕尺寸及百分比的使用
原文地址:http://www.jb51.net/web/70360.html 现在移动设备越来越普及,用户使用智能手机.pad上网页越来越普遍.但是传统的fix型的页面在移动终端上无法很好的显示.因 ...
- iOS开发小技巧--自定义带有占位文字的TextView(两种方式)
自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写setter,保证外界与内部的交互性 一.方案一:通过drawRect:方法将文字画到textView中,监听文字改变用的是通知中心(代理也可 ...
- Apache Shiro和Spring Security的详细对比
参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...
- 利用MVC的自定义过滤器FilterAttribute、IActionFilter、IExceptionFilter实现异常处理等功能
今天在博客园上看了一篇推荐文章,还说得蛮有道理: http://www.cnblogs.com/richieyang/p/4779028.html 项目中确实有各种后台验证过程,最常见的莫过于判空,而 ...
- git使用相关文章的链接
http://search.fishc.com/cse/search?s=14988791857133860392&q=git&partner=discuz http://bbs.fi ...
- 如何才能恢复Excel文档的打开密码
对于一些密码的破解,最常用的方法就是“暴力破解”,也是获取密码的最后一种方法,Advanced Office Password Recovery的暴力破解能够破解复杂的Office文档密码.wps也有 ...
- dede使用方法----如何转换时间戳
dede用sql调用一个mysql时间,mysql的时间字段是时间戳展示的,突然不知道咋转换了,有点迷茫,结果找了下,发现其实很简单,直接用dede的就行了,如下: 完整时间:[field:datel ...
- Asp.Net Mvc4分页,扩展HtmlHelper类
1.分页方法 using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; ...
- PL/SQL Developer连接本地64位Oracle数据库
1.安装oracle Clinet 首先到Oracle官网上去下载一个Oracle 11g Client(我的是11g的oracle),不过需要先申请一个Oracle 帐号,才能下载. 目前下载地址: ...
- js-JavaScript高级程序设计学习笔记7
第十一章 DOM扩展 1.对DOM的两个主要的扩展是Selectors API(选择符API)和HTML5. 2.jQuery的核心就是通过CSS选择符查询DOM文档取得元素的引用,从而抛开了getE ...