java算法:递归二叉树算法
来源:互联网 发布:sql数据库怎么打开 编辑:程序博客网 时间:2024/05/16 11:51
java算法:递归二叉树算法
二叉树的本质是递归结构,很多可以使用递归分治法完成的,推广了遍历算法。
在只给定指向树的一个指针的前提下,经常需要找到树的各种结构参数的值。
例1:树参数的计算,树的结点树和高度
- 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);
- }
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:快速的输出树方法
- 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);
- }
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:竞标赛树的构建(分支递归策略)
- 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;
- }
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:解析树的构建
- static Node parse(){
- char t = a[i++];
- Node x = new Node(t);
- if((t == '+') || (t == '*')){
- x.l = parse();
- x.r = parse();
- }
- return x;
- }
static Node parse(){char t = a[i++];Node x = new Node(t);if((t == '+') || (t == '*')){x.l = parse();x.r = parse();}return x;}
编译程序如编译器经常使用这样的内部树来表示程序,树可以用于很多目的。
- java算法:递归二叉树算法
- 二叉树-递归算法
- Java二叉树,递归与非递归遍历算法
- 二叉树的递归算法
- 二叉树非递归算法
- 二叉树的递归算法
- 二叉树遍历递归算法
- 算法:二叉树递归遍历
- JAVA语言实现二叉树的层次遍历的非递归算法及递归算法。
- JAVA语言实现二叉树的层次遍历的非递归算法及递归算法
- 【LintCode-66】二叉树的前序遍历(Java实现-递归算法/非递归算法)
- 【LintCode-67】二叉树的中序遍历(Java实现-递归算法/非递归算法)
- 二叉树的一些非递归算法(JAVA)
- java先序中序建立二叉树的递归算法
- java实现二叉树的三种遍历算法(递归)
- java版本_二叉树_非递归算法实现
- 利用java实现二叉树以及非递归遍历算法
- java实现二叉树的三种遍历算法(递归)
- MFC内存中的图片怎么保存?
- js打印jsp页面的表单
- java算法:树遍历
- 卡路里和运动关系!
- New Blogger
- java算法:递归二叉树算法
- 匈牙利命名法
- 十五. Sql server count函数
- 无配置文件使用log4j
- java输入语句
- java算法:图遍历(深度优先和广度优先)
- 用代码来控制自定义的控件 自动适应横竖屏时的相对位置
- 《TCP/IP详解-协议》(21)TCP的超时与重传
- linux数据结构