實驗目的及要求:
通過深入學習樹(Tree)和二叉樹(Binary Tree)這兩種重要的數據結構,掌握它們的基本概念、性質和操作,提高對樹形結構的理解和應用能力。通過本實驗,學生將深化對樹和二叉樹等數據結構的理解,提高編程能力和問題解決能力,為進一步學習復雜數據結構和算法打下基礎。
實驗設備環境:
1.微型計算機
2.DEV C++(或其他編譯軟件)
任務:
編寫程序,建立如圖 8-10(b) 所示的帶頭結點的二叉鏈存儲結構二叉樹,首先打印該二叉樹,然后分別輸出按照前序遍歷、中序遍歷和后序遍歷方法訪問各結點的信息,最后,查找字符'E是否在該二叉樹中。
[輸出顯示函數設計] 按照某種遍歷方法輸出二叉樹各結點的信息,其實就是把上述各遍歷二叉樹函數中的 Visit0 設計成輸出結點信息的函數。Visit0 設計如下:
void Visit(DataType item){
printf("%c",item);
}
[設計]
設二叉樹的結點定義以及帶頭結點二叉樹的初始化操作、左結點插入操作、右結點插入操作、左子樹刪除操作、右子樹刪除操作的實現函數存放在文件 BiTree.h 中,設二叉樹遍歷操作和撤銷操作的實現函數存放在文件 BiTreeTraverse.h 中。
代碼如下:
頭文件BiTree:#include<stdio.h>
#include<stdlib.h>
typedef char DataType;
typedef struct Node {DataType data;struct Node *leftChild;struct Node *rightChild;
} BiTreeNode;
void Initiate(BiTreeNode **root) {*root = (BiTreeNode*)malloc(sizeof(BiTreeNode));(*root)->leftChild = NULL;(*root)->rightChild = NULL;
}
BiTreeNode *InsertLeftNode(BiTreeNode *curr,DataType x) {BiTreeNode *s, *t;if(curr == NULL)return NULL;t = curr->leftChild;s = (BiTreeNode*)malloc(sizeof(BiTreeNode));s->data = x;s->leftChild = t;s->rightChild = NULL;curr->leftChild = s;return curr->leftChild;
}
BiTreeNode *InsertRightNode(BiTreeNode *curr,DataType x) {BiTreeNode *s, *t;if(curr == NULL)return NULL;t = curr->rightChild;s = (BiTreeNode*)malloc(sizeof(BiTreeNode));s->data = x;s->rightChild = t;s->leftChild = NULL;curr->rightChild = s;return curr->rightChild;
}
void Destroy(BiTreeNode **root) {if((*root) != NULL && (*root)->leftChild != NULL)Destroy(&(*root)->leftChild);if((*root) != NULL && (*root)->rightChild != NULL)Destroy(&(*root)->rightChild);free(*root);
}頭文件BiTreeTraverse:#include"BiTree.h"
void Visit(DataType item) {printf("%c ",item);
}
void PrintBiTree(BiTreeNode *root, int n) {int i;if(root == NULL)return;PrintBiTree(root->rightChild,n + 1);for(i = 0; i < n-1 ; i++)printf(" ");if(n>0) {printf("---");printf("%c\n",root->data);}PrintBiTree(root->leftChild,n + 1);
}
BiTreeNode *Search(BiTreeNode *root,DataType x) {BiTreeNode *find=NULL;if(root!=NULL) {if(root->data==x)find=root;else {find=Search(root->leftChild,x);if(find==NULL)find=Search(root->rightChild,x);}}return find;
}
void PreOrder(BiTreeNode *t,void Visit(DataType item)) {if(t != NULL) {Visit(t->data);PreOrder(t->leftChild, Visit);PreOrder(t->rightChild, Visit);}
}
void InOrder(BiTreeNode *t,void Visit(DataType item)) {if(t != NULL) {InOrder(t->leftChild, Visit);Visit(t->data);InOrder(t->rightChild, Visit);}
}
void PostOrder(BiTreeNode *t,void Visit(DataType item)) {if(t != NULL) {PostOrder(t->leftChild, Visit);PostOrder(t->rightChild, Visit);Visit(t->data);}
}8-2:#include"BiTreeTraverse.h"
int main() {BiTreeNode *root,*p,*find;char x='E';Initiate(&root);p=InsertLeftNode(root,'A');p=InsertLeftNode(p,'B');p=InsertLeftNode(p,'D');p=InsertRightNode(p,'G');p=InsertRightNode(root->leftChild,'C');InsertLeftNode(p,'E');InsertRightNode(p,'F');PrintBiTree(root,0);printf("前序遍歷:");PreOrder(root->leftChild,Visit);printf("\n中序遍歷:");InOrder(root->leftChild,Visit);printf("\n后序遍歷:");PostOrder(root->leftChild,Visit);find=Search(root,x);if(find!=NULL)printf("\n數據元素%c在二叉樹中",x);elseprintf("\n數據元素%c不在二叉樹中",x);Destroy(&root);return 0;
}