1. 程式人生 > >資料結構實驗6:C++實現二叉樹類

資料結構實驗6:C++實現二叉樹類

實驗6

學號:     姓名:      專業:

 

6.1 實驗目的

掌握二叉樹的動態連結串列儲存結構及表示。

掌握二叉樹的三種遍歷演算法(遞迴和非遞迴兩類)。

運用二叉樹三種遍歷的方法求解有關問題。

6.2 實驗要求

按照C++面向物件方法編寫二叉樹類;二叉樹的測試資料可用多種方式進行輸入,如鍵盤輸入、靜態寫入、檔案讀入等。//最難的是從檔案把資料讀進去!

設計二叉樹的二叉連結串列儲存結構,編寫演算法實現下列問題的求解。

<1>打印出二叉樹的三種遍歷序列。

<2>設計演算法按中序次序輸出二叉樹中各結點的值及其所對應的層次數。

<3>求二叉樹的高度。

<4>求二叉樹的結點數。

<5>求二叉樹的葉子結點數。

<6>求二叉樹的度為2的結點數。

<7>鍵盤輸入一個元素x,求其父節點、兄弟結點、子結點的值,不存在時給出相應提示資訊。對兄弟結點和孩子結點,存在時要明確指出是左兄弟、左孩子、右兄弟或右孩子。

<8>鍵盤輸入一個元素x,求其在樹中的層次,不存在時給出相應提示資訊。

<9>將按順序方式儲存在陣列中的二叉樹轉換為二叉連結串列形式。(陣列中要擴充套件為完全二叉樹)。

<10>交換二叉樹中每個結點的左右孩子指標的值。(即:左子樹變為右子樹,右子樹變為左子樹)。

(下面為選做實驗,有興趣的同學完成)

<11>複製一棵二叉樹T到T1。

<12>輸出二叉樹從每個葉子結點到根結點的路徑(經歷的結點)。

<13>對二叉連結串列表示的二叉樹,按從上到下,從左到右列印結點值,即按層次列印。(提示:需要使用佇列)

<14>對二叉連結串列表示的二叉樹,求2個結點最近的共同祖先。

           實驗測試資料基本要求:

<15>求二叉樹中一條最長的路徑長度(邊數),並輸出路徑上的個結點值。

           實驗測試資料基本要求:

6.3 實驗資料要求

自我編寫測試樣例,要求每個功能函式的測試樣例不少於兩組

6.4 執行結果截圖及說明

(後面有空上圖)

6.5 附原始碼

 1 // stdafx.h : include file for standard system include files,
 2 //  or project specific include files that are used frequently, but
 3 //      are changed infrequently
 4 //
 5 
 6 #if !defined(AFX_STDAFX_H__02F8C78B_9F6E_45FF_BFCE_7F99B5AC9359__INCLUDED_)
 7 #define AFX_STDAFX_H__02F8C78B_9F6E_45FF_BFCE_7F99B5AC9359__INCLUDED_
 8 
 9 #if _MSC_VER > 1000
10 #pragma once
11 #endif // _MSC_VER > 1000
12 
13 #include <stdc++.h>
14 #include <windows.h>
15 
16 using namespace std;
17 
18 typedef char elementType;
19 typedef int elementType1;
20 
21 typedef struct node
22 {
23     elementType data;//剛開始應該寫成將data寫成string或者直接將整個函式寫成模板的,寫完了最後測試時
24                     //才發現現在的寫法有諸多不便;但修改的話就又要重構一遍,懶得整了。
25     struct node *leftChild, *rightChild;
26 }bitNode, *binTree;
27 
28 typedef struct charNode
29 {
30     //elementType data;
31     bitNode *data;//the type must be bitNode*
32     struct charNode *link;
33 }CLNode, *CPNode;
34 
35 
36 //typedef struct charNode
37 //{
38     //elementType data;
39     //struct charNode *leftChild, *rightChild;
40 //}charBitNode, *charBinTree;
41 
42 // TODO: reference additional headers your program requires here
43 
44 //{{AFX_INSERT_LOCATION}}
45 // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
46 
47 #endif // !defined(AFX_STDAFX_H__02F8C78B_9F6E_45FF_BFCE_7F99B5AC9359__INCLUDED_)
 1 // charLinkedQueue.h: interface for the charLinkedQueue class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_CHARLINKEDQUEUE_H__13C2F642_81C0_4489_9CF2_3D58D8B48EA9__INCLUDED_)
 6 #define AFX_CHARLINKEDQUEUE_H__13C2F642_81C0_4489_9CF2_3D58D8B48EA9__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 //剛開始嘗試寫英文註釋的,後面知難而退了;不過原來的英文註釋我保留了
