-(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);
}
}