二叉树
- 每个节点上最多有两个子节点
- 满二叉树
- 完全二叉树
- 最后一层所有子节点左边连续
- 倒数第二层叶子节点右边连续
遍历
- 遍历
- 前序
- 先输出当前节点
- 如果左子节点不为空,则递归前序遍历
- 如果右子节点不为空,则递归前序遍历
- 中序
- 如果当前节点左子节点不为空,则递归中序遍历
- 输出当前节点
- 如果当前节点右子节点不为空,则递归中序遍历
- 后序
- 如果当前节点左子节点不为空,则递归中序遍历
- 如果当前节点右子节点不为空,则递归中序遍历
- 输出当前节点
public class Demo{
public static void main(String[] args){
BinaryTree binaryTree = new BinaryTree();
HeroNode node1 = new HeroNode(1."xx");
HeroNode node2 = new HeroNode(2."xx");
HeroNode node3 = new HeroNode(3."xx");
HeroNode node4 = new HeroNode(4."xx");
HeroNode node5 = new HeroNode(5."xx");
root1.setLeft(node2);
root1.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.preOrder();
binaryTree.infixOrder();
binaryTree.postOrder();
}
}
class BinaryTree{
private HeroNode root;
public void setRoot(HeroNode root){
this.root = root;
}
public void preOrder(){
if(this.root != null){
this.root.preOrder();
}else{
System.out.println("二叉树为空");
}
}
public void infixOrder(){
if(this.root != null){
this.root.infixOrder();
}else{
System.out.println("二叉树为空");
}
}
public void postOrder(){
if(this.root != null){
this.root.postOrder();
}else{
System.out.println("二叉树为空");
}
}
}
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no,String name){
super();
this.no = no;
this.name = name;
}
public void preOrder(){
System.out.println(this);
if(this.left!=null){
this.left.preOrder();
}
if(this.right!=null){
this.right.preOrder();
}
}
public void infixOrder(){
if(this.left!=null){
this.left.preOrder();
}
System.out.println(this);
if(this.right!=null){
this.right.preOrder();
}
}
public void postOrder(){
if(this.left!=null){
this.left.preOrder();
}
if(this.right!=null){
this.right.preOrder();
}
System.out.println(this);
}
}
查找
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no,String name){
super();
this.no = no;
this.name = name;
}
public HeroNode preOrderSearch(int no){
if(this.no == no){
return this;
}
HeroNode resNode = null;
if(this.left!=null){
resNode = this.left.preOrderSearch(no);
}
if(resNode!=null){
return resNode;
}
if(this.right!=null){
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
public HeroNode infixOrderSearch(){
HeroNode resNode = null;
if(this.left!=null){
resNode = this.left.infixOrderSearch(no);
}
if(resNode!=null){
return resNode;
}
if(this.no == no){
return this;
}
if(this.right!=null){
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
public HeroNode postOrderSearch(){
HeroNode resNode = null;
if(this.left!=null){
resNode = this.left.postOrderSearch(no);
}
if(resNode!=null){
return resNode;
}
if(this.right!=null){
resNode = this.right.postOrderSearch(no);
}
if(resNode!=null){
return resNode;
}
if(this.no == no){
return this;
}
return resNode;
}
}
删除
- 删除
- 如果删除的节点是叶子节点,删除该节点
- 如果删除的节点非叶子节点,删除该子树
- 判断子节点是否需要删除
- 如果左子节点需要删除,则this.left = null
- 如果右子节点需要删除,则this.right = null
- 如果没有删除,向左递归
- 如果没有删除,向右递归
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode(int no,String name){
super();
this.no = no;
this.name = name;
}
public void delNode(int no){
if(this.left!=null &7 this.left.no == no){
this.left = null;
return;
}
if(this.right!=null&&this.right.no == no){
this.right = null;
return;
}
if(this.left != null){
this.left.delNode(no);
}
if(this.right!= null){
this.right.delNode(no);
}
}
}
public void delNode(int no){
if(root!=null){
if(root.getNo()==no){
root = null;
}else{
root.delNode(no);
}
}else{
System.out.println("空树,不能为空");
}
}
顺序存储二叉树
- 顺序存储二叉树
- 完全二叉树
- 第n个元素的左子节点2n+1
- 第n个元素的右子节点2n+2
- 第n个元素的父节点(n-1)/2
public class Demo{
public sttaic void main(String[] args){
int[] arr = {1,2,3,4,5,6,7};
ArrBinaryTree arrBinary = new ArrBinaryTree(arr);
arrBinary.preOrder(0);
}
}
class ArrBinaryTree{
private int[] arr;
public ArrBinaryTree(int[] arr){
this.arr = arr;
}
public void preOrder(int index){
if(arr==null|| arr.length==0){
System.out.println("数组为空,不能前序遍历");
}
Systm.out.println(arr[index]);
if((index*2+1)<arr.length){
preOrder(2*index + 1);
}
if((index*2+2)<arr.length){
preOrder(2*ubdex+2);
}
}
}
线索二叉树
- n个二叉链表有n+1个空指针域
- 前序线索二叉树
- 中序线索二叉树
- 后序线索二叉树
public class Demo{
public static void main(String[] args){
}
}
class BinaryTree{
private HeroNOde root;
private HeroNode pre = null;
public void threadedNotes(HeroNode node){
if(node==null){
return ;
}
threadedNodes(node.getLeft())
if(node.getLeft()==null){
node.setLeft(pre);
node.setLeftType(1);
}
if(pre!=null&& pre.getRight()==null){
pre.setRight(node);
pre.setRightType(1);
}
pre = node;
threadedNodes(node.getRight())
}
}
calss HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
private int leftType;
private int rigthType;
}
线索化二叉树遍历
// 遍历线索化二叉树
public void threadedList(){
// 定义一个遍历 存储当前遍历的系欸但 从root开始
HeroNode node = root;
while(node!=null){
// 循环找到leftType==1的系欸但
// 后面随着遍历而发生变化
while(node.getLeftType()==0){
node = node.getLeftType();
}
// 打印当前节点
System.out.println(node);
// 如果当前系欸但右指针指向是后继节点,就一直输出
while(node.getRightType()==1){
// 获取当前系欸但的后继节点
node = node.getRight();
System.out.println(node);
}
// 替换这个遍历节点
node = node.getRight();
}
}
堆排序
- 堆排序
- 旋转排序
- 完全二叉树
- 大顶堆
- 每个节点大于等于左右节点的值
- arr[i]>=arr[2i+1]&&arr[i]>=arr[2i+2]
- 小顶堆
- 每个节点但小于等于左右节点的值
- arr[i]<=arr[2i+1]&&arr[i]<=arr[2i+2]
public class HeapSort{
public static void main(String[] args){
int[] arr = {4,6,8,5,9};
}
public static void heapSort(int arr[]){
System.out.println("堆排序");
for(int i=arr.length/2-1;i>=0;i--){
adjustHeap(arr,i,arr.length);
}
for(int j=arr.length-1;j>0;j--){
temp = arr[j];
arr[j] = arr[0];
arr[0] = temp;
adjustHeap(arr,0,j)
}
}
public void static adjustHeap(int arr[],int i,int length){
int temp = arr[i];
for(int k=i*2+1;k<length;k=k*2+1){
if(k+1<length && arr[k]<arr[k+1]){
k++;
}
if(arr[k]>temp){
arr[i] = arr[k];
i = k;
}else{
break;
}
}
arr[i] = temp;
}
}
|