java算法:递归二叉树算法

本文深入探讨了递归二叉树算法的核心概念、计算树参数的方法、快速输出树的技巧以及构建特定树结构的策略。通过实例代码,详细阐述了节点计数、高度计算、树的遍历及构建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java算法:递归二叉树算法

二叉树的本质是递归结构,很多可以使用递归分治法完成的,推广了遍历算法。

在只给定指向树的一个指针的前提下,经常需要找到树的各种结构参数的值。

例1:树参数的计算,树的结点树和高度

Java代码 复制代码
  1. privatestaticintcount(Nodeh){
  2. if(h==null){
  3. reutrn0;
  4. }
  5. returncount(h.l)+count(h.r)+1;
  6. }
  7. intcount(){
  8. returncount(root);
  9. }
  10. privatestaticintheight(Nodeh){
  11. if(h==null){
  12. return-1;
  13. }
  14. intu=height(h.l),v=height(h.r);
  15. if(u>v){
  16. returnu+1;
  17. }else{
  18. returnv+1;
  19. }
  20. }
  21. intheight(){
  22. returnheight(root);
  23. }
private static int count(Node h){
	if(h == null){
		reutrn 0;
	}
	return count(h.l) + count(h.r) + 1;
}
int count(){
	return count(root);
}
private static int height(Node h){
	if(h == null){
		return -1;
	}
	int u = height(h.l), v = height(h.r);
	if(u > v){
		return u + 1;
	}else{
		return v + 1;
	}
}
int height(){
	return height(root);
}

例2:快速的输出树方法

Java代码 复制代码
  1. staticvoidprintNode(Itemx,inth){
  2. for(inti=0;i<h;i++){
  3. System.out.println("");
  4. }
  5. System.out.println("["+x+"]");
  6. }
  7. privatestaticvoidshowR(Nodet,inth){
  8. if(t==null){
  9. printNode(null,h);
  10. return;
  11. }
  12. showR(t.r,h+1);
  13. printNode(t.item,h);
  14. showR(t.l,h+1);
  15. }
  16. voidshow(){
  17. showR(root,0);
  18. }
static void printNode(Item x, int h){
	for(int i = 0; i < h; i++){
		System.out.println("   ");
	}
	System.out.println("[" + x + "]");
}
private static void showR(Node t, int h){
	if(t == null){
		printNode(null, h);
		return;
	}
	showR(t.r, h + 1);
	printNode(t.item, h);
	showR(t.l, h + 1);
}
void show(){
	showR(root, 0);
}

例3:竞标赛树的构建(分支递归策略)

Java代码 复制代码
  1. staticclassNode{
  2. doubleval;
  3. Nodel;
  4. Noder;
  5. Node(doublev,Nodel,Noder){
  6. this.val=v;
  7. this.l=l;
  8. this.r=r;
  9. }
  10. }
  11. staticNodemax(doublea[],intl,intr){
  12. intm=(l+r)/2;
  13. Nodex=newNode(a[m],null,null);
  14. if(l==r){
  15. returnx;
  16. }
  17. x.l=max(a,l,m);
  18. x.r=max(a,m+1,r);
  19. doubleu=x.l.val,v=x.r.val;
  20. if(u>v){
  21. x.val=u;
  22. }else{
  23. x.val=v;
  24. }
  25. returnx;
  26. }
static class Node{
	double val;
	Node l;
	Node r;
	Node(double v, Node l, Node r){
		this.val = v;
		this.l = l;
		this.r = r;
	}
}
static Node max(double a[], int l, int r){
	int m = (l + r)/2;
	Node x = new Node(a[m], null, null);
	if(l == r){
		return x;
	}
	x.l = max(a, l, m);
	x.r = max(a, m + 1, r);
	double u = x.l.val, v = x.r.val;
	if(u > v){
		x.val = u;
	}else{
		x.val = v;
	}
	return x;
}

在某些情况下,构建递归数据结构可能要比通过扫描数据找到最大值好。

使二叉树构建前缀表达式。

前缀表达式

例4:解析树的构建

Java代码 复制代码
  1. staticNodeparse(){
  2. chart=a[i++];
  3. Nodex=newNode(t);
  4. if((t=='+')||(t=='*')){
  5. x.l=parse();
  6. x.r=parse();
  7. }
  8. returnx;
  9. }
static Node parse(){
	char t = a[i++];
	Node x = new Node(t);
	if((t == '+') || (t == '*')){
		x.l = parse();
		x.r = parse();
	}
	return x;
}

编译程序如编译器经常使用这样的内部树来表示程序,树可以用于很多目的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值