C++ 使用數組建立二叉樹 層序數組(方法一)
另外一種方法:見 C++ 使用數組建立二叉樹 層序數組(方法二)
1、輸入數組要求
數組是按照層序輸入的,當該結點爲空時,用‘#’代替空的位置。
如:
圖(a)中的二叉樹的 輸入數組爲:
int data[] = { 1, 2, 3, 4, 5, ‘#’, 6, ‘#’, ‘#’, 7, 8 };
圖(b)中的二叉樹的 輸入數組爲:
int data[] = { 1, 2, 3, 4, 5, ‘#’, 6, 7 };
2、數組創建二叉樹的子函數
本文的核心代碼,數組創建二叉樹子函數 CreateBiTree() 的原創爲 該博文
他的二叉樹結點結構的定義與筆者不同,故數組創建二叉樹子函數 也需要做一點改動:
筆者二叉樹的結點數據結構如下:
typedef struct BiTNode//二叉樹的結點數據結構
{
int data;
BiTNode *lchild, *rchild;
};
原博文如下:
//二叉樹的節點定義
class TreeNode {
public:
int val;
TreeNode *left, *right;
TreeNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
};
原博文的 數組創建二叉樹的子函數 如下:
//從數組的某個位置的元素開始生成樹
TreeNode* createTree(vector<int> list, int start){
if (list[start] == '#') {
return NULL;
}
TreeNode* root = new TreeNode(list[start]);
int lnode = 2*start + 1;
int rnode = 2*start + 2;
if ( lnode > list.size() -1) {
root -> left = NULL;
}else{
root -> left = createTree(list, lnode);
}
if (rnode > list.size() -1) {
root -> right =NULL;
}else{
root -> right = createTree(list, rnode);
}
return root;
}
筆者對其進行了修改,主要在生成新結點時,有所不同:
原博文:
TreeNode* root = new TreeNode(list[start]);
本文更改爲:
BiTNode* root = new BiTNode;//新建一個根結點
//給根結點 root 的 成員變量 root、lchild、rchild 賦初值
root->data = a[start];
root->lchild = NULL;
root->rchild = NULL;
並且還需要對新建的根結點指針賦值,因爲原博文在定義二叉樹結點類時,已經對其進行了初始化:
TreeNode(int val) {
this->val = val;
this->left = this->right = NULL;
}
筆者的 數組創建二叉樹的子函數 :
BiTNode *CreateBiTree(int *a, int n, int start)//按層序輸入,結點爲空時,輸入'#'
{
if (a[start] == '#')
return NULL;
BiTNode* root = new BiTNode;//新建一個根結點
//給根結點 root 的 成員變量 root、lchild、rchild 賦初值
root->data = a[start];
root->lchild = NULL;
root->rchild = NULL;
int lnode = 2 * start + 1;
int rnode = 2 * start + 2;
if (lnode > n - 1)
root->lchild = NULL;
else
root->lchild = CreateBiTree(a, n, lnode);
if (rnode > n - 1)
root->rchild = NULL;
else
root->rchild = CreateBiTree(a, n, rnode);
return root;
}
3、測試代碼:
/***************************************************************************
* @author: 東籬_
* @date: 2018.4.8
* @remark: this code is for binary tree with array
* @note: The numbers in the input array is arranged by level order
****************************************************************************/
#include <iostream>
#include <queue>
using namespace std;
typedef struct BiTNode//二叉樹的結點數據結構
{
int data;
BiTNode *lchild, *rchild;
};
BiTNode *CreateBiTree(int *a, int n, int start)//按層序輸入,結點爲空時,輸入'#'
{
if (a[start] == '#')return NULL;
BiTNode* root = new BiTNode;//新建一個根結點
root->data = a[start];//給根結點 root 的 成員變量 data、lchild、rchild 賦初值
root->lchild = NULL;
root->rchild = NULL;
int lnode = 2 * start + 1;
int rnode = 2 * start + 2;
if (lnode > n - 1) root->lchild = NULL;
else root->lchild = CreateBiTree(a, n, lnode);
if (rnode > n - 1) root->rchild = NULL;
else root->rchild = CreateBiTree(a, n, rnode);
return root;
}
//先序遍歷函數
void PreOrderTraverse(BiTNode *T)
{
if (T){
cout << T->data << " ";
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//層序遍歷--隊列
void LevelOrderTraverse(BiTNode *T)
{
queue<BiTNode *> Q;
if (T == NULL) return;
Q.push(T);//入隊根指針
while (!Q.empty()){
BiTNode *cur = Q.front();
Q.pop();
cout << cur->data << " ";
if (cur->lchild) Q.push(cur->lchild);
if (cur->rchild) Q.push(cur->rchild);
}
cout << endl;
}
void test1(){
BiTNode *t;
int data[] = { 1, 2, 3, 4, 5, '#', 6, '#', '#', 7, 8 };
//前序遍歷:1 2 4 5 7 8 3 6
t = CreateBiTree(data, sizeof(data) / sizeof(data[0]), 0);
printf("The pre order is : ");
PreOrderTraverse(t);
cout << endl;
printf("The level order is : ");
LevelOrderTraverse(t);
}
void test2(){
BiTNode *t;
int data[] = { 1, 2, 3, 4, 5, '#', 6, 7 };
//前序遍歷:1 2 4 7 5 3 6
t = CreateBiTree(data, sizeof(data) / sizeof(data[0]), 0);
printf("The pre order is : ");
PreOrderTraverse(t);
cout << endl;
printf("The level order is : ");
LevelOrderTraverse(t);
}
int main()
{
test1();
test2();
return 0;
}