Data structure Learning--two-fork search Tree ADT (programmatic)

Source: Internet
Author: User

Reference: "Data structure and algorithmic analysis--c language description" section 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);//two add void Printtree (Bintree t,void (*p) (Bintree T)) for the Fork tree node;//print operation void Printtreenode ( Bintree t);//Print a node bintree delete (Bintree t,int data);//delete a node Bintree findmin (Bintree t);//Find the Minimum node Bintree Findmax (    Bintree t);//Find the maximum node int findmaxsubmin (Bintree t);//The difference between the maximum node and the minimum node 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);} Middle order traversal void Printtree (Bintree t,void (*p) (Bintree t)) {if (t!=null) {printtree (t->left,p);//print left subtree P (T);    /Print node Printtree (t->right,p);//Print Right subtree}}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);}


Data structure Learning--two-fork search Tree ADT (programmatic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.