1。 二叉树接口
interfaz pública binarytreeinterface <t> {public t getRootData (); público int getheight (); público int getNumberOfRoot (); público nulo claro (); public void settree (t rootdata); // 用 RootData 设置树 public void settree (t rootdata, binarytreeinterface <t> izquierda, binarytreeinterface <t> derecha); // 设置树 用左右子节点}} 2 节点类
paquete com.jimmy.impl; public class binaryNode <t> {private t data; private binarynode <t> izquierda; // 左子节点 BinaryNode privado <t> derecho; // 右子节点 public binaryNode () {this (null);} public binaryNode (t data) {this (data, null, null);} public binaryNode (t data, binarynode <t> izquierda, binarynode <t> derecho) {this.data = data; this.left = izquierda; this.right = right;} public t getData () {return data; } public void setData (t data) {this.data = data; } public BinaryNode <T> getLeft () {return a la izquierda; } public void setleft (binaryNode <T> izquierda) {this.left = izquierda; } 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 getheHight (this); } public int getheight (binaryNode <t> nodo) {int h = 0; if (nodo! = null) h = 1+math.max (node.getheight (node.left), node.getheight (node.right)); regresar h; } public int getNumOfNodes () {int lnum = 0, rnum = 0; if (izquierda! = null) lnum = left.getNuMOfNodes (); if (derecho! = null) rnum = right.getNuMOfNodes (); return lnum+rnum+1; }}3. 二叉树实现
paquete com.jimmy.impl; import java.util.stack; import com.jimmy.binarytreeinterface; public class binarytree <t> implementa 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> 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> izquierda, binarytreeinterface <t> right) {root = new BinaryNode <T> (rootData); BinaryTree LeftTree = nulo; Binarytree righttree = null; if ((lefttree = (binarytree) izquierda)! = null) {root.setleft (lefttree.root); } if ((righttree = (binarytree) derecho)! = null) {root.setRight (righttree.root); }} @Override public void clear () {root = null; } @Override public int getheight () {// TODO Auto Generado Método STUB return Root.getheight (); } @Override public int getNumberOfRoot () {// TODO Auto Generado Método STUB Return 0; } @Override public t getRootData () {if (root! = Null) return root.getData (); de lo contrario regresa nulo; } 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 (raíz); } // 用栈方法遍历 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> nodo) {if (node! = Null) {inOrderTraverse (node.getleft ()); System.out.println (node.getData ()); inloderTraverse (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 ());}}通过此文 , 希望能帮助到大家 谢谢大家对本站的支持! 谢谢大家对本站的支持!