C++ 使用数组建立二叉树 层序数组(方法一)

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;
}

在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值