1、用先序方法建立一棵二叉树;
2、实现输出二叉树先序、中序和后序遍历序列中第k个数据元素的操作;
3、实现判断二叉树是否是完全二叉树的操作。
BiTreeNode.java
package experiment2; public class BiTreeNode { public Object data; public BiTreeNode lchild,rchild; public BiTreeNode(){ this(null); } public BiTreeNode(Object data){ this(data,null,null); } public BiTreeNode(Object data,BiTreeNode lchild,BiTreeNode rchild){ this.data=data; this.lchild=lchild; this.rchild=rchild; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } public BiTreeNode getLchild() { return lchild; } public void setLchild(BiTreeNode lchild) { this.lchild = lchild; } public BiTreeNode getRchild() { return rchild; } public void setRchild(BiTreeNode rchild) { this.rchild = rchild; } }
BiTree.java
package experiment2; import java.util.Scanner; public class BiTree { private BiTreeNode root; private static int index=0; int count1=0,count2=0,count3=0; public BiTree() { this.root = null; } public BiTree(BiTreeNode root) { this.root = root; } public BiTree(String preStr){ char c = preStr.charAt(index++); if (c != '#') { root = new BiTreeNode(c); root.lchild = new BiTree(preStr).root; root.rchild = new BiTree(preStr).root; } else root = null; } public void preRootTraverse(int k){ if (root==null) { System.out.println(root); } else { preRootTraverse(root, k); } } private void preRootTraverse(BiTreeNode T,int k) { if (T != null) { count1++; if(count1==k) { System.out.println("先序的数据:"+T.data); } preRootTraverse(T.lchild,k); preRootTraverse(T.rchild,k); } } public void inRootTraverse(int k){ if (root==null) { System.out.println(root); } else { inRootTraverse(root, k); } } private void inRootTraverse(BiTreeNode T,int k) { if (T != null) { inRootTraverse(T.lchild,k); count2++; if (count2==k) { System.out.println("中序的数据:"+T.data); } inRootTraverse(T.rchild,k); } } public void postRootTraverse(int k){ if (root==null) { System.out.println(root); } else { postRootTraverse(root, k); } } private void postRootTraverse(BiTreeNode T,int k) { if (T != null) { postRootTraverse(T.lchild,k); postRootTraverse(T.rchild,k); count3++; if (count3==k) { System.out.println("后序的数据:"+T.data); } } } public BiTreeNode getRoot() { return root; } public void setRoot(BiTreeNode root) { this.root = root; } public int countNode() { return countNode(root); } private int countNode(BiTreeNode t) { if(t == null) return 0; return countNode(t.lchild) + countNode(t.rchild) + 1; } public void isCompleteTree(){ isCompleteTree(root); } private boolean isCompleteTree(BiTreeNode t) { if(t==null){ System.out.println("该二叉树是完全二叉树"); return true; } LinkQueue2<BiTreeNode> q=new LinkQueue2<>(); q.offer(t); boolean isLeafOrLeft=false; while(!q.isEmpty()){ BiTreeNode cur=q.poll(); if (isLeafOrLeft){ if (cur.lchild!=null||cur.rchild!=null){ System.out.println("该二叉树不是完全二叉树"); return false; } } else { if(cur.lchild!=null &&cur.rchild!=null){//左右孩子都有 q.offer(cur.lchild); q.offer(cur.rchild); } else if(cur.lchild!=null){//只有左孩子 q.offer(cur.lchild); isLeafOrLeft=true; } else if(cur.rchild!=null){//只有右孩子 System.out.println("该二叉树不是完全二叉树"); return false; } else{//叶子结点 isLeafOrLeft=true; } } } System.out.println("该二叉树是完全二叉树"); return true; } public static void main(String[] args){ BiTree t=new BiTree("AB##CDF##G##E##");//ABD##EG###CF#H###先序:ABDEGCFH,中序:DBGEAFHC后序:DGEBHFCA //ABDF###E##C##,AB##CDF##G##E## Scanner sc=new Scanner(System.in); System.out.println("请输入k:"); int k=sc.nextInt(); t.preRootTraverse(k); t.inRootTraverse(k); t.postRootTraverse(k); t.isCompleteTree(); } } 代码不能直接运行,参考思想。
LinkQueue2链队列未给出