1。 二叉树接口
공개 인터페이스 BinaryTreeInterface <t> {public t getRootData (); public int getheight (); 공개 int getNumberOfroot (); 공개 void clear (); 공개 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> 왼쪽; // private binarynode <t> 오른쪽; // public binarynode () {this (null);} public binarynode (t data) {this (data, null, null);} public binarynode (t data, binarynode <t> 왼쪽, binarynode <t> 오른쪽) {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 () {왼쪽 리턴; } public void setleft (binarynode <t> 왼쪽) {this.left = 왼쪽; } public binarynode <t> getright () {rack right; } public void setright (binarynode <t> 오른쪽) {this.right = 오른쪽; } public boolean hasleft () {return left! = null; } public boolean hasright () {return right! = null; } public boolean isleaf () {return (왼쪽 == null) && (오른쪽 == null); } public int getheight () {return getheight (this); } public int getheight (binarynode <t> 노드) {int h = 0; if (node! = null) h = 1+math.max (node.getheight (node.left), node.getheight (node.right)); 반환 h; } public int getNumofnodes () {int lnum = 0, rnum = 0; if (left! = null) lnum = left.getnumofnodes (); if (right! = 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) left)! = 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 자동 생성 메소드 스터브 return root.getheight (); } @Override public int getNumberOfRoot () {// TODO 자동 생성 메소드 스터브 리턴 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.getUmofNodes (); } public void inorderTraverse () {inorderTraverse (root); } // 用栈方法遍历 public void inorderstacktraverse () {stack <binarynode> stack = new Stack <binarynode> (); Binarynode cur = 루트; //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 <문자열> (); BinaryTree <string> t8 = new BinaryTree <string> ( "8"); BinaryTree <string> t7 = new BinaryTree <string> ( "7"); T.SetTree ( "6", T8); // 用 t7, t8 设置树 tt.inorderstacktraverse (); system.out.println (t.getheight ());}}通过此文 通过此文, 希望能帮助到大家, 谢谢大家对本站的支持!