数组转换为完全二叉树的二叉链表表示

建树有两种方式,深度优先和广度优先,代码如下

#include <iostream>
#include <vector>
#include <stack>
#include <deque>
using namespace std;

struct BinaryTree
{
	int data;
	BinaryTree* left = nullptr;
	BinaryTree* right = nullptr;
	BinaryTree(int d) :data(d) {}
};

BinaryTree* buildDFS(vector<int>& input)
{
	struct StackNode
	{
		BinaryTree* cur;
		size_t arrary_index;
		size_t process_flag = 0;
		StackNode(BinaryTree* c, size_t a) :cur(c), arrary_index(a) {}
	};

	stack<StackNode> work_stack;
	BinaryTree* root = new BinaryTree(input[0]);
	work_stack.push(StackNode(root, 1));
	while (work_stack.empty() == false)
	{
		if (work_stack.top().process_flag == 0)
		{
			size_t temp = 0;
			if ((temp = 2 * work_stack.top().arrary_index) <= input.size())
			{
				work_stack.top().cur->left = new BinaryTree(input[temp-1]);
				work_stack.push(StackNode(work_stack.top().cur->left, temp));
			}
			else
			{
				work_stack.top().process_flag = 2;
			}
		}
		else if (work_stack.top().process_flag == 1)
		{
			size_t temp = 0;
			if ((temp = 2 * work_stack.top().arrary_index + 1) <= input.size())
			{
				work_stack.top().cur->right = new BinaryTree(input[temp - 1]);
				work_stack.push(StackNode(work_stack.top().cur->right, temp));
			}
			else
			{
				work_stack.top().process_flag = 2;
			}
		}
		else
		{
			work_stack.pop();
			if (work_stack.empty() == false)
			{
				if (work_stack.top().process_flag == 0)
				{
					work_stack.top().process_flag = 1;
				}
				else
				{
					work_stack.top().process_flag = 2;
				}
			}
		}
	}

	return root;
	 
}

bool isCompeleteBinaryTree(BinaryTree* root)
{
	enum Flag {NO_EMPTY, EMPTY} flag = NO_EMPTY;
	deque<BinaryTree*> work_queue;
	work_queue.push_back(root);
	while (work_queue.empty() == false)
	{
		BinaryTree* cur = work_queue.front();
		work_queue.pop_front();
		if (flag == NO_EMPTY)
		{
			if (cur->left != nullptr)
			{
				work_queue.push_back(cur->left);
				if (cur->right != nullptr)
				{
					work_queue.push_back(cur->right);
				}
				else
				{
					flag = EMPTY;
				}
			}
			else
			{
				if (cur->right != nullptr)
				{
					return false;
				}
				else
				{
					flag = EMPTY;
				}
			}
		}
		else if (flag == EMPTY)
		{
			if (cur->left != nullptr)
			{
				return false;
			}
			else if (cur->right != nullptr)
			{
				return false;
			}
		}
	}
	return true;
}

BinaryTree* buildBFS(vector<int>& input)
{
	struct DequeNode
	{
		BinaryTree* cur;
		size_t arrary_index;
		DequeNode(BinaryTree* c, size_t a):cur(c), arrary_index(a) {}
    };

	deque<DequeNode> work_queue;
	BinaryTree* root = new BinaryTree(input[0]);
	work_queue.push_back(DequeNode(root, 1));
	while (work_queue.empty() == false)
	{
		size_t temp = 0;
		DequeNode t = work_queue.front();
		work_queue.pop_front();
		if ((temp = 2 * t.arrary_index) <= input.size())
		{
			t.cur->left = new BinaryTree(input[temp - 1]);
			work_queue.push_back(DequeNode(t.cur->left, temp));
			if ((++temp) <= input.size())
			{
				t.cur->right = new BinaryTree(input[temp - 1]);
				work_queue.push_back(DequeNode(t.cur->right, temp));
			}
		}
	}
	return root;
}
int main()
{
	vector<int> input{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
	BinaryTree* rootDFS = buildDFS(input);
	BinaryTree* rootBFS = buildBFS(input);
	if (isCompeleteBinaryTree(rootDFS))
	{
		cout << "DFS建树成功!" << endl;
	}
	else
	{
		cout << "DFS建树失败!" << endl;
	}

	if (isCompeleteBinaryTree(rootBFS))
	{
		cout << "BFS建树成功!" << endl;
	}
	else
	{
		cout << "BFS建树失败!" << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值