欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼

 更新時(shí)間:2016年09月29日 14:10:51   投稿:lqh  
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下

1。 二叉樹接口

public interface BinaryTreeInterface<T> {
  public T getRootData();
  public int getHeight();
  public int getNumberOfRoot();
  public void clear();
  
  public void setTree(T rootData); // 用rootData設(shè)置樹
  public void setTree(T rootData,BinaryTreeInterface<T> left,BinaryTreeInterface<T> right); //設(shè)置樹,用左右子節(jié)點(diǎn)
  }

2 節(jié)點(diǎn)類

package com.jimmy.impl;

public class BinaryNode<T> {
private T data;
private BinaryNode<T> left;  //左子節(jié)點(diǎn)
private BinaryNode<T> right; //右子節(jié)點(diǎn)
public BinaryNode(){
  this(null);
}
public BinaryNode(T data){
  this(data,null,null);
}
public BinaryNode(T data,BinaryNode<T> left,BinaryNode<T> 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<T> getLeft() {
    return left;
  }
  public void setLeft(BinaryNode<T> left) {
    this.left = left;
  }
  public BinaryNode<T> getRight() {
    return right;
  }
  public void setRight(BinaryNode<T> 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<T> 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.二叉樹實(shí)現(xiàn)

package com.jimmy.impl;

import java.util.Stack;

import com.jimmy.BinaryTreeInterface;

public class Binarytree<T> implements BinaryTreeInterface<T> {

  private BinaryNode<T> root;    //只要一個(gè)數(shù)據(jù)節(jié)點(diǎn)就夠了
// 構(gòu)造空樹
  public Binarytree(){
  root=null;
  }
  

// 用rootData構(gòu)造樹(有個(gè)根)
  public Binarytree(T rootdata){
    root=new BinaryNode<T>(rootdata) ;
    }
  // 用其他樹構(gòu)造樹
  public Binarytree(T rootdata,Binarytree<T> leftTree,Binarytree<T> rightTree){
    root=new BinaryNode<T>(rootdata) ;
    if(leftTree!=null){
      root.setLeft(leftTree.root);
    }
    
    if(rightTree!=null){
      root.setRight(rightTree.root);
    }
    }
// 用rootData設(shè)置樹(有個(gè)根)
  @Override
  public void setTree(T rootData) {
    root=new BinaryNode<T>(rootData) ;
    
  }
// 用其他樹設(shè)置樹
  public void setTree(T rootData, BinaryTreeInterface<T> left,BinaryTreeInterface<T> right) {
    root=new BinaryNode<T>(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<T> getRoot() {
    return root;
  }

  public void setRoot(BinaryNode<T> root) {
    this.root = root;
  }


  public int getNumOfNodes(){
  return root.getNumOfNodes();
  }
  
public void inOrderTraverse(){
    
    inOrderTraverse(root);
  }
  
//用棧方法遍歷
public void inOrderStackTraverse(){
  
  Stack<BinaryNode> stack=new Stack<BinaryNode>();
  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<T> node){
    
    if(node!=null)
      {inOrderTraverse(node.getLeft());
    System.out.println(node.getData());
  
    inOrderTraverse(node.getRight());
      }
  }

public static void main(String[] args) {
Binarytree<String> t=new Binarytree<String>();
Binarytree<String> t8=new Binarytree<String>("8");
Binarytree<String> t7=new Binarytree<String>("7");
t.setTree("6",t7,t8); //用t7,t8設(shè)置樹t

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



通過此文,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Java中HashMap的初始容量設(shè)置方式

    Java中HashMap的初始容量設(shè)置方式

    這篇文章主要介紹了Java中HashMap的初始容量設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 解決springboot mapper注入報(bào)紅問題

    解決springboot mapper注入報(bào)紅問題

    這篇文章主要介紹了解決springboot mapper注入報(bào)紅問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Springboot整合SpringSecurity的完整案例詳解

    Springboot整合SpringSecurity的完整案例詳解

    Spring Security是基于Spring生態(tài)圈的,用于提供安全訪問控制解決方案的框架,Spring Security登錄認(rèn)證主要涉及兩個(gè)重要的接口 UserDetailService和UserDetails接口,本文對(duì)Springboot整合SpringSecurity過程給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2024-01-01
  • java基礎(chǔ)之Object類

    java基礎(chǔ)之Object類

    這篇文章主要介紹了java基礎(chǔ)之Object類 的相關(guān)資料,需要的朋友可以參考下
    2015-06-06
  • 關(guān)于Java中配置ElasticSearch集群環(huán)境賬號(hào)密碼的問題

    關(guān)于Java中配置ElasticSearch集群環(huán)境賬號(hào)密碼的問題

    這篇文章主要介紹了Java中配置ElasticSearch集群環(huán)境賬號(hào)密碼的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • 控制臺(tái)顯示java冒泡排序流程示例

    控制臺(tái)顯示java冒泡排序流程示例

    這篇文章主要介紹了控制臺(tái)顯示java冒泡排序流程示例,需要的朋友可以參考下
    2014-03-03
  • springAOP的三種實(shí)現(xiàn)方式示例代碼

    springAOP的三種實(shí)現(xiàn)方式示例代碼

    這篇文章主要介紹了springAOP的三種實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Spring與Web整合實(shí)例

    Spring與Web整合實(shí)例

    下面小編就為大家?guī)硪黄猄pring與Web整合實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問題

    解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問題

    這篇文章主要介紹了解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 監(jiān)聽器獲取Spring配置文件的方法

    監(jiān)聽器獲取Spring配置文件的方法

    這篇文章主要為大家詳細(xì)介紹了監(jiān)聽器獲取Spring配置文件的方法,很實(shí)用的方法,感興趣的小伙伴們可以參考一下
    2016-06-06

最新評(píng)論