java的数据结构二叉树_java 数据结构二叉树的实现代码

1。 二叉树接口

public interface BinaryTreeInterface {

public T getRootData();

public int getHeight();

public int getNumberOfRoot();

public void clear();

public void setTree(T rootData); // 用rootData设置树

public void setTree(T rootData,BinaryTreeInterface left,BinaryTreeInterface right); //设置树,用左右子节点

}

2 节点类

package com.jimmy.impl;

public class BinaryNode {

private T data;

private BinaryNode left; //左子节点

private BinaryNode right; //右子节点

public BinaryNode(){

this(null);

}

public BinaryNode(T data){

this(data,null,null);

}

public BinaryNode(T data,BinaryNode left,BinaryNode right){

this.data=data;

this.left=left;

this.right=right;

}

public T getData()

{

return data;

}

public void setData(T data)

{

this.data= data;

}

public BinaryNode getLeft() {

return left;

}

public void setLeft(BinaryNode left) {

this.left = left;

}

public BinaryNode getRight() {

return right;

}

public void setRight(BinaryNode right) {

this.right = right;

}

public boolean hasLeft()

{return left!=null;

}

public boolean hasRight()

{return right!=null;

}

public boolean isLeaf()

{return (left==null)&&(right==null);

}

public int getHeight()

{

return getHeight(this);

}

public int getHeight(BinaryNode node)

{

int h=0;

if(node!=null)

h=1+Math.max(node.getHeight(node.left),node.getHeight(node.right));

return h;

}

public int getNumOfNodes(){

int lnum=0,rnum=0;

if(left!=null)

lnum=left.getNumOfNodes();

if(right!=null)

rnum=right.getNumOfNodes();

return lnum+rnum+1;

}

}

3.二叉树实现

package com.jimmy.impl;

import java.util.Stack;

import com.jimmy.BinaryTreeInterface;

public class Binarytree implements BinaryTreeInterface {

private BinaryNode root; //只要一个数据节点就够了

// 构造空树

public Binarytree(){

root=null;

}

// 用rootData构造树(有个根)

public Binarytree(T rootdata){

root=new BinaryNode(rootdata) ;

}

// 用其他树构造树

public Binarytree(T rootdata,Binarytree leftTree,Binarytree rightTree){

root=new BinaryNode(rootdata) ;

if(leftTree!=null){

root.setLeft(leftTree.root);

}

if(rightTree!=null){

root.setRight(rightTree.root);

}

}

// 用rootData设置树(有个根)

@Override

public void setTree(T rootData) {

root=new BinaryNode(rootData) ;

}

// 用其他树设置树

public void setTree(T rootData, BinaryTreeInterface left,BinaryTreeInterface right) {

root=new BinaryNode(rootData) ;

Binarytree leftTree=null;

Binarytree rightTree=null;

if((leftTree=(Binarytree)left)!=null){

root.setLeft(leftTree.root);

}

if((rightTree=(Binarytree)right)!=null){

root.setRight(rightTree.root);

}

}

@Override

public void clear() {

root=null;

}

@Override

public int getHeight() {

// TODO Auto-generated method stub

return root.getHeight();

}

@Override

public int getNumberOfRoot() {

// TODO Auto-generated method stub

return 0;

}

@Override

public T getRootData() {

if (root!=null)

return root.getData();

else

return null;

}

public BinaryNode getRoot() {

return root;

}

public void setRoot(BinaryNode root) {

this.root = root;

}

public int getNumOfNodes(){

return root.getNumOfNodes();

}

public void inOrderTraverse(){

inOrderTraverse(root);

}

//用栈方法遍历

public void inOrderStackTraverse(){

Stack stack=new Stack();

BinaryNode cur=root;

//stack.push(root);

while(!stack.isEmpty()||(cur!=null)){

while(cur!=null)

{

stack.push(cur);

cur=cur.getLeft();

}

if(!stack.isEmpty())

{

BinaryNode tmp=stack.pop();

if(tmp!=null)

{System.out.println(tmp.getData());

cur=tmp.getRight();

}

}

}

}

// 递归遍历

public void inOrderTraverse(BinaryNode node){

if(node!=null)

{inOrderTraverse(node.getLeft());

System.out.println(node.getData());

inOrderTraverse(node.getRight());

}

}

public static void main(String[] args) {

Binarytree t=new Binarytree();

Binarytree t8=new Binarytree("8");

Binarytree t7=new Binarytree("7");

t.setTree("6",t7,t8); //用t7,t8设置树t

t.inOrderStackTraverse();

System.out.println(t.getHeight());

}

}

通过此文,希望能帮助到大家,谢谢大家对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值