实验目标:
(1)以二叉树的先序序列创建二叉树的二叉链表存储结构,先序序列包含空结点
(2)用递归算法实现前序遍历二叉树,输出遍历序列
(3)用递归算法实现中序遍历二叉树,输出遍历序列
(4)用递归算法实现后序遍历二叉树,输出遍历序列
(5)用非递归算法实现中序遍历二叉树,输出遍历序列
(6)求二叉树深度,返回深度值
(7)统计二叉树结点个数,返回二叉树结点个数
(8)查找二叉树结点,在根指针为 T 的二叉树中,查找值为 x 的结点, 查找成功, p 赋值为该结点指针,返回 true;查找失败, p==NULL ,返回 false
实验代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include<bits/stdc++.h> using namespace std;
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2
typedef char TElemType;
#define MAX_TREE_SIZE 100 typedef TElemType SqBiTree[MAX_TREE_SIZE]; SqBiTree bt;
typedef struct BiNode { TElemType data; struct BiNode *lchild, *rchild; }*BiTree,BiNode;
void CreateBiTree(BiTree &T) { char ch; cout<<"请输入要插入的值:"; cin>>ch; cout<<endl; if(ch=='#') T=NULL; else{ T=(BiTree)malloc(sizeof(BiNode)); if(T==NULL){ cout<<"分配存储空间失败!"<<endl; return; } T->data=ch; CreateBiTree(T->lchild); CreateBiTree(T->rchild); } return; }
void PreOrderTraverse (BiTree T) { if(T==NULL) return; cout<<T->data<<" "; PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); }
void InOrderTraverse (BiTree T) { if(T==NULL) return; InOrderTraverse(T->lchild); cout<<T->data<<" "; InOrderTraverse(T->rchild); }
void PostOrderTraverse (BiTree T) { if(T==NULL) return; PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); cout<<T->data<<" "; }
void InOrderTraverseNon (BiTree T) { BiTree stack[MAX_TREE_SIZE],Node; int top=0; if(T==NULL) return;
Node=T;
while (Node != NULL || top>0){ while(Node != NULL){ stack[++top]=Node; Node=Node->lchild; }
Node=stack[top--]; cout<<Node->data<<" "; Node=Node->rchild; } }
int Depth(BiTree T) { if(T==NULL) return ERROR; char maxLeft=Depth(T->lchild); char maxRight=Depth(T->rchild); if(maxLeft>maxRight) return (1+maxLeft); else return (1+maxRight); }
int Count(BiTree T) { if(T==NULL) return ERROR;
else return Count(T->lchild) + Count(T->rchild)+1; }
bool Search(BiTree T, TElemType x, BiTree &p) { if(T==NULL) return ERROR;
if(T->data==x){ p=T; return p; }
if(Search(T->lchild,x,p)) return Search(T->lchild,x,p); else return Search(T->rchild,x,p); }
int main() { BiTree T; while(1){ cout<<endl; cout<<"**************************************************************************"<<endl; cout<<"**1,创建二叉树,结束输入请输入“#” \t**"<<endl; cout<<"**2,先序遍历 \t**"<<endl; cout<<"**3,中序遍历 \t**"<<endl; cout<<"**4,后序遍历 \t**"<<endl; cout<<"**5,以非递归函数中序遍历 \t**"<<endl; cout<<"**6,最大深度 \t**"<<endl; cout<<"**7,节点数 \t**"<<endl; cout<<"**8,查找值为x的结点 \t**"<<endl; cout<<"**0,退出系统 \t**"<<endl; cout<<"**************************************************************************"<<endl<<endl;
int choice; cout<<"请输入要进行的操作:"; cin>>choice; cout<<endl;
switch(choice){ case 1:{ CreateBiTree(T); cout<<endl; break; } case 2:PreOrderTraverse(T);cout<<endl;break; case 3:InOrderTraverse(T);cout<<endl;break; case 4:PostOrderTraverse(T);cout<<endl;break; case 5:InOrderTraverseNon(T);cout<<endl;break; case 6:cout<<Depth(T)<<endl;cout<<endl;break; case 7:cout<<Count(T)<<endl;cout<<endl;break; case 8:{ TElemType x; cout<<"请输入要查找的值:"; cin>>x; cout<<endl; BiTree p; Search(T,x,p); cout<<"查找成功!"<<endl; break; } case 0:exit(0); } } return 0; }
|