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<t> {Private T data, private binarynode<t> left; Zoozi node private binarynode<t> right; Right child node 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. Two fork tree Implementation
Package Com.jimmy.impl;
Import Java.util.Stack;
Import Com.jimmy.BinaryTreeInterface; public class Binarytree<t> implements binarytreeinterface<t> {private binarynode<t> 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<t> (rootdata); ////with other tree constructs the tree public Binarytree (T rootdata,binarytree<t> lefttree,binarytree<t> righttree) {root=new Bi
Narynode<t> (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<t> (rootdata); ////Set tree public void settree with other tree (T rootdata, binarytreeinterface<t> left,binarytreeinterface<t> right
) {root=new binarynode<t> (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<t> Getroot () {return root;
public void Setroot (binarynode<t> root) {this.root = root;
public int getnumofnodes () {return root.getnumofnodes ();
public void Inordertraverse () {inordertraverse (root);
////stack method to traverse the 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 (); }}///recursively traverse public void Inordertraverse (binarynode<t> node) {if (node!=null) {Inordertraver
Se (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);
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!