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