7-7 Complete Binary Search Tree(30 point(s))---MOOC浙大数据结构

7-7 Complete Binary Search Tree(30 point(s))

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:


  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

思路:

二叉搜索树一般用链表,完全二叉树其中有优先队列,用数组实现。

这个题目就是完全二叉搜索树,既然不浪费空间,那用数组好些。效率的话,插入和删除数组会差些,但题目并不涉及。

  1. 输入
  2. 递归计算左右两个子树的节点个数,同时判断出根节点,根节点放在新数组中,
    这里的数组下标必须用*2和*2+1进行计算,因为输出要求是层序遍历,利用了完全二叉树的特性。
  3. 每次走完一条路径时,必须进行/2的下标操作(看完姥姥的视频发现是多余的)
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

#define MAXNUM 2000
int arr[MAXNUM]={0};
int tree[MAXNUM]={0};

void fun1(int leftP,int N,int a,int tree_index)
{

	float level=0;
	if (N==0)
		return;
	if(N==1)
	{
		if(a==0)
			tree_index=tree_index*2;
		else
			if(a==1)
				tree_index=tree_index*2+1;

		tree[tree_index]=arr[leftP];	
		tree_index/=2;
		return;
	}
	while((pow(2,level)-1)<N)//caculate the level of tree
		level++;
	int left_node=N-(pow(2,level-1)-1);//last level nodes number
	int rightnode=(pow(2,level-1)-1-1)/2;//right node number
	int leftnode=(pow(2,level-1)-1-1)/2;//left node number
	if (left_node>pow(2,level-1)/2)
	{
		rightnode+=left_node-pow(2,level-1)/2;//
		leftnode+=pow(2,level-1)/2;
	}else
		leftnode+=left_node;
	if(a==0)
		tree_index=tree_index*2;
	else
		if(a==1)
			tree_index=tree_index*2+1;

	tree[tree_index]=arr[leftP+leftnode];
	fun1(leftP,leftnode,0,tree_index);//left child
	fun1(leftP+leftnode+1,rightnode,1,tree_index);//right child
	tree_index/=2;
}

int main()
{
	int N;
	cin>>N;
	for (int i=1;i<=N;i++)
		cin>>arr[i];
	sort(arr+1,arr+N+1);
	fun1(1,N,2,1);
	cout<<tree[1];
	for (int i=2;i<=N;i++)
		cout<<" "<<tree[i];
	cout<<endl;

	return 0;
}

最后发现一个问题,完全可以把n=1的情况去掉,。

#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

#define MAXNUM 2000
int arr[MAXNUM]={0};
int tree[MAXNUM]={0};

void fun1(int leftP,int N,int a,int tree_index)
{

	float level=0;
	if (N==0)
		return;
	/*去掉了n=1的情况*/
	while((pow(2,level)-1)<N)//caculate the level of tree
		level++;
	int left_node=N-(pow(2,level-1)-1);//last level nodes number
	int rightnode=(pow(2,level-1)-1-1)/2;//right node number
	int leftnode=(pow(2,level-1)-1-1)/2;//left node number
	if (left_node>pow(2,level-1)/2)
	{
		rightnode+=left_node-pow(2,level-1)/2;//
		leftnode+=pow(2,level-1)/2;
	}else
		leftnode+=left_node;
	if(a==0)
		tree_index=tree_index*2;
	else
		if(a==1)
			tree_index=tree_index*2+1;

	tree[tree_index]=arr[leftP+leftnode];
	fun1(leftP,leftnode,0,tree_index);//left child
	fun1(leftP+leftnode+1,rightnode,1,tree_index);//right child
	tree_index/=2;
}

int main()
{
	int N;
	cin>>N;
	for (int i=1;i<=N;i++)
		cin>>arr[i];
	sort(arr+1,arr+N+1);
	fun1(1,N,2,1);
	cout<<tree[1];
	for (int i=2;i<=N;i++)
		cout<<" "<<tree[i];
	cout<<endl;

	return 0;
}

包括

1086 Tree Traversals Again (25)(25 point(s))---MOOC浙大数据结构

这个题目也是。

也就是说,叶节点的处理跟其他节点的处理一样


最后呢。。。

发现自己考虑多了,白调试了这么久。。。

递归的过程中,一定要注意保持栈中的局部变量,这样递归回来还能继续用,所以左右节点的计算,新建一个变量即可,没有必要再考虑去程和回城的问题了。


#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

#define MAXNUM 2000
int arr[MAXNUM]={0};
int tree[MAXNUM]={0};

void fun1(int leftP,int N,int tree_index)
{

	float level=0;
	int tree_indexL=0;
	int tree_indexR=0;
	if (N==0)
		return;
	while((pow(2,level)-1)<N)//caculate the level of tree
		level++;
	int left_node=N-(pow(2,level-1)-1);//last level nodes number
	int rightnode=(pow(2,level-1)-1-1)/2;//right node number
	int leftnode=(pow(2,level-1)-1-1)/2;//left node number
	if (left_node>pow(2,level-1)/2)
	{
		rightnode+=left_node-pow(2,level-1)/2;//
		leftnode+=pow(2,level-1)/2;
	}else
		leftnode+=left_node;

		tree_indexL=tree_index*2;
			tree_indexR=tree_index*2+1;

	tree[tree_index]=arr[leftP+leftnode];
	fun1(leftP,leftnode,tree_indexL);//left child
	fun1(leftP+leftnode+1,rightnode,tree_indexR);//right child
}

int main()
{
	int N;
	cin>>N;
	for (int i=1;i<=N;i++)
		cin>>arr[i];
	sort(arr+1,arr+N+1);
	fun1(1,N,1);
	cout<<tree[1];
	for (int i=2;i<=N;i++)
		cout<<" "<<tree[i];
	cout<<endl;

	return 0;
}



一往无前虎山行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值