1。 二叉树接口
public interface 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> links, BinaryTreeInterface <T> rechts); // 设置树 , 用左右子节点} 2 节点类
Paket com.jimmy.impl; öffentliche Klasse BinaryNode <T> {private t data; private BinaryNode <T> links; // 左子节点 Private BinaryNode <t> rechts; // 右子节点 public BinaryNode () {this (null);} public BinaryNode (t data) {this (data, null, null);} public BinaryNode (t data, binaryNode <t> links, BinaryNode <T> rechts) {this.data = data; this.left = links; this.right = rechts;} public t getData () {returndaten; } public void setData (t data) {this.data = data; } public BinaryNode <T> getleft () {return links; } public void setleft (binaryNode <t> links) {this.left = links; } public BinaryNode <T> getRight () {Return Right; } public void setright (binaryNode <t> rechts) {this.right = rechts; } public boolean hasleft () {return links! = null; } public boolean hasright () {return right! = null; } public boolean isleaf () {return (links == null) && (rechts == null); } public int getheight () {return getheight (this); } public int getheight (BinaryNode <T> Knoten) {int H = 0; if (node! Rückkehr H; } public int getnumofnodes () {int lnum = 0, rnum = 0; if (links! = null) lnum = links.getNumofnodes (); if (rechts! = null) rnum = rechts.getNumofnodes (); return lnum+rnum+1; }}3. 二叉树实现
Paket com.jimmy.impl; import java.util.stack; import com.jimmy.baryTreeInterface; public class BinaryTree <T> Implementiert 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> linksTree, binaryTree <T> righttree) {root = new BinaryNode <T> (rootData); if (LeftTree! } 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> links, BinaryTreeInterface <T> rechts) {root = new BinaryNode <T> (rootData); BinaryTree LeftTree = NULL; BinaryTree righttree = null; if ((linksTree = (binaryTree) links)! = null) {root.setLeft (LeftTree.root); } if ((rightTree = (binaryTree) rechts)! = null) {root.setright (rightTree.root); }} @Override public void clear () {root = null; } @Override public int getheight () {// Todo automatisch generierte Methode Stub return root.getheight (); } @Override public int getNumberofroot () {// Todo automatisch generierte Methode Stub Return 0; } @Override public t GetRootData () {if (root! = Null) return root.getData (); sonst kehre null zurück; } 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); } //. 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> Knoten) {if (node! = Null) {inOrderTraverse (node.getleft ()); System.out.println (node.getData ()); inordntraverse (node.getRight ()); }} public static void main (String [] args) {binaryTree <string> t = neuer BinaryTree <string> (); BinaryTree <string> t8 = neuer BinaryTree <string> ("8"); BinaryTree <string> t7 = new BinaryTree <string> ("7"; // 用 t7, t8 设置树 tt.inorderStacktraverse (); System.out.println (T.Getheight ());}}通过此文 , 希望能帮助到大家 , 谢谢大家对本站的支持! 谢谢大家对本站的支持!