1。 二叉树接口
Interface publique 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> gauche, binaryTreeInterface <T> à droite); // 设置树 , 用左右子节点} 2 节点类
package com.jimmy.impl; classe publique BinaryNode <T> {données privées; private binaryNode <T> Left; // 左子节点 左子节点 左子节点 左子节点 左子节点 左子节点 左子节点 左子节点 左子节点 BinaryNode privé <T> à droite; // 右子节点 public binaryNode () {this (null);} public binaryNode (t data) {this (data, null, null);} public binaryNode (t data, binaryNode <t> gauche, binaryNode <t> à droite) {this.data = data; this.left = gauche; this.right = droit;} public t getData () {return data; } public void setData (t data) {this.data = data; } public binaryNode <T> getleft () {return à gauche; } public void SetLeft (binaryNode <T> Left) {this.left = Left; } public binaryNode <t> getRight () {return reight; } public void setRight (binaryNode <T> droite) {this.Right = droite; } public boolean hasleft () {return à gauche! = null; } public boolean hasright () {return reight! = null; } public boolean isleaf () {return (Left == null) && (droit == null); } public int getheight () {return getheight (this); } public int getheight (binaryNode <T> nœud) {int h = 0; if (node! = null) h = 1 + math.max (node.getheight (node.left), node.getheight (node.right)); retour h; } public int getnumofnodes () {int lnum = 0, rnum = 0; if (Left! = null) lnum = left.getNumofNodes (); if (à droite! = null) rnum = droite.getNumofNodes (); return lnum + rnum + 1; }}3. 二叉树实现
package com.jimmy.impl; import java.util.stack; import com.jimmy.binaryTreeInterface; public class binarytree <T> implémente 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> LeftRee, binarytree <T> droite) {root = new BinaryNode <T> (rootData); if (LeftTree! = null) {root.setLeft (LeftTree.root); } if (droiteTree! = null) {root.setRight (droiteTree.root); }} // 用 rootdata 设置树 (有个根)) @Override public void setTree (t rootData) {root = new BinaryNode <T> (rootData); } // 用其他树设置树 public void setTree (t rootData, binaryTreeInterface <T> gauche, binaryTreeInterface <T> droite) {root = new BinaryNode <T> (rootData); BinaryTree Lefttree = null; BinaryTree RightTree = NULL; if ((LeftTree = (binarytree) Left)! = null) {root.setLeft (LeftRee.root); } if ((droiteTree = (binaryTree) à droite)! = null) {root.setRight (droiteTree.root); }} @Override public void clear () {root = null; } @Override public int getheight () {// TODO Méthode générée automatique Stume return root.getheight (); } @Override public int getNumberofroot () {// TODO Méthode générée automatique Stub RETOUR 0; } @Override public t getrootdata () {if (root! = Null) return root.getData (); sinon retourne null; } public binaryNode <t> getroot () {return root; } public void setroot (binaryNode <T> root) {this.root = root; } public int getNumofnodes () {return root.getNumofnodes (); } public void inOrderTaverse () {inOrderTaverse (root); } // 用栈方法遍历 public void inorderstackTaverse () {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 inOrderTaverse (binaryNode <t> nœud) {if (node! = Null) {inOrderTaverse (node.getLeft ()); System.out.println (node.getData ()); inOrderTaverse (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.inorderstacktaverse (); System.out.println (t.getheight ());}}通过此文 , 希望能帮助到大家 , 谢谢大家对本站的支持!