一.二叉树的遍历算法(前序遍历,中序遍历,后序遍历,层次遍历)
1,前序遍历
(1)使用面向对象的思想将树的节点封装成对象
public class TNode {
public int data;
public String inf;
public TNode LeftNode;
public TNode RightNode;
public TNode(int value,String inf) {
this.data=value;
this.inf=inf;
}
(2)将节点构造成树的对象(其中也包括了二叉树的插入,寻找,删除(原理是:寻找到中序后继节点跟所要删除的节点进行替换))
public class Tree {
public TNode Root;
public TNode getRoot() {
return Root;
}
public void Insert(int value,String s) {
TNode current=Root;
TNode node = new TNode(value,s);
if(Root==null) {
Root=node;
return ;
}
while(true) {
TNode parent=current;
if(current.data<value) {
current=current.RightNode;
if(current==null) {
parent.RightNode=node;
break;
}
}else {
current=current.LeftNode;
if(current==null) {
parent.LeftNode=node;
break;
}
}
}
}
/*
*删除二叉树节点有三种情况:
*1.该节点是叶子节点,没有叶子节点{只需要改变其父节点的引用值,将其设置为null即可}
*2.该节点有一个子节点{将其直接指向要删除的节点的子节点}
*3.该节点有两个子节点{要使用到他的中序后继来代替该节点}
*
*/
public boolean Delet(int value) {
TNode parent=Root;
TNode current=Root;
boolean isLeftNode=false; //用来判断所要删除的节点对于其父节点parent是左子节点,还是右子节点
while(current.data!=value) {
parent=current;
if(current.data<value) {
current=current.RightNode;
isLeftNode=false;
}else {
current=current.LeftNode;
isLeftNode=true;
}
if(current==null) {
return false;
}
}
if (current.LeftNode == null && current.RightNode == null) {
if (current == Root) {
Root = null;
} else if (isLeftNode) {
parent.LeftNode = null;
} else {
parent.RightNode = null;
}
} else if (current.LeftNode == null) {
if (current == Root) {
Root = Root.RightNode;
} else if (isLeftNode) {
parent.LeftNode = current.RightNode;
} else {
parent.RightNode = current.RightNode;
}
} else if (current.RightNode == null) {
if (current == Root) {
Root = Root.LeftNode;
} else if (isLeftNode) {
parent.LeftNode = current.LeftNode;
} else {
parent.RightNode = current.LeftNode;
}
}else {
TNode Successor=getSuccessor(current);
if(current==Root) {
Root=Successor;
}else if(isLeftNode) {
parent.LeftNode=Successor;
}else {
parent.RightNode=Successor;
}
Successor.LeftNode=current.LeftNode;
}
return true;
}
public TNode getSuccessor(TNode delNode) { //用来寻找中序后继节点
TNode Successor=delNode;
TNode SuccessorParent=delNode;
TNode current=delNode.RightNode;
while(current!=null) {
SuccessorParent=Successor;
Successor=current;
current=current.LeftNode;
}
if(Successor!=delNode.RightNode) {
SuccessorParent.LeftNode=Successor.RightNode;
Successor.RightNode=delNode.RightNode;
}
return Successor;
}
public TNode find(int value) {
TNode current=Root;
if(current.data==value) {
return current;
}
while(current.data!=value) {
if(current.data<value) {
current=current.RightNode;
} else {
current=current.LeftNode;
}
if(current==null) {
return null;
}
}
return current;
}
(3)接下来就是用递归实现的前序遍历
public void FrontTree(TNode NowNode) { //这个是前序遍历:其顺序是中左右
if(NowNode!=null) {
System.out.println(NowNode.inf+"-->"+NowNode.data);
FrontTree(NowNode.LeftNode);
FrontTree(NowNode.RightNode);
}
2,中序遍历
public void InorderTree(TNode NowNode) {//这个是中序遍历:其顺序是左右中
if(NowNode!=null) {
InorderTree(NowNode.LeftNode);
System.out.println(NowNode.inf+"-->"+NowNode.data);
InorderTree(NowNode.RightNode);
}
}
3,后序遍历
public void AfterTree(TNode NowNode) {//这个是后序遍历,其顺序是左右中
if(NowNode!=null) {
AfterTree(NowNode.LeftNode);
AfterTree(NowNode.RightNode);
System.out.println(NowNode.inf+"-->"+NowNode.data);
}
}
4,层次遍历
二,二叉树所实现的排序算法
1,堆排序算法
背景知识:(1)最后一个非叶子节点的下标为:length/2-1
(2)假设根节点的下标为i,其左孩子的下标为2*i+1,其右孩子的下标为2*i
public static void adjustHeap(int[] a, int i, int len) {
int temp, j;
temp = a[i];
for (j = 2 * i; j < len; j *= 2) {// 沿关键字较大的孩子结点向下筛选
if (j < len && a[j] < a[j + 1])
++j; // j为关键字中较大记录的下标
if (temp >= a[j])
break;
a[i] = a[j];
i = j;
}
a[i] = temp;
}
public static void heapSort(int[] a) {
int i;
for (i = a.length / 2 - 1; i >= 0; i--) {// 构建一个大顶堆
adjustHeap(a, i, a.length - 1);
}
for (i = a.length - 1; i >= 0; i--) {// 将堆顶记录和当前未经排序子序列的最后一个记录交换
int temp = a[0];
a[0] = a[i];
a[i] = temp;
adjustHeap(a, 0, i - 1);// 将a中前i-1个记录重新调整为大顶堆
}
}