1. 程式人生 > >資料結構實踐專案——樹和二叉樹

資料結構實踐專案——樹和二叉樹

【專案1 - 二叉樹演算法庫】

  定義二叉樹的鏈式儲存結構,實現其基本運算,並完成測試。 
要求: 
  1、標頭檔案btree.h中定義資料結構並宣告用於完成基本運算的函式。對應基本運算的函式包括:

<code class="hljs scss has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">void <span class="hljs-function" style="box-sizing: border-box;">CreateBTNode(BTNode *&b,char *str)</span>;        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//由str串建立二叉鏈</span>
BTNode *<span class="hljs-function" style="box-sizing: border-box;">FindNode(BTNode *b,ElemType x)</span>;     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回data域為x的節點指標</span>
BTNode *<span class="hljs-function" style="box-sizing: border-box;">LchildNode(BTNode *p)</span>;      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回*p節點的左孩子節點指標</span>
BTNode *<span class="hljs-function" style="box-sizing: border-box;">RchildNode(BTNode *p)</span>;      <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//返回*p節點的右孩子節點指標</span>
int <span class="hljs-function" style="box-sizing: border-box;">BTNodeDepth(BTNode *b)</span>;     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//求二叉樹b的深度</span>
void <span class="hljs-function" style="box-sizing: border-box;">DispBTNode(BTNode *b)</span>;     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//以括號表示法輸出二叉樹</span>
void <span class="hljs-function" style="box-sizing: border-box;">DestroyBTNode(BTNode *&b)</span>;     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//銷燬二叉樹</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

  2、在btree.cpp中實現這些函式 
  3、在main函式中完成測試,包括如下內容: 
  (1)用”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”建立如圖的二叉樹用於測試。 
這裡寫圖片描述 
  (2)輸出二叉樹 
  (3)查詢值為’H’的節點,若找到,輸出值為’H’的節點的左、右孩子的值 
  (4)求高度二叉樹高度 
  (5)銷燬二叉樹 
  [參考解答

【專案2 - 二叉樹遍歷的遞迴演算法】

  實現二叉樹的先序、中序、後序遍歷的遞迴演算法,並對用”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))”建立的二叉樹進行測試。 
  請利用

二叉樹演算法庫。 
  [參考解答

【專案3 - 利用二叉樹遍歷思想解決問題】

  假設二叉樹採用二叉鏈儲存結構儲存,分別實現以下演算法,並在程式中完成測試: 
  (1)計算二叉樹節點個數; 
  (2)輸出所有葉子節點; 
  (3)求二叉樹b的葉子節點個數; 
  (4)設計一個演算法Level(b,x,h),返回二叉鏈b中data值為x的節點的層數。 
  (5)判斷二叉樹是否相似(關於二叉樹t1和t2相似的判斷:①t1和t2都是空的二叉樹,相似;②t1和t2之一為空,另一不為空,則不相似;③t1的左子樹和t2的左子樹是相似的,且t1的右子樹與t2的右子樹是相似的,則t1和t2相似。) 
  請利用

二叉樹演算法庫。 
  [參考解答] 
  

紙上談兵:“知原理”檢驗題目

1、某樹,用括號表示法描述為:A(B(E),C(F,G(I,J),H(K)),D) 
(1)請用樹形表示法,畫出這個樹 
(2)雙親表示法的資料結構定義為

<code class="hljs d has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> 
{
    ElemType data;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> parent;
} PTree[MaxSize];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

請描述這個樹利用雙親表示法時的儲存 
(3)用孩子鏈儲存時,每個節點定義下面的型別

<code class="hljs d has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> node
{
   ElemType data;   
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> node *sons[MaxSons];
} TSonNode;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

請描述這個樹利用孩子連結串列示法時的儲存 
(4)孩子兄弟鏈儲存結構中的節點定義為

<code class="hljs d has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> tnode 
{
  ElemType data;    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//節點的值</span>
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> tnode *hp;     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//指向兄弟</span>
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> tnode *vp;     <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//指向孩子節點</span>
} TSBNode;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

請描述這個樹利用孩子兄弟連結串列示法時的儲存 
(5)樹結構還可以用一種孩子連結串列示法

<code class="hljs d has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定義孩子節點,將形成一個單鏈表</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> node
{
   Int no;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//孩子節點編號   </span>
   <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> node *next;  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//指向下一個孩子</span>
} NodeType;
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定義樹節點型別,各節點組成順序儲存的線性表,若節點要作為孩子節點,“孩子節點編號(no)”即為在順序表中的序號</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> 
{
    ElemType data;   <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//元素值</span>
    NodeType *firstChild;  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//指向第一個孩子</span>
} CTree[MaxSize];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

請描述這個樹利用孩子兄弟連結串列示法時的儲存。

2、某二叉樹的順序儲存結構如下所示: 
這裡寫圖片描述 
(1)請用樹形表示法,畫出二叉樹的結構 
(2)請用括號表示法,給出這個二叉樹的表示 
(3)請寫出這個二叉樹的先序、中序、後序遍歷序列

3、二叉樹還可以採用一種“偽”鏈式儲存結構,如下所示 
這裡寫圖片描述 
其中,lchild和rchild分別為節點左右孩子的指標域(在這裡,使用節點編號作為指標域值,0表示指標域為空),data為節點的資料域。請畫出這個二叉樹的樹形表示。

4、一棵二叉樹的先序、中序和後序序列分別如下所示,其中有一部分未顯示出來: 
先序:_B_F_ICEH_G 
中序:D_KFIA_EJC_ 
後序:_K_FBHJ_G_A 
請求出空格處的內容,並畫出二叉樹。