13 
14 class charLinkedQueue  
15 {
16 public:
17     charLinkedQueue();
18     virtual ~charLinkedQueue();
19     bool emptyCharLinkedQueue();
20     //bool fullSeqCircleQueue();
21     bool enQueue( bitNode *value );//the type must be bitNode*
22     bool deQueue( /*bitNode *value*/ );
23     bool getFront( bitNode *&value );//the type must be bitNode*&
24     int length();
25     friend ostream &operator<<( ostream &os, charLinkedQueue &clq )
26     {
27         /*
28         if( ( scq._front - 1 ) % maxn == scq._rear )
29             return os;
30         int column  = 0;
31         for( int i = scq._front; i % maxn != scq._rear; i = ( i + 1 ) % maxn )
32         {
33             os << setw(3) << setiosflags(ios::left) << scq.data[i] << " ";
34             column ++;
35             if( column % 10 == 0 )
36                 os << endl;
37         }
38         os << endl;
39         */
40         if( clq._front == NULL )
41             return os;
42         CLNode *tmp = clq._front;
43         int column = 0;
44         while( tmp != clq._rear->link )
45         {
46             os << setw(4) << setiosflags(ios::left) << tmp->data << " ";
47             column ++;
48             tmp = tmp->link;
49             if( column % 10 == 0 )
50                 os << endl;
51         }
52         os << endl;
53     }
54     //為了能順利使用原來的這個程式碼塊來進行二叉樹的層次便利,我主要的精力都放在_front、_rear型別、
55     //deQueue()、enQueue()、charNode的型別確定上,經過無數次嘗試,總算結果對了----
56     //如果有Git,看了這個程式碼的每個版本你就會知道我付出了多少心血。。。。
57 private:
58     CLNode *_front;//the type must be CLNode*
59     CLNode *_rear;//the type must be CLNode*
60 };
61 
62 #endif // !defined(AFX_CHARLINKEDQUEUE_H__13C2F642_81C0_4489_9CF2_3D58D8B48EA9__INCLUDED_)
 1 // _Binary_Tree.h: interface for the _Binary_Tree class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX__BINARY_TREE_H__9381B15F_E185_4489_9415_360A22C0A4E2__INCLUDED_)
 6 #define AFX__BINARY_TREE_H__9381B15F_E185_4489_9415_360A22C0A4E2__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 #include "charLinkedQueue.h"
