Java Data structure binary tree implementation code _java

Source: Internet
Author: User

1. Two Fork Tree interface

Public interface Binarytreeinterface<t> {public
  T getrootdata ();
  public int getheight ();
  public int getnumberofroot ();
  public void Clear ();
  
  public void Settree (T rootdata); Use Rootdata to set the tree public
  void Settree (T rootdata,binarytreeinterface<t> left,binarytreeinterface<t> right); Set tree, with left and right child nodes
  }

2 Node class

Package Com.jimmy.impl;  public class Binarynode&lt;t&gt; {Private T data, private binarynode&lt;t&gt; left; Zoozi node private binarynode&lt;t&gt; right; Right child node public Binarynode () {this (null);} public Binarynode (T-data) {this (data,null,null);} public Binarynode (t data,
  Binarynode&lt;t&gt; left,binarynode&lt;t&gt; 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&lt;t&gt; GetLeft () {return left;
  public void Setleft (binarynode&lt;t&gt; left) {this.left = left;
  Public binarynode&lt;t&gt; GetRight () {return right;
  public void Setright (binarynode&lt;t&gt; right) {this.right = right;
    
  public Boolean hasleft () {return left!=null;
    
  public Boolean hasright () {return right!=null;
    
  public boolean isleaf () {return (Left==null) &amp;&amp; (right==null); public int GetHeight () {return getheight (this);
    public int getheight (binarynode&lt;t&gt; 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. Two fork tree Implementation

Package Com.jimmy.impl;

Import Java.util.Stack;

Import Com.jimmy.BinaryTreeInterface;    public class Binarytree&lt;t&gt; implements binarytreeinterface&lt;t&gt; {private binarynode&lt;t&gt; root;
  As long as a data node is enough//construct empty Tree public Binarytree () {root=null;
    }////with Rootdata construct tree (with root) public Binarytree (T rootdata) {root=new binarynode&lt;t&gt; (rootdata); ////with other tree constructs the tree public Binarytree (T rootdata,binarytree&lt;t&gt; lefttree,binarytree&lt;t&gt; righttree) {root=new Bi
    Narynode&lt;t&gt; (Rootdata);
    if (lefttree!=null) {root.setleft (lefttree.root);
    } if (Righttree!=null) {root.setright (righttree.root);
    
  The tree (with a root) is set with Rootdata @Override public void Settree (T rootdata) {root=new binarynode&lt;t&gt; (rootdata); ////Set tree public void settree with other tree (T rootdata, binarytreeinterface&lt;t&gt; left,binarytreeinterface&lt;t&gt; right
    ) {root=new binarynode&lt;t&gt; (rootdata);
    Binarytree Lefttree=null; BinarytreeRighttree=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 a 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&lt;t&gt; Getroot () {return root;
  public void Setroot (binarynode&lt;t&gt; root) {this.root = root;
  public int getnumofnodes () {return root.getnumofnodes ();
  public void Inordertraverse () {inordertraverse (root);
  ////stack method to traverse the public void Inorderstacktraverse () {stack&lt;binarynode&gt; stack=new stack&lt;binarynode&gt; (); 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 (); }}///recursively traverse public void Inordertraverse (binarynode&lt;t&gt; node) {if (node!=null) {Inordertraver
    Se (Node.getleft ());
  
    System.out.println (Node.getdata ());
      Inordertraverse (Node.getright ());
} public static void Main (string[] args) {binarytree&lt;string&gt; t=new binarytree&lt;string&gt; ();
binarytree&lt;string&gt; t8=new binarytree&lt;string&gt; ("8");
binarytree&lt;string&gt; t7=new binarytree&lt;string&gt; ("7"); T.settree ("6", t7,t8);
Setting the tree T T.inorderstacktraverse () with t7,t8;
System.out.println (T.getheight ());



 }
}

Through this article, I hope to help everyone, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.