1。 二叉树接口
الواجهة العامة BinaryTreeInterface <T> {public t getRootData () ؛ العام int getheight () ؛ العام int getNumberOfroot () ؛ الفراغ العام clear () ؛ public void settree (t rootdata) ؛ // 用 rootdata 设置树 public void settree (t rootdata ، BinaryTreeInterface <T> يسار ، BinaryTreeInterface <T> يمين) ؛ // 设置树 , 用左右子节点} 2 节点类
package com.jimmy.impl ؛ public class BinaryNode <T> {private t data ؛ private binaryNode <T> left ؛ // 左子节点 private binaryNode <T> يمين ؛ // 右子节点 public BinaryNode () {this (null) ؛} public BinaryNode (t data) {this (data ، null ، null) ؛} public binarynode (t data ، binarynode <t> left ، binarynode <t> right) {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 ؛ } setLeft public void (BinaryNode <T> يسار) {this.left = left ؛ } public BinaryNode <T> getRight () {return right ؛ } public void setRight (BinaryNode <T> right) {this.right = right ؛ } boolean hasleft () {return left! = null ؛ } boolean hasright () {return right! = null ؛ } boolean public isLeaf () {return (left == null) && (right == 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)) ؛ العودة ح. } public int getNumofnodes () {int lnum = 0 ، rnum = 0 ؛ if (left! = null) lnum = left.getnumofnodes () ؛ إذا (يمين! = null) rnum = right.getNumofNodes () ؛ إرجاع lnum+rnum+1 ؛ }}3. 二叉树实现
package com.jimmy.impl ؛ import java.util.stack ؛ import com.jimmy.binaryTreeInterface ؛ public class 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> 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 method method tuto method round.getheight () ؛ } Override public int getNumberOfroot () {// todo method method method tuto 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 void inordertraverse () {inordertraverse (root) ؛ } // 用栈方法遍历 public void inorderstacktraverse () {stack <BinaryNode> stack = new stack <BinaryNode> () ؛ BinaryNode cur = الجذر ؛ //stack.push(root) ؛ بينما (! stack.isempty () || (cur! = null)) {بينما (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") // 用 t7 ، t8 设置树 tt.inorderstacktraverse () ؛ system.out.println (t.getheight ()) ؛}}通过此文 , 希望能帮助到大家 , 谢谢大家对本站的支持!