1。 二叉树接口
interface pública binarytreeInterface <t> {public t getRootData (); public int getHeight (); public int getNumberOfroot (); public void clear (); public void setTree (T rootata); // 用 rootdata 设置树 public void settree (t rootdata, binarytreeInterface <t> esquerda, binarytreeInterface <T> à direita); // 设置树 , 用左右子节点} 2 节点类
pacote com.jimmy.impl; public class BinaryNode <T> {Dados T Private; Private BinaryNode <T> Esquerda; // 左子节点 左子节点 BinaryNode privado <T> Certo; // 右子节点 public binaryNode () {this (null);} public binaryNode (t data) {this (dados, nulo, nulo);} public binaryNode (t dados, binaryNode <t> esquerda, binaryNode <T> direita) {this.data = data; this.left = esquerda; this.right = Right;} public t getData () {retornar dados; } public void setData (t data) {this.data = data; } public binaryNode <T> getLeft () {return à esquerda; } public void SetLeft (BinaryNode <T> Esquerda) {this.left = esquerda; } public binaryNode <T> getRright () {return Right; } public void Setright (BinaryNode <T> Right) {this.right = Right; } public boolean hasleft () {return esquerda! = null; } public boolean hasright () {return Right! = null; } public boolean isleaf () {return (esquerda == null) && (direita == null); } public int getHeight () {return getHeight (this); } public int getHeight (nó bininarynode <T>) {int h = 0; if (node! = null) h = 1+math.max (node.getHeight (node.left), node.getHeight (node.right)); retornar h; } public int getNumOfnodes () {int lnum = 0, rnum = 0; if (esquerda! = null) lnum = esquerd.getNumofNodes (); if (certo! = null) rnum = right.getNumOfnodes (); retornar lnum+rnum+1; }}3. 二叉树实现
pacote com.jimmy.impl; importar java.util.stack; importar com.jimmy.binarytreeInterface; classe pública binarytree <t> implementa o 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> esquerda, binaryTreeInterface <T> direita) {root = new BinaryNode <T> (rootData); Binarytree leftTree = null; BinaryTree RightTree = NULL; if ((leftTree = (binaryTree) à esquerda)! = null) {root.setLeft (leftTree.root); } if ((RightTree = (binaryTree) à direita)! = null) {root.setright (righttree.root); }} @Override public void clear () {root = null; } @Override public int getHeight () {// TODO Method Auto-Gerated Stub Return root.getHeight (); } @Override public int getNumberOfroot () {// TODO Method Auto-Generated Stub Return 0; } @Override public t getRootData () {if (root! = Null) retornar root.getData (); mais retornar 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 (root); } // 用栈方法遍历 public void neordestackTraverse () {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 <tring> ("8"); binarytree <string> t7 = new BinaryTee <string> ("7"); // 用 t7, t8 设置树 tt.IrorderstackTraverse (); System.out.println (t.getHeight ());}}通过此文 , 希望能帮助到大家 , 谢谢大家对本站的支持!