二叉树的相关算法学习笔记--java实现

一.二叉树的遍历算法(前序遍历,中序遍历,后序遍历,层次遍历)


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个记录重新调整为大顶堆
	        }
	    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶人超有料

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值