13 
14 //剛開始嘗試寫英文註釋的,後面知難而退了;不過原來的英文註釋我保留了
15 
16 class _Binary_Tree  
17 {
18 public:
19     _Binary_Tree();//不帶引數的建構函式
20     _Binary_Tree( elementType *Arr );//帶引數的建構函式
21     void build( elementType *Arr );//從陣列建立二叉樹,相當於初始化;不帶引數的建構函式無法適用於這裡
22     void createNode( binTree BT, elementType *Arr, int number );//根據從陣列讀到的資料先序遞迴建樹
23     virtual ~_Binary_Tree();//解構函式
24     bool createBinaryTree( binTree &BT, elementType stringLine[100][3], int length, int &row );//根據文字資料
25                                                             //先序構造二叉樹
26     bool readFileToArray( elementType stringLine[100][3], int &length );//將文字資料讀入二維陣列中
27     bool emptyBinaryTree();//二叉樹判空,僅適用於帶引數建構函式建立的二叉樹
28     bool _exit( binTree BT, elementType value );//判斷節點資料是否在二叉樹中
29     binTree getNodePoint();//返回根節點地址
30     binTree getNodePoint( binTree BT, elementType value );//返回value在二叉樹中的地址
31     binTree getParent( binTree BT, elementType value );//返回value的父母
32     void PreOrderTraverse(binTree BT);//前序遍歷
33     void InOrderTraverse(binTree BT);//中序遍歷
34     void PostOrderTraverse(binTree BT);//後序遍歷
35     void levelOrderTraverse(binTree BT);//層次遍歷
36     void destroy( binTree BT );//銷燬二叉樹
37     void level( binTree BT, int number );//求二叉樹中各個節點的層次
38     int height( binTree BT );//求二叉樹高度
39     int numberOfBTreeNode( binTree BT );//返回二叉樹節點總數
40     int numberOfBTreeLeafNode( binTree BT, int &number );//返回二叉樹葉節點個數
41     void numberOfNodeDegreeTwo( binTree BT, int &number );//求二叉樹中度為2的節點個數
42     //void family( binTree BT, elementType1 number );
43     void getParent( binTree BT, elementType value, bool &flag );//求value的父節點
44     void getSibling( binTree BT, elementType value, bool &flag );//when call the function, the parameter flag
45                                                                 //must be assigned for false
46                                                                 //求value的兄弟節點,法1;有一個bug
47     void getSibling( binTree BT, elementType value );//求value的兄弟節點,法2
48     void getChild( binTree BT, elementType value, bool &flag );//求value孩子節點
49     int levelJudge( binTree BT, elementType value, int &number, int level );//返回value節點的層次
50     void exchangeLeftAndRightSibling( binTree BT );//交換左右子樹
51     void copyBTree( binTree BT1, binTree BT );//複製二叉樹
52     charLinkedQueue clq;//包含
53     void allLeafToRootPath( binTree BT, elementType *path, int &pathLength );//求所有葉節點到根節點路徑
54     void binaryTreeLongestPath( binTree BT, elementType *path, int &pathLength, 
55         elementType *longestPath, int &longestLength );//求葉節點到根節點的最長路徑
56     binTree nearestAncestor( binTree BT, bitNode *BNode1, bitNode *BNode2 );//求兩個節點的最近祖先
57                                                                             //本來打算用elementType資料
58                                                                             //作為引數的,後面發現行不通
59                                                                             //可能是我太菜了吧
60 private:
61     bitNode *BTree;
62 
63 };
64 
65 #endif // !defined(AFX__BINARY_TREE_H__9381B15F_E185_4489_9415_360A22C0A4E2__INCLUDED_)
 1 // charLinkedQueue.cpp: implementation of the charLinkedQueue class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #include "stdafx.h"
 6 #include "charLinkedQueue.h"
 7 
 8 //////////////////////////////////////////////////////////////////////
 9 // Construction/Destruction
