資料結構學習——二叉尋找樹ADT(程式化),資料結構學習adt

來源:互聯網
上載者:User

資料結構學習——二叉尋找樹ADT(程式化),資料結構學習adt

參考資料:《資料結構與演算法分析——C語言描述》4.3一節

#include<stdio.h>#include<stdlib.h>#define N 10typedef struct BinTreeNode{    int data;    struct BinTreeNode *left;    struct BinTreeNode *right;}BinTreeNode,*BinTree;BinTree insert(BinTree T,int data);//二叉樹節點的添加void PrintTree(BinTree T,void (*p)(BinTree T));//列印操作void PrintTreeNode(BinTree T);//列印一個節點BinTree Delete(BinTree T,int data);//刪除某個節點BinTree FindMin(BinTree T);//尋找最小節點BinTree FindMax(BinTree T);//尋找最大節點int FindMaxSubMin(BinTree T);//最大節點與最小節點的差值int main(){    int i=0,n=0;    int data[N]= {10,23,11,98,111,87,34,11,154,4};    BinTreeNode *root=NULL;    BinTree p;    for(i=0;i<N;i++)    {        root=insert(root,data[i]);    }    PrintTree(root,PrintTreeNode);    n=FindMaxSubMin(root);    printf("%d\n",n);    p=FindMin(root);    PrintTreeNode(p);    p=FindMax(root);    PrintTreeNode(p);    printf("\n");    root=Delete(root,154);    printf("\n");    PrintTree(root,PrintTreeNode);    printf("\n");    n=FindMaxSubMin(root);    printf("%d\n",n);    p=FindMin(root);    PrintTreeNode(p);    p=FindMax(root);    PrintTreeNode(p);    root=Delete(root,1154);        free(root);    free(p);    return 0;}BinTree insert(BinTree T,int data){    if(T==NULL)    {        T=malloc(sizeof(BinTreeNode));        if(T==NULL)            printf("Out of space!\n");        else        {            T->data=data;            T->left=NULL;            T->right=NULL;        }    }    else    {        if(data<T->data)            T->left=insert(T->left,data);        else            T->right=insert(T->right,data);    }    return T;}void PrintTreeNode(BinTree T){    printf("%d\t",T->data);}//中序遍曆void PrintTree(BinTree T,void (*p)(BinTree T)){   if(T!=NULL)   {       PrintTree(T->left,p);//列印左子樹       p(T);//列印節點       PrintTree(T->right,p);//列印右子樹   }}BinTree Delete(BinTree T,int data){    BinTree temp;    if(T==NULL)        printf("\n%d not found.\n",data);    else    {        if(data<T->data)            T->left=Delete(T->left,data);        else        {            if(data>T->data)                T->right=Delete(T->right,data);            else            {                if( T->left &&  T->right)//two children                {                    temp=FindMin(T->right);                    T->data=temp->data;                    T->right=Delete(T->right,T->data);                }                else//one or zero child                {                    temp=T;                    if(T->left==NULL)                        T=T->right;                    else                    {                        if(T->right==NULL)                            T=T->left;                    }                    free(temp);                }            }        }    }    return T;}BinTree FindMin(BinTree T){    if(T==NULL)        return NULL;    else        if(T->left==NULL)            return T;        else return FindMin(T->left);}BinTree FindMax(BinTree T){    if(T==NULL)        return NULL;    else        if(T->right==NULL)            return T;        else return FindMax(T->right);}int FindMaxSubMin(BinTree T){    int Max,Min;    BinTree p;    p=T;    if(T==NULL)        return 0;    while(T->left!=NULL)        T=T->left;    Min=T->data;    while(p->right)        p=p->right;    Max=p->data;    return (Max-Min);}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.