1。
公共接口BinaryTreeInterface <t> {public t getRootdata();公共int getheight();公共int getnumberofroot();公共void clear(); public void settree(t rootdata); //用rootdata設置樹public void settree(t rootdata,binarytreeinterface <t>左,binarytreeinterface <t>右); //設置樹,用左右子節點} 2節點類
軟件包com.jimmy.impl; public class binaryNode <t> {private t data; private binaryNode <t> left; //左子節點私有二進制node <t> right; //右子節點public binareNode(){this(null);} public binaryNode(t data){this(data,null,null,null);} public binaryNode(t數據,binaryNode <t>左,binaryNode,binaryNode <t> right){this.data = data; this.left =左; this.right = right;} public t getData(){返回數據; } public void setData(t data){this.data = data; } public binaryNode <t> getLeft(){返回左; } public void setleft(binaryNode <t>左){this.left = left; } public binaryNode <t> getright(){返回正確; } public void setRight(binaryNode <t> right){this.right = right; } public boolean hasleft(){返回! = null; } public boolean hasright(){返回正確! = null; } public boolean isleaf(){return(left == null)&&(right == null); } public int getheight(){return geteight(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));返回h; } public int int getNumofNodes(){int lnum = 0,rnum = 0;如果(左!= null)lnum = left.getNumofNodes(); if(right!= null)rnum = right.getNumofNodes();返回lnum+rnum+1; }}}3.二叉樹實現
軟件包com.jimmy.impl; import java.util.stack; import com.jimmy.binary.binarytreeinterface; public類BinaryTree <t>實現BinaryTreeInterface <t> {private BinaryNode <t> root; //只要一個數據節點就夠了//構造空樹public binarytree(){root = null; } //用rootdata(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>左,binarytreeinterface <t>右){root = new binarynode <t>(rootdata); binarytree leftTree = null; binarytree righttree = null; if(((leftTree =(binaryTree)左)!= 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自動生成的方法stub return root.getheight(); } @Override public int int getNumberofroot(){// todo自動生成的方法stub return 0; } @Override public t getRootdata(){if(root! = null)return root.getData();否則返回null; } public binaryNode <t> getRoot(){return root; } public void setRoot(binaryNode <t> root){this.root = root; } public int 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 <BinaryTree <String> t8 = new BinaryTree <String>(“ 8”); BinaryTree <String> T7 = t7 = new BinaryTree <new BinaryTree <Strinee <String>(“ 7”); //用t7,t8設置樹tt.inorderstacktraverse(); system.out.println(t.getheight());}}}通過此文,希望能幫助到大家,謝謝大家對本站的支持! ,謝謝大家對本站的支持!