04-树6 Complete Binary Search Tree (30分) (2020浙大Mooc数据结构配套题)

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

题目解析

  题目的目的是输入一串数字,然后构建完全二叉搜索树,搜索树的特点是左子树比根小,右子树比根大,完全二叉树的特点是层次遍历是没有中断点的,一行一行满满当当,可以用数组实现比较不会太浪费内存,最后也是要按照层次遍历输出结果,数组实现比较容易.

解题思路:

GtHeun.png

*完全二叉树*

1. 中序遍历法

  1. 输入长度n,然后输入n个数都存到数列里面
  2. 想办法给函数排个序
  3. 建立一个数组用来存放树的结点
  4. 通过中序遍历(中序遍历在搜索树中是从小到大的遍历顺序)挨个在树里插入

2. 计算根结点(函数法)

  1. 输入长度n,然后输入n个数都存到数列里面
  2. 想办法给函数排个序
  3. 建立一个数组用来存放树的结点
  4. 设计函数来计算根的位置(根据完全二叉树的定义),然后再挨个在树里插入
    GtHsvd.png
*计算根函数分析*
  两种方法时间复杂度都是一样的,需要遍历一次,不算排序的话复杂度是O(n),我也不清楚排序原理,看不懂源码...两种都需要递归所以空间复杂度也差不多,区别在于中序遍历比较容易懂,一般的思路就是中序遍历了,用函数计算虽然加了点难度,各种分析,感觉像是开拓了一下思维,提供了一种思路,用函数算法优化编程算法,要是有空可以两种方案都写写,写完帮助还是挺大的.

代码示例(函数法):

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 1001

/* 定义全局变量比较容易操作,用指针传递不好看,动态数组节约空间,但是写题目没必要 */
int Sequence[MAXSIZE],Tree[MAXSIZE];

void Sort(int *Sequence,int n);
int compare(const void*a,const void*b);
int GetRoot(int n);
void BuildBST(int SLeft,int SRight,int TRoot); 

int main(){
	int i,n,Root;
	scanf("%d",&n);//输入长度,一开始先写了,老套路了
	Sort(Sequence,n);
	BuildBST( 0, n - 1, 0);
	for( i = 0; i < n; i++){
		if(i == 0){
			printf("%d",Tree[i]);
		}else{
			printf(" %d",Tree[i]);
		}
	}
}

int compare(const void*a,const void*b)
{
	return *(int *)a - *(int *)b;
} 

void Sort(int *Sequence,int n)
{
	int i = 0;
	for( ; i < n; i++){
		scanf("%d",&Sequence[i]);
	}
	qsort(Sequence,n,sizeof(int),compare);
}

void BuildBST(int SLeft,int SRight,int TRoot)
{
	int n,Root,LeftRoot,RightRoot;
	n = SRight - SLeft + 1;//计算数列长度 
	if(n <= 0) return;//长度为0就返回
	Root = GetRoot(n);//找到数列里面要操作的根,然后把下标返回,输入进树里 
	Tree[TRoot] = Sequence[SLeft + Root - 1];
	//事实上面这里用函数找根插入然后递归比起中序遍历的算法要难理解 
	
	LeftRoot =  TRoot * 2 + 1;//+1是因为下标从0开始 
	RightRoot = TRoot * 2 + 2;
	BuildBST( SLeft, SLeft + Root - 2, LeftRoot);
	BuildBST( SLeft + Root, SRight, RightRoot);
}

int GetRoot(int n)
{
	int h,x,Root;
	h = log(n + 1) / log(2);//log()是以e为底取对数,简单的换底公式 
	x = n + 1 - pow( 2, h);
	x = ( x >= 0 && x <= pow(2,h-1) ) ? x : pow(2,h-1);
	Root = pow(2,h-1) + x;
	return Root;
}

代码示例(中序遍历法):

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1001

/* Sort是不变的,BuildBST变了*/
int compare(const void *a, const void *b);
void BuildBST(int *Tree, int *Sequence, int index, int *i);
void Sort(int *Sequence,int n);

int Sequence[MAXSIZE],Tree[MAXSIZE];

int main()
{
    int n, index, i;
    scanf("%d", &n);
    Tree[0] = n;//用下标0来存长度 
    index = 1, i = 0;
    Sort(Sequence,n);
    BuildBST(Tree, Sequence, index, &i); 
    for (i = 1; i <= Tree[0]; ++i) {//注意从1开始
        if(i == 1){
			printf("%d",Tree[i]);
		}else{
			printf(" %d",Tree[i]);
		}
    }

    return 0;
}


int compare(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

void Sort(int *Sequence,int n)
{
	int i = 0;
	for( ; i < n; i++){
		scanf("%d",&Sequence[i]);
	}
	qsort(Sequence,n,sizeof(int),compare);
}

/* 经典中序遍历 */ 
void BuildBST(int *Tree, int *Sequence, int index, int *i)
{
    // 越界或者index超过最大值返回 
    if (index < 1 || index > Tree[0] || (*i) == Tree[0]) return;
    else {  
        BuildBST(Tree, Sequence, index * 2, i);
        Tree[index] = Sequence[(*i)++];
        BuildBST(Tree, Sequence, index * 2 + 1, i);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值