1. 程式人生 > >c語言 04-樹6 Complete Binary Search Tree

c語言 04-樹6 Complete Binary Search Tree

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

Show me the code:

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#ifndef NULL
#define NULL 0
#endif // NULL
#define MAXSIZE 10
#define Null -1

typedef int ElementType;
typedef struct TNode *BinTree;
struct TNode
{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};
typedef BinTree TPosition;

struct QNode
{
    TPosition Data;
    struct QNode *Next;
};
typedef struct QNode *Position;
typedef Position Queque;


Queque CreateQueue();
int add(Queque q, TPosition e);
TPosition Delete(Queque q);
int IsEmpty(Queque q);

BinTree CreateTNode();
void BubbleSort(int*,int);
BinTree BuildCompleteTree(int Data[],int N);
int TreeHeight(int n);
void LevelOrderTraversal(BinTree root);
int main()
{
    int N;
    scanf("%d",&N);
    if(N)
    {
        int i, Data[N];
        BinTree T;
        for(i=0;i<N;i++)
            scanf("%d",Data+i);
        BubbleSort(Data,N);
        T = BuildCompleteTree(Data,N);
        LevelOrderTraversal(T);
    }
    return 0;
}

/*佇列抽象資料結構*/
Queque CreateQueue()
{
    Queque q;
    q = (Queque)malloc(sizeof(struct QNode));
    q->Data = NULL;
    q->Next = NULL;
    return q;
}

int add(Queque q, TPosition e)
{
    Position p,tmp;
    if(!q)
        q = CreateQueue();
    tmp = q;
    while(tmp->Next)
    tmp = tmp->Next;
    p = (Position)malloc(sizeof(struct QNode));
    p->Data = e;
    p->Next = NULL;
    tmp->Next = p;
    return 0;
}

TPosition Delete(Queque q)
{
    TPosition First;
    Position tmp;
    if(!q || q->Next == NULL)
        return NULL;
    else
    {
        tmp = q->Next;
        q->Next = tmp->Next;
        First = tmp->Data;
        free(tmp);
        return First;
    }
}
int IsEmpty(Queque q)
{
    return(q->Next == NULL);
}

/*二叉搜尋樹資料結構*/
BinTree CreateTNode()
{
    BinTree T;
    T = (BinTree)malloc(sizeof(struct TNode));
    T->Data = 0;
    T->Left = T->Right = NULL;
    return T;
}
BinTree BuildCompleteTree(int Data[],int N)
{
    BinTree T;
    int H,R,i;
    int LeftCnt;
    H = TreeHeight(N);
    if(H>-1)
    {
        if(H == 0)
        {
            T = CreateTNode();
            T->Data = Data[0];
        }
        else
        {
            R = N- pow(2,H) + 1;
            if(R > (int)pow(2,(H-1)))
            {
                LeftCnt = pow(2,H) - 1;
            }
            else
                LeftCnt = pow(2,H-1) - 1 + R;
            int RightCnt = N - LeftCnt - 1;
            int LeftData[LeftCnt];
            for(i=0;i<LeftCnt;i++)
            {
                LeftData[i] = Data[i];
            }
            T = CreateTNode();
            T->Data = Data[LeftCnt];
            T->Left = BuildCompleteTree(LeftData,LeftCnt);
            if(RightCnt)
            {
                int RightData[RightCnt];
                for(i=0;i<RightCnt;i++)
                {
                    RightData[i] = Data[LeftCnt+1+i];
                }
                T->Right = BuildCompleteTree(RightData,RightCnt);
            }
        }
        return T;
    }
    else
    {
        printf("Tree Height Error");
        return NULL;
    }
}

int TreeHeight(int n)
{
    if(n)
        return (int)(log(n)/log(2));
    else
        return -1;
}


void BubbleSort(int a[], int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
        {
            if(a[i] > a[i + 1])
            {
                temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
}

void LevelOrderTraversal(BinTree root)
{
    Queque q;
    q = CreateQueue();
    if(!root)
        return;
    int flag = 1;
    TPosition tmp;
    add(q,root);
    while(!IsEmpty(q))
    {
        tmp = Delete(q);

        if(flag)
        {
            printf("%d",tmp->Data);
            flag = 0;
        }
        else
            printf(" %d",tmp->Data);
        if(tmp->Left == NULL && tmp->Right == NULL)
            ;
        else if(tmp->Left == NULL)
            add(q,tmp->Right);
        else if(tmp->Right == NULL)
            add(q,tmp->Left);
        else
        {
            add(q,tmp->Left);
            add(q,tmp->Right);
        }
    }
}

結果:

這裡寫圖片描述

問題分析與總結: