實驗7、二叉樹的應用
阿新 • • 發佈:2020-12-10
-(1)實驗目的
通過該實驗,使學生理解二叉樹的鏈式儲存,掌握二叉樹的幾種遍歷演算法,並通過該實驗使學生理解遞迴的含義,掌握C語言編寫遞迴函式的方法和注意事項。
-(2)實驗內容
實現教材中演算法6.4描述的二叉樹建立演算法,在此基礎上實現二叉樹的先序、後序遞迴遍歷演算法、兩種非遞迴中序遍歷、層序遍歷、求二叉樹的深度。注意:在非遞迴演算法中用到棧和佇列時,不要呼叫系統的棧和佇列,需要自己實現棧和佇列的操作。
package sjjg;
import java.util.*;
public class BanaryTree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("********************************");
System.out.println("**********1.建立二叉樹************");
System.out.println("**********2.先序遍歷二叉樹*********");
System. out.println("**********3.中序遍歷二叉樹1********");
System.out.println("**********4.中序遍歷二叉樹2********");
System.out.println("**********5.後序遍歷二叉樹*********");
System.out.println("**********6.層序遍歷二叉樹*********");
System.out.println("**********7.求二叉樹的深度*********" );
System.out.println("**********8.退出*****************");
Tree tree = new Tree();
int q = 1;
Node[] t=new Node[10];
while (q == 1) {
int a = scanner.nextInt();
switch (a) {
case 1:
Node root = new Node("A");
Node d1 = new Node("B");
Node d2 = new Node("C");
Node d3 = new Node("D" );
Node d4 = new Node("E" );
Node d5 = new Node( "F");
Node d6 = new Node( "G");
tree.setRoot(root);
root.setLefrnode(d1);
d1.setLefrnode(d2);
d1.setRightnode(d3);
d3.setLefrnode(d4);
d3.setRightnode(d5);
d4.setRightnode(d6);
System.out.println("建立成功!");
break;
case 2:
System.out.println("前序查詢:");
tree.DLR();
System.out.println();
break;
case 3:
System.out.println("中序查詢(1):");
tree.LDR();
System.out.println();
break;
case 4:
System.out.println("中序查詢(2):");
tree.LDR2();
System.out.println();
break;
case 5:
System.out.println("後序查詢:");
tree.LRD();
System.out.println();
break;
case 6:
System.out.println("層序遍歷:");
tree.cX();
System.out.println();
break;
case 7:
System.out.println("樹的深度: ");
tree.deep();
break;
case 8:
return;
}
}
}
}
class Node {
public Node() {
}
public String getZm() {
return Zm;
}
public void setZm(String zm) {
Zm = zm;
}
private String Zm;
private Node lefrnode;
private Node rightnode;
public Node(String Zm) {
this.Zm = Zm;
}
public void setLefrnode(Node lefrnode) {
this.lefrnode = lefrnode;
}
public void setRightnode(Node rightnode) {
this.rightnode = rightnode;
}
public Node getLefrnode() {
return lefrnode;
}
public Node getRightnode() {
return rightnode;
}
//前序遍歷
public Node DLR() {
System.out.print(this);
if (this.lefrnode != null) {
this.lefrnode.DLR();
}
if (this.rightnode != null) {
this.rightnode.DLR();
}
return null;
}
//中序遍歷(1)
public void LDR() {
if (this.lefrnode != null) {
this.lefrnode.LDR();
}
System.out.print(this);
if (this.rightnode != null) {
this.rightnode.LDR();
}
}
//中序遍歷(2)
public void LDR2() {
Stack<Node> stack = new Stack<>();
Node P=this;
stack.add(P);
while (P!=null||!stack.isEmpty()) {
if (P!= null&&P.lefrnode!=null) {
stack.add(P.lefrnode);
P=P.lefrnode;
}else {
P=stack.pop();
System.out.print(P);
if (P!=null&&P.rightnode!=null){
stack.add(P.rightnode);
P=P.rightnode;
}else {
P=null;
}
}
}
}
//後序遍歷
public void LRD() {
if (this.lefrnode != null) {
this.lefrnode.LRD();
}
if (this.rightnode != null) {
this.rightnode.LRD();
}
System.out.print(this);
}
//層序遍歷
ArrayList<Node> arrayList = new ArrayList<>();
public void cX() {
Queue<Node> queue = new LinkedList<>();
if (this != null) {
queue.add(this);
}
while (!queue.isEmpty()) {
ArrayList<Node> arrayList1 = new ArrayList<>();
while (!queue.isEmpty()) {
arrayList1.add(queue.poll());
}
for (int i = 0; i < arrayList1.size(); i++) {
arrayList.add(arrayList1.get(i));
if (arrayList1.get(i).lefrnode != null) {
queue.offer(arrayList1.get(i).lefrnode);
}
if (arrayList1.get(i).rightnode != null) {
queue.offer(arrayList1.get(i).rightnode);
}
}
}
System.out.println(arrayList);
}
//求深度
public void deep() {
int deep1=0,count=0,nextcount=1;
Queue<Node> queue=new LinkedList<>();
if (this != null) {
queue.add(this);
}
while (!queue.isEmpty()) {
Node p = queue.poll();
count++;
if (p.lefrnode != null) {
queue.add(p.lefrnode);
}
if (p.rightnode != null) {
queue.add(p.rightnode);
}
if (count == nextcount) {
nextcount = queue.size();
count = 0;
deep1++;
}
}
System.out.println(deep1);
}
public String toString() {
return Zm;
}
}
class Tree {
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void DLR() {
if (root != null) {
this.root.DLR();
} else {
System.out.println("二叉樹為空無法遍歷。");
}
}
public void LDR() {
if (root != null) {
this.root.LDR();
} else {
System.out.println("二叉樹為空無法遍歷。");
}
}
public void LDR2() {
if (root != null) {
this.root.LDR2();
} else {
System.out.println("二叉樹為空無法遍歷.");
}
}
public void LRD() {
if (root != null) {
this.root.LRD();
} else {
System.out.println("二叉樹為空無法遍歷.");
}
}
public void cX(){
if (root!=null){
this.root.cX();
}else {
System.out.println("二叉樹為空無法遍歷。");
}
}
public void deep(){
if (root!=null){
this.root.deep();
}else {
System.out.println("二叉樹為空無法遍歷。");
}
}
public String toString() {
return String.valueOf(root);
}
}