1. 程式人生 > >BST及其各種操作(C實現)

BST及其各種操作(C實現)

#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct Node{
    ElementType data;
    struct Node* left;
    struct Node* right;
}BinNode,*BinTree;
/*****************************************************/
void insert (BinTree* head, ElementType x){
    if(*head == NULL){
        *head = (BinTree)malloc(sizeof(BinNode));
        (*head)->data = x;
        (*head)->left = NULL;
        (*head)->right = NULL;
        return;
    }
    if((*head)->data == x)
        return;
    else if(x < (*head)->data)
        insert(&((*head)->left), x);
    else
        insert(&((*head)->right), x);
}
/*****************************************************/
void PreShowBST(BinTree head){   //前序
    if(head == NULL)
        return;
    printf("%d\n",head->data);
    PreShowBST(head->left);
    PreShowBST(head->right);
}
void MidShowBST(BinTree head){  //中序
    if(head == NULL)
        return;
    PreShowBST(head->left);
    printf("%d\n",head->data);
    PreShowBST(head->right);
}
void LastShowBST(BinTree head){  //前序
    if(head == NULL)
        return;
    PreShowBST(head->left);
    PreShowBST(head->right);
    printf("%d\n",head->data);
}
/*****************************************************/
BinTree findMin(BinTree head){
    if(head == NULL)
        return NULL;
    else if(head->left == NULL){
        //printf("%d\n",head->data);
        return head;
    }
    return findMin(head->left);
}
BinTree findMax(BinTree head){
    if(head == NULL)
        return  NULL;
    else if(head->right == NULL){
        //printf("%d\n",head->data);
        return head;
    }
    return findMax(head->right);
}
/*****************************************************/
int getHeight(BinTree head){   //求最深的高度
    int lH=0,rH=0;
    if(head == NULL)
        return 0;
    if(head->left != NULL)
        lH += getHeight(head->left)+1;
    if(head->right != NULL)
        rH += getHeight(head->right)+1;
    return lH >= rH ? lH: rH;
}
/*****************************************************/
BinTree deleteNode(BinTree* head,ElementType x){
    BinTree temp = NULL;
    if(*head==NULL)
        printf("刪除失敗");
    else if((*head)->data < x)
        deleteNode(&(*head)->left, x);
    else if((*head)->data > x)
        deleteNode(&(*head)->right, x);
    else if((*head)->left && (*head)->right){
        temp = findMin((*head)->right);
        (*head)->data = temp->data;     //將目標節點替換成右子樹中最大的節點。
        (*head)->right = deleteNode(&(*head)->right, (*head)->data);//在右子樹中將該節點刪除
    }
    else{
        temp = *head;
        if((*head)->left == NULL)
            return *head = (*head)->right;
        else if((*head)->right == NULL)
            return *head = (*head)->left;
        free(temp);
    }
    return *head;
}

int main(){
    BinTree head = NULL;
    insert(&head, 6);
    insert(&head, 2);
    insert(&head, 8);
    insert(&head, 1);
    insert(&head, 5);
    insert(&head, 3);
    insert(&head, 4);
    PreShowBST(head);
    findMax(head);
    printf("*%d*\n",getHeight(head));
    deleteNode(&head,6);
    PreShowBST(head);
    return 0;
}