10 //////////////////////////////////////////////////////////////////////
11 
12 charLinkedQueue::charLinkedQueue()
13 {
14     _front = _rear = NULL;
15 }
16 
17 charLinkedQueue::~charLinkedQueue()
18 {
19     CLNode *tmp = NULL;
20     while( _front != _rear )
21     {
22         tmp = _front;
23         _front = _front->link;
24         delete tmp;
25     }
26     cout << "The charLinkedQueue destruction has been called!" << endl;
27 }
28 
29 bool charLinkedQueue::emptyCharLinkedQueue()
30 {
31     return _front == NULL;
32 }
33 
34 bool charLinkedQueue::enQueue( bitNode *value )
35 {
36     CLNode *newNode = new CLNode;
37     if( !newNode )
38     {
39         cerr << "Space allocating falied!Error in charLinkedQueue::enQueue()!" << endl;
40         return false;
41     }
42     newNode->data = value;
43     newNode->link = NULL;
44     if( emptyCharLinkedQueue() )
45     {
46         _front = _rear = newNode;
47     }
48     else
49     {
50         _rear->link = newNode;
51         _rear = newNode;
52     }
53     return true;
54 }
55 
56 bool charLinkedQueue::deQueue( /*elementType &value*/ )
57 {
58     if( emptyCharLinkedQueue() )
59     {
60         cerr << "Node deleting falied!Error in charLinkedQueue::deQueue()!" << endl;
61         return false;
62     }
63     CLNode *tmp = _front;
64     //value = _front->data;
65     _front = _front->link;
66     delete tmp;
67     if( _front == NULL )
68         _rear = NULL;
69     return true;
70 }
71 
72 bool charLinkedQueue::getFront( bitNode *&value )
73 {
74     if( emptyCharLinkedQueue() )
75     {
76         cerr << "Queue is empty!\nNode-data acquiring falied!Error in charLinkedQueue::deQueue()!" << endl;
77         return false;
78     }
79     value = _front->data;//原來我是註釋掉的,導致輸出一直是A;
80     return true;
81 }
82 
83 int charLinkedQueue::length()
84 {
85     if( emptyCharLinkedQueue() )
86     {
87         cerr << "Queue is empty!" << endl;
88         return -1;
89     }
90     CLNode *tmp = _front;
91     int _size = 0;
92     while( tmp != NULL )
93     {
94         tmp = tmp->link;
95         _size ++;
96     }
97     return _size;
98 }
  1 // _Binary_Tree.cpp: implementation of the _Binary_Tree class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 #include "_Binary_Tree.h"
  7 #include "charLinkedQueue.h"
  8 
  9 //////////////////////////////////////////////////////////////////////
 10 // Construction/Destruction
 11 //////////////////////////////////////////////////////////////////////
 12 
 13 
 14 _Binary_Tree::_Binary_Tree()       //新建一個結點
 15 {
 16     //BTree = NULL;
 17     BTree = new bitNode;
 18     BTree->leftChild = BTree->rightChild = NULL;
 19 }
 20 
 21 _Binary_Tree::~_Binary_Tree()
 22 {
 23     destroy(BTree);//解構函式不能帶引數,只能這麼處理了
 24 }
 25 
 26 _Binary_Tree::_Binary_Tree( elementType *Arr )
 27 {
 28     BTree = NULL;
 29     build(Arr);
 30 }
 31 
 32 void _Binary_Tree::build( elementType *Arr )
 33 {
 34     if(BTree)
 35       destroy(BTree);
 36     if( Arr[0] == '^' )
 37     {
 38         BTree = NULL;
 39         return;
 40     }
 41     BTree = new bitNode;
 42     BTree->leftChild = NULL;
 43     BTree->rightChild = NULL;
 44     BTree->data = Arr[0];
 45     createNode( BTree, Arr, 0 );
 46 }
 47 
 48 void _Binary_Tree::createNode( binTree BT, elementType *Arr, int number )
 49 {
 50     bitNode *tmp = new bitNode;
 51     if( Arr[ number * 2 + 1 ] != '^' )
 52     {
 53         BT->leftChild =new bitNode ;
 54         tmp = BT->leftChild;
 55         tmp->data = Arr[ number * 2 + 1 ];
 56         tmp->leftChild = NULL;
 57         tmp->rightChild = NULL;
 58         createNode( tmp, Arr, number * 2 + 1 );
 59     }
 60     if( Arr[ number * 2 + 2 ] != '^' )
 61     {
 62         BT->rightChild =new bitNode ;
 63         tmp = BT->rightChild;
 64         tmp->data = Arr[ number * 2 + 2 ];
 65         tmp->leftChild = NULL;
 66         tmp->rightChild = NULL;
 67         createNode( tmp, Arr, number * 2 + 2 );
 68     }
 69 }
 70 
 71 bool _Binary_Tree::createBinaryTree( binTree &BT, elementType stringLine[100][3], int length, int &row )
 72 {     
 73     if (row >= length || length == 0 )      //strlen存資料的二維陣列,nRow結點所在的位數,nlen結點的個數
 74         return false;
 75     if ( row == 0 )
 76         BT = BTree;
 77     else
 78         BT = new bitNode;//new下面是公用的,用if的目的是改變private裡BTree裡的值
 79     BT->data = stringLine[row][0];
 80     BT->leftChild = NULL;
 81     BT->rightChild = NULL;
 82     
 83     int nextRow = row;
 84     if ( stringLine[nextRow][1] == '1' )
 85     {
 86         ++ row;
 87         createBinaryTree( BT->leftChild, stringLine, length, row );
 88     }
 89     if ( stringLine[nextRow][2] == '1' )
 90     {
 91         ++row;
 92         createBinaryTree( BT->rightChild, stringLine, length, row );
 93     }
 94     return true;
 95 }
 96 
 97 bool _Binary_Tree::readFileToArray( elementType stringLine[100][3], int &length )
 98 {
 99     FILE *fp;
100     char str[100];
101                 
102     cout << "Please input the file name(belike includes the file path):" << endl;
103     char name[50];// = "bt10.btr";
104     cin >> name;
105     fp = fopen( name, "r" );
106     if (!fp)
107     {
108         cout << "Error!" << endl;
109         return false;
110     }
111     if (fgets(str, 1000, fp) != NULL)
112     {
113         if (strcmp(str, "BinaryTree\n") != 0)
114         {
115             cout << "Error!" << endl;
116             fclose(fp);
117             return false;
118         }
119     }
120     length = 0;
121     while (fscanf(fp, "%c %c %c\n", &stringLine[length][0], &stringLine[length][1], &stringLine[length][2]) != EOF)
122     {
123         length ++;
124     }
125     fclose(fp);
126     return true;
127 }
128 
129 
130 bool _Binary_Tree::emptyBinaryTree()
131 {
132     //if(BTree)
133         //return BTree->leftChild == NULL && BTree->rightChild == NULL;
134     //else
135     return BTree == NULL;
136 }
137 
138 bool _Binary_Tree::_exit( binTree BT, elementType value )
139 {
140     if(!BT)
141         return false;
142         //return NULL;
143     if( BT->data == value )
144         return true;
145         //return BT;
146     //bitNode *index = _exit( BT->leftChild, value );
147     bool flag = _exit( BT->leftChild, value );
148     //if(!index)
149     if(!flag)
150     //_exit( BT->leftChild, value );
151         _exit( BT->rightChild, value );
152 }
153 
154 binTree _Binary_Tree::getNodePoint()
155 {
156     //if( emptyBinaryTree() )
157     //{
158         //throw "Empty binary tree!Error in binTree _Binary_Tree::getNodePoint()!\n";
159         //return NULL;
160     //}
161     return (*this).BTree;
162 }
163 
164 binTree _Binary_Tree::getNodePoint( binTree BT, elementType value )
165 {
166     /*
167     if(!BT)
168     {
169         return NULL;
170     }
171     else
172     {
173         if( BT->data == value )
174             return BT;
175         else
176         {
177             bitNode *tmp;
178             if( tmp = getNodePoint( BT->leftChild, value ) )
179                 return tmp;
180             if( tmp = getNodePoint( BT->rightChild, value ) )
181                 return tmp;
182             return NULL;
183         }
184     }
185     */
186     if(!BT)
187     {
188         return NULL;
189     }
190     else
191     {
192         if( BT->data == value )
193         {
194             return  BT;
195         }
196         //getNodePoint( BT->leftChild, value );
197         //getNodePoint( BT->rightChild, value );
198         
199         bitNode *tmp = getNodePoint( BT->leftChild, value );
200         if(!tmp)
201         {
202             getNodePoint( BT->rightChild, value );
203         }
204         //follow statement can't be added to the code
205         //return tmp;
206     }
207 }
208 
209 void _Binary_Tree::PreOrderTraverse(binTree BT)   
210 {
211     //if( emptyBinaryTree() )
212 //    {
213         //throw "Empty binary tree!Error in void _Binary_Tree::PreOrderTraverse(binTree BT) !\n";
214         //return;
215     //}
216     if (BT)  
217     {
218              cout << BT->data << " ";
219             PreOrderTraverse(BT->leftChild);
220             PreOrderTraverse(BT->rightChild);
221     }
222 }
223 
224 void _Binary_Tree::InOrderTraverse(binTree BT)   
225 {
226     if (BT)   
227     {
228             InOrderTraverse(BT->leftChild);
229              cout << BT->data << " ";
230             InOrderTraverse(BT->rightChild);
231     }
232     //return 0;
233 }
234 
235 void _Binary_Tree::PostOrderTraverse( binTree BT )
236 {
237     if (BT)  
238     {
239             PostOrderTraverse(BT->leftChild);
240             PostOrderTraverse(BT->rightChild);
241             cout << BT->data << " ";
242     }
243 }
244 
245 void _Binary_Tree::destroy( binTree BT )
246 {
247     if(BT)
248     {
249         destroy( BT->leftChild );
250         destroy( BT->rightChild );
251         delete BT;
252         BT = NULL;
253     }
254 }
255 
256 void _Binary_Tree::level( binTree BT, int number )
257 {
258 
259     if(BT)
260     {
261         level( BT->leftChild, number + 1 );
262         ///number +=3;
263         //cout << number << endl;
264         cout << BT->data << " level: " << number << endl;
265         
266         level( BT->rightChild, number + 1 );
267         //number -=2;
268     }
269     //number --;
270 }
271 
272 int _Binary_Tree::height( binTree BT )
273 {
274     if(!BT)
275     {
276         return 0;
277     }
278     else
279     {
280         int i = height( BT->leftChild );
281         int j = height( BT->rightChild );
282         return i < j ? j + 1 : i + 1;
283 
284     }
285 }
286 
287 int _Binary_Tree::numberOfBTreeNode( binTree BT )
288 {
289     if(!BT)
290         return 0;
291     else
292     {
293         return numberOfBTreeNode( BT->leftChild ) + numberOfBTreeNode( BT->rightChild ) + 1;
294     }
295 }
296 
297 int _Binary_Tree::numberOfBTreeLeafNode( binTree BT, int &number )
298 {
299     if(!BT)
300     {
301         return 0;
302     }
303     else
304     {
305         if( !BT->leftChild && !BT->rightChild )
306             //number += 1;
307             number ++;
308             //return 1;
309         else
310         {
311             numberOfBTreeLeafNode( BT->leftChild, number );
312             numberOfBTreeLeafNode( BT->rightChild, number );
313         }
314         return number;    
315     }
316 }
317 
318 void _Binary_Tree::numberOfNodeDegreeTwo( binTree BT, int &number )
319 {
320     if(!BT)
321     {
322         return;
323     }
324     else
325     {
326         if( BT->leftChild && BT->rightChild )
327             //number += 1;
328             number += 1;
329             //return 1;
330         //else
331         //{
332             numberOfNodeDegreeTwo( BT->leftChild, number );
333             numberOfNodeDegreeTwo( BT->rightChild, number );
334             //return numberOfNodeDegreeTwo( BT->leftChild, number ) + numberOfNodeDegreeTwo( BT->rightChild, number );
335         //}
336         //return number;    
337     }
338 }
339 
340 /*
341 void _Binary_Tree::family( binTree BT, elementType1 number )
342 {
343     if(!BT)
344     {
345         return;
346     }
347     if( BT->leftChild->data == number || BT->rightChild->data == number )
348     {
349         cout << "parent ---- " << BT->data << endl;
350         if( BT->leftChild->data == number && BT->rightChild )
351         {
352             cout << "rights sibling ---- " << BT->rightChild->data << endl;
353         }
354         if( BT->leftChild && BT->rightChild->data == number )
355         {
356             cout << "left sibling ---- " << BT->leftChild->data << endl;
357         }
358     }
359     if( BT->data == number && ( BT->leftChild || BT->rightChild ) )
360     {
361         cout << ( BT->leftChild ? "left child ---- " : true ) << endl;
362         cout << ( BT->rightChild ? "right child ---- " : true ) << endl;
363     }
364     family( BT->leftChild, number );
365     family( BT->rightChild, number );
366     //if( BT->data == number && BT-)
367     //if( BT->leftChild->data == number &&)
368 }
369 */
370 
371 //bool _Binary_Tree::getParent( binTree BT, elementType number, bool flag )
372 void _Binary_Tree::getParent( binTree BT, elementType value, bool &flag )
373 {
374     if(!BT)
375     {
376         //return false;
377         return;
378     }
379     if( ( BT->leftChild && BT->leftChild->data == value ) || ( BT->rightChild ) && ( BT->rightChild->data == value ) )
380     {
381         flag = true;
382         cout << value << " Parent ---- " << BT->data << endl;
383         return;
384         //return true;
385     }
386     /*
387     if( BT && BT->rightChild->data == number )
388     {
389         cout << "parent ---- " << BT->data << endl;
390         return true;
391     }
392     */
393     getParent( BT->leftChild, value, flag );
394     getParent( BT->rightChild, value, flag );
395 }    
396 
397 binTree _Binary_Tree::getParent( binTree BT, elementType value )
398 {
399     
400     if( !_exit( BT, value ) )
401     {
402         cerr << value << " is not in the binary tree!" << endl;
403         cerr << "Error in binTree _Binary_Tree::getParent( binTree BT, elementType value )!" << endl;
404         return NULL;
405     }
406     
407     if(!BT)
408     {
409         return NULL;
410     }
411     if( BT->data == value )
412     {
413         return BT;
414     }
415     if( ( BT->leftChild && BT->leftChild->data == value ) || ( BT->rightChild && BT->rightChild->data == value ) )//|| BT->rightChild->data == value )
416     {
417         return BT;
418     }
419     bitNode *tmp = getParent( BT->leftChild, value );
420     if(!tmp)
421     {
422         getParent( BT->rightChild, value );
423     }
424 }
425 
426 void _Binary_Tree::getSibling( binTree BT, elementType value, bool &flag )
427 {
428     if(!BT)
429     {
430         cout << value << " No LeftSibling!" << endl << value << " No RightSibling!" << endl;
431         return;
432     }
433     if( !flag &&  !BT->leftChild )//|| !BT->rightChild )//write as "if(!BT)" would result error
434     {
435         
436         if( BT->rightChild )
437         {
438             getSibling( BT->rightChild, value, flag );
439             //return;
440         }
441         else
442         {
443             //cout << value << " No LeftSibling!" << endl;
444             return;
445         }
446         return;//why would deleting the statement cause error!
447     }
448 
449     if( !flag && !BT->rightChild )
450     {
451 
452         if( BT->leftChild )
453         {
454             getSibling( BT->leftChild, value, flag );
455             
456         }
457         else
458         {
459             //cout << value << "No LeftSibling!" << endl;
460             //cout << value << " No RightSibling!" << endl;
461             return;
462             
463         }    
464         return;//why would deleting the statement cause error!
465     }
466     if( BT->rightChild->data == value )
467     {
468         if( BT->leftChild )
469         {
470             flag = true;
471             cout << value << " LeftSibling ---- " << BT->leftChild->data << endl;
472             return;
473         }
474         else if( !BT->leftChild )
475         {
476             cout << value << " No LeftSibling!" << endl;
477             return;
478         }
479         
480     }
481     if( BT->leftChild->data == value )
482     {
483         if( BT->rightChild )
484         {
485             flag = true;
486             cout << value << " RightSibling ---- " << BT->rightChild->data << endl;
487             return;
488         }
489         else if( !BT->rightChild )
490         {
491             cout << value << " No RightSibling!" << endl;
492             return;
493         }
494     }
495     getSibling( BT->leftChild, value, flag );
496     if( !flag && BT->rightChild )
497         getSibling( BT->rightChild, value, flag );
498 }
499 
500 void _Binary_Tree::getSibling( binTree BT, elementType value )
501 {
502     bitNode *parent = getParent( BT, value );
503     
504     if( BT->data == value )
505     {
506         cout << value << " is the root node,neither left sibling also useless right sibling!" << endl;
507         return;
508     }
509     if( !_exit( BT, value ) )
510     {
511         cout << value << " is not in the binary-tree!" << endl;
512         cerr << "Error in void _Binary_Tree::getSibling( binTree BT, elementType value )!" << endl;
513         return;
514     }
515     if( parent->leftChild && parent->leftChild->data == value )
516     {
517         if( parent->rightChild )
518         {
519             cout << value << " RightSibling ---- " << parent->rightChild->data << endl;
520             return;
521         }
522         else
523         {
524             cout << value << " No RightSibling!" << endl;
525             return;
526         }
527     }
528     if( parent->rightChild && parent->rightChild->data == value )
529     {
530         if( parent->leftChild )
531         {
532             cout << value << " LeftSibling ---- " << parent->leftChild->data << endl;
533             return;
534         }
535         else
536         {
537             cout << value << " No LeftSibling!" << endl;
538             return;
539         }
540     }
541 }
542 
543 void _Binary_Tree::getChild( binTree BT, elementType value, bool &flag )//It costed me several minutes to   
544 {                                                                        //write and almost one hour to 
545     /*                                                                    //perfect the function
546     if( BT->leftChild )
547     {
548         getChild( BT->leftChild, value, flag );
549     }
550     if( BT->rightChild )
551     {
552         getChild( BT->rightChild, value, flag );
553     }
554     */
555     
556     /*
557     if( !BT->leftChild )//|| !BT->rightChild )
558     {
559         if(flag)
560         {    
561             cout << "No LeftChild! " << endl;
562             flag = true;
563             return;
564         }
565         else
566         {
567             cout << "No LeftChild! " << endl;
568             flag = false;
569         }
570         //return;
571     }
572     if( !BT->rightChild )
573     {
574         if(flag)
575         {
576             cout << "No RightChild! " << endl;
577             flag = true;
578             return;
579         }
580         else
581         {
582             cout << "No RightChild! " << endl;
583             flag = false;
584         }
585     }
586     */
587     //if(!BT)
588 //    {
589     //    return;
590 //    }
591     if( !_exit( BT, value ) )
592     {
593         cerr << value << " is not in the binary tree!\nError in void _Binary_Tree::getChild( binTree BT, elementType value, bool &flag )" << endl;
594         return;
595     }
596     if( BT->data == value )//at first I neglected this detail that resulted wrong judgement at root-node 
597     {
598         if( BT->leftChild )
599         {
600             flag = true;
601             cout << value << " LeftChild ---- " << BT->leftChild->data << endl;
602             
603         }
604         else
605         {
606             cout << "No LeftChild!" << endl;
607 
608         }
609         if( BT->rightChild )
610         {
611             flag = true;
612             cout << value << " RightChild ---- " << BT->rightChild->data << endl;
613             return;
614         }
615         else
616         {
617             cout << "No RightChild! " << endl;
618             return;
619         }
620     }
621     if( !BT->leftChild )
622     {
623         if( BT->rightChild )
624         {
625             getChild( BT->rightChild, value, flag );
626         }
627         return;
628         /*
629         if(flag)
630         {
631             return;
632         }
633         else
634         {
635             flag = false;
636             return;
637         }*/
638     }
639     if( !BT->rightChild )
640     {
641         if( BT->leftChild )
642         {
643             getChild( BT->leftChild, value, flag );
644<