題目 :
請依照下面 Binary tree 的 Structure, 寫出函式API 傳入該 Binary tree 的資料結構後返回該 Binary tree 的 deepest depth.
* Binary Tree 資料結構 :
- typedef struct BiTNode{
- int data;
- struct BiTNode *lchild, *rchild;
- }
解題 :
1. 首先為測試目的, 撰寫函數 BiTNode* createBiTree() 建立測試用的 Binary tree.
2. 依據提議撰寫函數 int fetchDepthInBiTree(BiTree tree) 求出傳入 Binary tree 的 deepest depth.
3. 再求 Binary tree 的函數裡利用了 recursive 的方法分別求 binary tree 的 left tree 與 right tree 的 depth, 再回傳較大的值回去.
範例代碼 :
* BitTree.h 代碼 :
- #include
- #include
-
- typedef struct BiTNode{
- int data;
- struct BiTNode *lchild, *rchild;
- }BiTNode, *BiTree;
-
- BiTNode* createBiTree();
-
- int fetchDepthInBiTree(BiTree tree);
* BitTree.cpp 代碼 :
- #include "BitTree.h"
-
- BiTNode* createBiTree() {
- BiTree node;
- printf("Please key in int to create BinaryTree (0 to exit): ");
- int tmp = 0;
- scanf("%d", &tmp);
-
- if(tmp > 0) {
- node = new BiTNode;
- node->data = tmp;
- printf("\tFor left node...");
- node->lchild = createBiTree();
- printf("\tFor right node...");
- node->rchild = createBiTree();
- return node;
- } else {
- return NULL;
- }
- }
-
- int fetchDepthInBiTree(BiTree tree){
- if(tree == NULL)
- return 0;
- int r = 1+ fetchDepthInBiTree(tree->rchild);
- int l = 1+ fetchDepthInBiTree(tree->lchild);
- if(r>l) {
- return r;
- } else {
- return l;
- }
- }
* 執行範例代碼 :
- BiTree biTree = createBiTree();
- int depth = fetchDepthInBiTree(biTree);
- printf("Depth of Binary Tree= %d\n", depth);
執行結果 :
首先建立 Binary tree 如下 :
執行過程 :
Please key in int to create BinaryTree (0 to exit): 1
For left node...Please key in int to create BinaryTree (0 to exit): 2
For left node...Please key in int to create BinaryTree (0 to exit): 3
For left node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 4
For left node...Please key in int to create BinaryTree (0 to exit): 6
For left node...Please key in int to create BinaryTree (0 to exit): 5
For left node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 7
For left node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 8
For left node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 9
For left node...Please key in int to create BinaryTree (0 to exit): 10
For left node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 11
For left node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 0
For right node...Please key in int to create BinaryTree (0 to exit): 0
Depth of Binary Tree= 6
補充說明 :
*
[ 資料結構 小學堂 ] 樹狀結構導論 : 二元樹的儲存方式
沒有留言:
張貼留言