C language Implementation two fork sorting tree

Source: Internet
Author: User

Today should be invited by students, wrote a binary sorting tree, there is insufficient place please correct, there is the implementation of that stack in the sequence traversal function, I also did not understand, according to their own understanding to write it, by the way to expand a bit of knowledge of double pointers, in fact, with C language the most annoying pointers, the sense of Java is not considered, The end of the two-fork sort tree.

/*
(double pointer bstree *t)
Q: Why do binary trees establish nodes in data structures with double pointers? Detailed explanation under double pointers

A: pointer to pointer.
Because the nodes of the tree are to be described with pointers.
If only a pointer is used as a parameter to the function that establishes the node, the pointer value is passed to the memory in the function stack, and the function
Once returned, the function stack is destroyed and the node cannot be obtained.
With pointers, the function modifies the value that the double pointer points to (that is, the node pointer), and the node can be obtained outside the function.

The swap () function uses pointers instead of values for parameters. Just the value here is a pointer in itself, so use the pointer pointer.
*/

/*binarysorttree*/#include <iostream> #include <stdio.h> #include <stdlib.h> #include <stack>    #define SIZE 30using namespace std;/* two fork sort tree */typedef struct node{int data; struct Node *rchild,*lchild;} *bstree;stack<char> s;/* Sequential table */struct seqlist{char data[size];};/    * Create order Table */seqlist create_seqlist (seqlist L) {gets (l.data); return L;}        /* Insert construct two fork sort tree */void insert_bstree (bstree *t,char data) {Bstree S;//new node as child node or root node//tree is empty, create root node if (*t==null) {        s = (bstree) malloc (sizeof (struct Node));        S->data = data;        s->lchild=null;        s->rchild=null;    *t=s;    }//small plug left large plug right equal throw away else if (data< (*t)->data) Insert_bstree (& ((*t)->lchild), data); else if (data> (*t)->data) Insert_bstree (& ((*t)->rchild), data);}    /* Insert Create two fork sort tree */void create_bstree (bstree *t,seqlist L) {*t=null;    int k=0;    char ch = l.data[k++];        while (ch!= ' # ') {insert_bstree (t,ch);    ch = l.data[k++]; }}/* using the stack to traverse the two fork rowsSequence Tree */void stackinordertraverse (bstree root) {if (root!=null) {stackinordertraverse (root->lchild);//Traversal 左子树        S.push (Root->data); Stackinordertraverse (root->rchild);//Walk grapefruit tree} else {stack<char> s1;//auxiliary stack//output while        (!s.empty ())            {S1.push (S.top ());        S.pop ();            } while (!s1.empty ()) {printf ("%c", S1.top ());        S1.pop (); }}}/* in sequence traverse binary sort tree */void inordertraverse (bstree root) {if (root!=null) {inordertraverse (root->lchild);//Traversal        Left Dial hand tree printf ("%c", root->data);    Inordertraverse (root->rchild);//traverse grapefruit tree}}/* Delete function (very difficult) */void Delete_bstree (bstree *t,char data) {Bstree p;    Bstree p_parent;//save P's parent node Bstree s,s_parent;    p = *t;        while (p) {if (p->data==data)//found break; P_parent = p;        Record precursor initial value if (P->data>data)//root node big words find left dial hand tree p = p->lchild; else P = P->rchilD        } if (P==null) {printf ("Delete failed without this data \ n");    Return }//Zuozi if not, p may be the right subtree or the leaf if (p->lchild==null) {///root node if (p_parent==null) {*t = p->        Rchild; }//p is the parent's left subtree else if (p_parent->lchild==p) {p_parent->lchild = p->rchild;//Its right subtree as the left child of its parents Child} else P_parent->rchild = p->rchild;    Use its right subtree as the right child of its parents free (p);        }//Saoto, p may have only Zuozi or right subtree left subtree have else {s_parent = p;        s = p->lchild;            Find the right-most lower node of P while (s->rchild) {s_parent = s;        s = s->rchild; }//If p is the parent node of S if (s_parent==p) {s_parent->lchild = s->lchild;//will s right subtree as its right son}else s        _parent->rchild = s->lchild;        P->data = s->data;    Free (s);    } printf ("Delete succeeded, deleted sequence is: \ n"); Inordertraverse (*t);}    int main () {stack<char> s;    char data;    Seqlist List;    Bstree Root=null; List = create_sEqlist (List);    Create_bstree (&root,list);    Inordertraverse (root);    printf ("\ n");    Stackinordertraverse (root);    printf ("\ n");    printf ("Enter the data you want to delete:");    scanf ("%c", &data);    Delete_bstree (&root,data);    printf ("\ n"); return 0;}

  

C language Implementation two fork sorting tree

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.