1. 程式人生 > >Complete Binary Search Tree

Complete Binary Search Tree

recursive .cn 慕課 end ati choose mat term next

本博客的代碼的思想和圖片參考:好大學慕課浙江大學陳越老師、何欽銘老師的《數據結構》

Complete Binary Search Tree

1 Question


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
時間限制:400ms
內存限制:64MB
代碼長度限制:16kB
判題程序:系統默認
作者:陳越
單位:浙江大學

2 Question Analysis


2.1 How to Choose Data Structure


There are two data structures to choose, array or link list. How to choose them. Let us to analysis the question requests.
First the question requests search the array or link list and put elements in it array and link list can do it easily.
Second the question need to level search binary tree. Array just need to print element in order but link list need a queue to implement this function.
Third we use the complete tree, we just need to store elements in order in array, don’t need to use link list.
In a word, we choose the array as the data structures


2.2 Algorithm Thoughts


2.2.1 Analysis the Input


We know from the question, input is the a N distinct non-negative integer keys. For complete binary tree, if we know N, we can determine the unique binary tree we want. So the result work it how to put elements into the binary tree.


2.2.2 Find Root Element


We can reference complete tree’s attributes to query how many left nodes(L) and how many right nodes(N) if given total nodes. At this moment we need to sort elements from smaller to greater in given array. So we can get the root element’s index is L +start index in given input data array. Then we recursive use this method to complete sub left tree and sub right tree until all elements be put into the complete binary search tree.
技術分享

2.2.3 How to Calculate the quantity of sub tress 技術分享





The Code is followed:



技術分享
  1 /*
  2  * completeBinarySearchTree.c
  3  *
  4  *  Created on: 2017年5月10日
  5  *      Author: ygh
  6  */
  7 
  8 /*
  9  * Algorithm thoughts:
 10  * 1.We use the array to store the complete binary search tree
 11  * 2.We will sort the data ascending given in form of a integer array
 12  * 3.We can get a binary tree sub left nodes and sub right nodes by calculating
 13  * 4.The root element index in given data array is startIndex + left nodes
 14  * 5.Call same method for sub left tree and sub right tree recursive
 15  */
 16 
 17 #include <stdio.h>
 18 #include <stdlib.h>
 19 #include <math.h>
 20 
 21 /*
 22  * Give a tree with n nodes,calculate how many left nodes it has
 23  * @param n The total nodes the tree has.
 24  * @return A integer number of the left tree nodes
 25  */
 26 int calculateLeftTreeLength(int n) {
 27     int h, x, l;
 28     h = log2(n + 1);
 29     x = n + 1 - pow(2, h);
 30     if (x > pow(2, h - 1)) {
 31         x = pow(2, h - 1);
 32     }
 33     l = pow(2, h - 1) - 1 + x;
 34     return l;
 35 }
 36 
 37 /*
 38  * A function to put the input array elements to complete
 39  * the initialized condition is (0,n-1,0,tree[],array[])
 40  * binary search tree
 41  * @param aLeft The complete binary search tree data start index in input array
 42  * @param aLeft The complete binary search tree data end index in input array
 43  * @param tRoot The index of the complete binary search tree root
 44  * @param tree A array to store complete binary search tree.
 45  * @param array A array store the input data and has been sorted ascending
 46  */
 47 void solve(int aLeft, int aRight, int tRoot, int *tree, int *array) {
 48     /*
 49      * n:the total nodes of the tree
 50      * letfLength:the nodes of the sub left nodes
 51      * leftRoot:the index of the sub left root in the tree
 52      * rightRoot:the index of the sub right root in the tree
 53      */
 54     int n, leftLength, leftRoot, rightRoot;
 55     /*
 56      * Get the all nodes of the tree
 57      */
 58     n = aRight - aLeft + 1;
 59     if(n==0){
 60         return;
 61     }
 62     leftLength = calculateLeftTreeLength(n);
 63     tree[tRoot] = array[aLeft + leftLength];
 64     leftRoot = tRoot * 2 + 1;
 65     rightRoot = leftRoot + 1;
 66     solve(aLeft, aLeft + leftLength - 1, leftRoot, tree, array);
 67     solve(aLeft + leftLength + 1, aRight, rightRoot, tree, array);
 68 }
 69 
 70 void getInputData(int length, int *array) {
 71     int data, i;
 72     for (i = 0; i < length; i++) {
 73         scanf("%d", &data);
 74         array[i] = data;
 75     }
 76 }
 77 
 78 void levelSearch(int *tree, int length) {
 79     int i;
 80     for (i = 0; i < length; i++) {
 81         if (i == length - 1) {
 82             printf("%d", tree[i]);
 83         } else {
 84             printf("%d ", tree[i]);
 85         }
 86     }
 87 }
 88 
 89 /*
 90  * A method to compare tow integer number
 91  */
 92 int compare(const void *a, const void *b) {
 93     return *(int*) a - *(int*) b;
 94 }
 95 
 96 int main() {
 97     int maxLength = 1000;
 98     int array[maxLength];
 99     int tree[maxLength];
100     int n;
101     scanf("%d", &n);
102     getInputData(n, array);
103     qsort(array, n, sizeof(int), compare);
104     solve(0, n - 1, 0, tree, array);
105     levelSearch(tree, n);
106     return 0;
107 }
Complete Binary Search Tree

PTA test result:

技術分享

Complete Binary Search Tree