1。 二叉树接口
публичный интерфейс 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> слева, бинарноинтерфейс <T> справа); // 设置树 , 用左右子节点} 2 节点类
пакет com.jimmy.impl; открытый класс BinaryNode <t> {private t data; private binaryNode <T> слева; // 左子节点 Приватный бинарический конец <T> справа; // 右子节点 public binaryNode () {this (null);} public binaryNode (t data) {this (data, null, null);} public binaryNode (t data, binaryNode <T> слева, binaryNode <t> справа) {this.Data = data; this.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> слева) {this.left = Left; } public BinaryNode <t> getright () {return right; } public void setright (binaryNode <t> справа) {this.right = right; } public boolean hasleft () {return left! = null; } public boolean hasright () {return right! = null; } public boolean isleaf () {return (left == null) && (справа == 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)); возврат H; } public int getNumOfNodes () {int lnum = 0, rnum = 0; if (слева! = null) lnum = left.getnumofnodes (); if (right! = null) rnum = right.getnumofnodes (); вернуть lnum+rnum+1; }}3. 二叉树实现
пакет com.jimmy.impl; import java.util.stack; import com.jimmy.binaryTreeInterface; открытый класс BinaryTree <t> реализует 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> Leathertree, 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 LeatsTree = NULL; BinaryTree righttree = null; if ((LeatsTree = (binaryTree) слева)! = null) {root.setleft (LeatsTree.Root); } if ((righttree = (binarytree) right)! = null) {root.setright (righttree.root); }} @Override public void clear () {root = null; } @Override public int getheight () {// todo Автопогенерированный метод заглушка return root.getheight (); } @Override public int getNumberOfRoot () {// todo автоматически генерируемый метод return 0; } @Override public t getRootdata () {if (root! = Null) return root.getData (); иначе вернуть ноль; } public BinaryNode <t> getRoot () {return root; } public void setRoot (binaryNode <t> root) {this.root = root; } public int getNumOfNodes () {return root.getNumOfNodes (); } public inOrderTraverse () {indorderTraverse (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 ());}}通过此文 , 希望能帮助到大家 谢谢大家对本站的支持!