java实现二叉树的添加和中序,前序排列;求二叉树的高度
2014-03-22 11:08
633 查看
package com.kane.test;
class BiTree{
private int data;//存放的数据
private BiTree left;//左子树
private BiTree right;//右子树
public BiTree(int x) {
data=x;
}
public void add(BiTree x) {
if (x.data<this.data) {//值小的给左子树
if (left==null) {
left=x;
}
else {
left.add(x);//如果左边为空就添加,不为空就递归添加
}
}
else {
if (right==null) {
right=x;
}
else {
right.add(x);
}
}
}
/**
* 用递归解决中序遍历
*/
public void midTravel() {
if (left!=null) {//要判断,不让有BUG
left.midTravel();
}
System.out.println(data);
if (right!=null) {
right.midTravel();
}
}
/**
* 用递归解决先序遍历
*/
public void preTravel() {
System.out.println(data);
if (left!=null) {//要判断,不让有BUG
left.preTravel();
}
if (right!=null) {
right.preTravel();
}
}
}
public class TestBinaryTre {
public static void main(String[] args) {
BiTree root=new BiTree(12);
root.add(new BiTree(9));
root.add(new BiTree(5));
root.add(new BiTree(8));
root.add(new BiTree(15));
root.add(new BiTree(20));
root.preTravel();
root.midTravel();
}
}
求二叉树的高度
package com.zhangle.arithmetic;
class BiTree {
private int data;// 存放的数据
private BiTree left;// 左子树
private BiTree right;// 右子树
public BiTree(int x) {
data = x;
}
public void add(BiTree x) {
if (x.data < this.data) {// 值小的给左子树
if (left == null) {
left = x;
} else {
left.add(x);// 如果左边为空就添加,不为空就递归添加
}
} else {
if (right == null) {
right = x;
} else {
right.add(x);
}
}
}
/**
* 二叉树的高度就是它的左子树和右子树中高度最大值 + 1
* @param x
* @return
*/
public int height(BiTree x) {
int a=0,b=0;
if (x.left!=null) {
a=height(x.left)+1;
}
if (x.right!=null) {
b=height(x.right)+1;
}
return a>b?a:b;
}
}
public class HeightBiTree {
public static void main(String[] args) {
BiTree root = new BiTree(12);
root.add(new BiTree(9));
root.add(new BiTree(5));
root.add(new BiTree(8));
root.add(new BiTree(15));
root.add(new BiTree(20));
System.out.println(root.height(root)+1);
}
}
class BiTree{
private int data;//存放的数据
private BiTree left;//左子树
private BiTree right;//右子树
public BiTree(int x) {
data=x;
}
public void add(BiTree x) {
if (x.data<this.data) {//值小的给左子树
if (left==null) {
left=x;
}
else {
left.add(x);//如果左边为空就添加,不为空就递归添加
}
}
else {
if (right==null) {
right=x;
}
else {
right.add(x);
}
}
}
/**
* 用递归解决中序遍历
*/
public void midTravel() {
if (left!=null) {//要判断,不让有BUG
left.midTravel();
}
System.out.println(data);
if (right!=null) {
right.midTravel();
}
}
/**
* 用递归解决先序遍历
*/
public void preTravel() {
System.out.println(data);
if (left!=null) {//要判断,不让有BUG
left.preTravel();
}
if (right!=null) {
right.preTravel();
}
}
}
public class TestBinaryTre {
public static void main(String[] args) {
BiTree root=new BiTree(12);
root.add(new BiTree(9));
root.add(new BiTree(5));
root.add(new BiTree(8));
root.add(new BiTree(15));
root.add(new BiTree(20));
root.preTravel();
root.midTravel();
}
}
求二叉树的高度
package com.zhangle.arithmetic;
class BiTree {
private int data;// 存放的数据
private BiTree left;// 左子树
private BiTree right;// 右子树
public BiTree(int x) {
data = x;
}
public void add(BiTree x) {
if (x.data < this.data) {// 值小的给左子树
if (left == null) {
left = x;
} else {
left.add(x);// 如果左边为空就添加,不为空就递归添加
}
} else {
if (right == null) {
right = x;
} else {
right.add(x);
}
}
}
/**
* 二叉树的高度就是它的左子树和右子树中高度最大值 + 1
* @param x
* @return
*/
public int height(BiTree x) {
int a=0,b=0;
if (x.left!=null) {
a=height(x.left)+1;
}
if (x.right!=null) {
b=height(x.right)+1;
}
return a>b?a:b;
}
}
public class HeightBiTree {
public static void main(String[] args) {
BiTree root = new BiTree(12);
root.add(new BiTree(9));
root.add(new BiTree(5));
root.add(new BiTree(8));
root.add(new BiTree(15));
root.add(new BiTree(20));
System.out.println(root.height(root)+1);
}
}
相关文章推荐
- Java二叉树(一)--定义及前序、中序、后序、层次遍历及求高度的实现
- Java实现二叉树,以及先序、中序、后序遍历算法的实现
- 二叉树的前序,中序,后序遍历Java实现
- 通过前序遍历和中序遍历重建二叉树以及输出后序遍历(Java实现)(二)
- 二叉树学习——前序、中序、后序遍历(Java实现)
- Java实现二叉树的前序、中序、后序遍历(递归方法)
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归)
- Java实现二叉树的前序、中序、后序遍历(非递归方法)
- 二叉树的前序,中序,后序和层序遍历java实现
- 二叉树的先序、中序、后序和中序遍历——Java实现
- 通过前序遍历和中序遍历重建二叉树以及输出后序遍历(Java实现)
- java实现二叉树的先序、中序、后序遍历
- Java实现二叉树的先序、中序、后序、层级遍历
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- java 实现二叉树的构建,先序,中序,后序,层次,递归,非递归的遍历
- Java 二叉树的前序、中序、后续遍历 递归和迭代实现
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- Java实现-二叉树先序,中序,后序遍历及递归,非递归遍历