java 資料結構二叉樹的實現代碼_java

來源:互聯網
上載者:User

1。 二叉樹介面

public interface BinaryTreeInterface<T> {  public T getRootData();  public int getHeight();  public int getNumberOfRoot();  public void clear();    public void setTree(T rootData); // 用rootData設定樹  public void setTree(T rootData,BinaryTreeInterface<T> left,BinaryTreeInterface<T> right); //設定樹,用左右子節點  }

2 節點類

package com.jimmy.impl;public class BinaryNode<T> {private T data;private BinaryNode<T> left;  //左子節點private BinaryNode<T> right; //右子節點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.二叉樹實現

package com.jimmy.impl;import java.util.Stack;import com.jimmy.BinaryTreeInterface;public class Binarytree<T> implements BinaryTreeInterface<T> {  private BinaryNode<T> root;    //只要一個資料節點就夠了// 構造空樹  public Binarytree(){  root=null;  }  // 用rootData構造樹(有個根)  public Binarytree(T rootdata){    root=new BinaryNode<T>(rootdata) ;    }  // 用其他樹構造樹  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設定樹(有個根)  @Override  public void setTree(T rootData) {    root=new BinaryNode<T>(rootData) ;      }// 用其他樹設定樹  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設定樹tt.inOrderStackTraverse();System.out.println(t.getHeight());}}

通過此文,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.