1。 二叉树接口
antarmuka publik BinaryTreeInterface <T> {public t getrootData (); publik int getheight (); publik int getNumberofroot (); public void clear (); public void settree (t rootdata); // 用 rootData 设置树 public void settree (t rootData, BinaryTreeInterface <T> Kiri, BinaryTreeInterface <T> kanan); // 设置树 , 用左右子节点} 2 节点类
paket com.jimmy.impl; kelas publik BinaryNode <T> {data private t; private BinaryNode <T> kiri; // 左子节点 BinaryNode pribadi <T> benar; // 右子节点 BinaryNode publik () {this (null);} publik BinaryNode (data t) {this (data, null, null);} publik BinaryNode (data t, BinaryNode <T> Kiri, BinaryNode <T> kanan) {this.data = data; this.left = kiri; this.right = right;} public t getData () {return data; } public void setData (data T) {this.data = data; } publik BinaryNode <T> getLeft () {return left; } public void setleft (BinaryNode <T> kiri) {this.left = kiri; } publik BinaryNode <T> getRight () {return right; } public void setRight (BinaryNode <T> kanan) {this.right = kanan; } public boolean hasleft () {return left! = null; } public boolean hasRight () {return right! = null; } public boolean isleaf () {return (kiri == null) && (kanan == 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)); kembali 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. 二叉树实现
Paket com.jimmy.impl; impor java.util.stack; impor com.jimmy.binarytreeinterface; kelas publik BinaryTree <T> mengimplementasikan BinaryTreeInterface <T> {private BinaryNode <T> root; // 只要一个数据节点就够了 // 构造空树 BinaryTree publik () {root = null; } // 用 rootData 构造树(有个根) BinaryTree publik (t rootData) {root = BinaryNode baru <T> (rootData); } // 用其他树构造树 BinaryTree publik (t rootData, BinaryTree <T> LeftTree, BinaryTree <T> RightTree) {root = BinaryNode baru <T> (rootData); if (lefttree! = null) {root.setleft (lefttree.root); } if (rightTree! = null) {root.setright (rightTree.root); }} // 用 rootData 设置树(有个根) @Override public void settree (t rootData) {root = BinaryNode baru <T> (rootData); } // 用其他树设置树 public void settree (t rootData, BinaryTreeInterface <T> Kiri, BinaryTreeInterface <T> kanan) {root = BinaryNode baru <T> (rootData); BinaryTree Lefttree = null; BinaryTree rightTree = null; if ((lefttree = (BinaryTree) kiri)! = null) {root.setleft (lefttree.root); } if ((rightTree = (BinaryTree) kanan)! = null) {root.setright (righttree.root); }} @Override public void clear () {root = null; } @Override public int getHeight () {// TODO Metode yang dihasilkan otomatis Stub Return root.getHeight (); } @Override Public int getNumberOfRoot () {// TODO Metode yang dihasilkan secara otomatis Stub Return 0; } @Override public t getrootData () {if (root! = Null) return root.getData (); lagi kembali nol; } publik 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 = stack baru <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 = BinaryTree baru <string> (); BinaryTree <String> t8 = BinaryTree baru <string> ("8"); BinaryTree <String> t7 = BinaryTree baru <string> ("7"); t.settree ("6" t7, t7, t7) ("7"); t.settree ("6" ("7"); t.settree ("6"); // 用 t7, t8 设置树 tt.inorderStackTraverse (); System.out.println (t.getHeight ());}}通过此文 , 希望能帮助到大家 , 谢谢大家对本站的支持!