Binary sort Tree

Source: Internet
Author: User

Topic:

Ideas:

The operation of binary sorting tree please see another blog post.

The code is as follows:
1 /*2 Two fork sorting tree3 */4 5#include <stdio.h>6#include <string.h>7#include <math.h>8#include <stdlib.h>9#include <time.h>Ten#include <stdbool.h> One  A //two fork tree structure definition -typedefstruct_node { -     intdata; the     struct_node *lchild, *Rchild; - } node; -  - //Two fork Sort tree Insert node x + voidInsert (node** root,intx) { -     if(*root = = NULL) {//if the tree is empty, that is the insertion position +         //new Node Anode* p = (node*)malloc(sizeof(node)); atP->data =x;  -P->lchild = P->rchild =NULL; -(*root) = p;//Insert new node -         return; -     } -     if(x = = (*root)->data) {//repeating nodes, not processing in         return; -}Else if(x < (*root)->data) {//Small root node weight value toInsert (& (*root)->lchild, x);//insert to left subtree +}Else{//more than the root node weight value -Insert (& (*root)->rchild, x);//insert to right subtree the     } * } $ Panax Notoginseng //First Order Traversal - voidPreorder (node*root) { the     if(Root = =NULL) { +         return; A     } theprintf"%d", root->data); +Preorder (root->lchild); -Preorder (root->rchild); $ } $  - //Middle Sequence Traversal - voidInorder (node*root) { the     if(Root = =NULL) { -         return;Wuyi     }     theInorder (root->lchild); -printf"%d", root->data); WuInorder (root->rchild); - } About  $ //subsequent traversal - voidPostorder (node*root) { -     if(Root = =NULL) { -         return; A     }     +Postorder (root->lchild); thePostorder (root->rchild); -printf"%d", root->data); $ } the  the intMain () { the     intN; the      while(SCANF ("%d", &n)! =EOF) { -         inti; innode* root =NULL; the          for(i=0; i<n; ++i) { the             intx; Aboutscanf"%d", &x);//input Data theInsert (&root, x);//inserting Nodes the         } thePreorder (root);//output pre-order sequence +printf"\ n"); -Inorder (root);//Output sequencing sequence theprintf"\ n");BayiPostorder (root);//output order sequence theprintf"\ n"); the     } -  -     return 0; the}

Binary sort 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.