前序遍历——根 左 右

中序遍历——左 根 右

后序遍历——左 右 根

//=================================================
// 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数据结构——二叉树的更多相关文章

  1. (2)Java数据结构--二叉树 -和排序算法实现

    === 注释:此人博客对很多个数据结构类都有讲解-并加以实例 Java API —— ArrayList类 & Vector类 & LinkList类Java API —— BigDe ...

  2. Java数据结构——二叉树 增加、删除、查询

    //二叉树系统 public class BinarySystem { public static void main(String[] args) { BinaryDomain root = nul ...

  3. java数据结构——二叉树(BinaryTree)

    前面我们已经学习了一些线性结构的数据结构和算法,接下来我们开始学习非线性结构的内容. 二叉树 前面显示增.删.查.遍历方法,完整代码在最后面. /** * 为什么我们要学习树结构. * 1.有序数组插 ...

  4. Java数据结构——二叉树的遍历(汇总)

    二叉树的遍历分为深度优先遍历(DFS)和广度优先遍历(BFS) DFS遍历主要有: 前序遍历 中序遍历 后序遍历 一.递归实现DFSNode.java: public class Node { pri ...

  5. java数据结构(二叉树)

    Node节点: public class Node { public long data; public String sData; public Node leftChild; public Nod ...

  6. Java数据结构——二叉树节点的增删改查、获取深度及最大最小值

    一.查找最大值 // 查找最大值 public static Node maxNode() { Node node = root; Node maxNode = node; while (node ! ...

  7. Java数据结构和算法(四)赫夫曼树

    Java数据结构和算法(四)赫夫曼树 数据结构与算法目录(https://www.cnblogs.com/binarylei/p/10115867.html) 赫夫曼树又称为最优二叉树,赫夫曼树的一个 ...

  8. Java数据结构之树和二叉树(2)

    从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...

  9. Java数据结构之树和二叉树

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

随机推荐

  1. webservice的常用注解

    定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...

  2. [转]CSS Display(显示) 与 Visibility(可见性)

    CSS Display(显示) 与 Visibility(可见性) display属性设置一个元素应如何显示,visibility属性指定一个元素应可见还是隐藏. 隐藏元素 - display:non ...

  3. maven2打包不同jdk版本的包

    通常在一些特别情况下,我们需要为单独某一个构件打包多个不同jdk版本的包,用来支持不同的jdk,基于maven我们就可以很方便的做到这点. 1.在项目的pom文件中加入如下配置 <project ...

  4. 【LintCode】链表求和

    问题分析: 我们通过遍历两个链表拿到每个位的值,两个值加上前一位进位值(0或者1)模10就是该位的值,除以10就是向高位的进位值(0或者1). 由于两个链表可以不一样长,所以要及时判断,一旦为null ...

  5. js checkbox获取选中的值

    //将选择标签 var addCode = function () { var codes = []; var elements = document.getElementsByName(" ...

  6. 64.GitHub 排名前100的android项目简介

    GitHub Android Libraries Top 100 简介 排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不 ...

  7. BZOJ 1123: [POI2008]BLO

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1030  Solved: 440[Submit][Status] ...

  8. linux中/和/root(~) 和 /home

    winodws是森林型目录结构,它有很多根,如C.D.E.F等都是它的根目录,然后在其实创建子目录linux是树型目录结构,它只有一个根就是/目录,然后在/目录在有子目录如/root./home./e ...

  9. 【poj1738】 An old Stone Game

    http://poj.org/problem?id=1738 (题目链接) 题意 一排n堆石子,合并两堆石子的代价为两堆石子总数之和.问将所有石子合并为一堆所需要的最小代价. Solution 本来想 ...

  10. IE的layout属性详解

    http://www.cnblogs.com/yuzhongwusan/archive/2012/03/09/2387052.html 很多在谷歌浏览器(chrome).火狐浏览器(Fire Fox) ...