版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29567701/article/details/79857137
用数组建立二叉树
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 的 成员变量 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;
}
//先序遍历函数
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;
}
int main()
{
BiTNode *t;
// int data[] = { 1, 2, 3, 4, 5, '#', 6, '#', '#', 7, 8 };
//前序遍历:1 2 4 5 7 8 3 6
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);
return 0;
}