It's called a two-fork search tree.
Code:
Binary sort tree (binary search tree binary searches Trees)//Xin Yang # include <stdio.h> #include <stdlib.h>typedef struct node{int key; struct node *lchild, *rchild;} Bstnode, *bstree;//Insert int insertbst (bstree *bst, int k) {Bstree R, s, Pre;r = (bstree) malloc (sizeof (Bstnode)); r->key = k; R->lchild = Null;r->rchild = null;if (*bst = = NULL) {*bst = R;return 1;} Pre = Null;s = *bst;while (s) {if (k = = S->key) return 0;else if (K < S->key) {pre = S;s = S->lchild;} Else{pre = S;s = S->rchild;}} if (K < Pre->key) Pre->lchild = R;elsepre->rchild = R;return 1;} void Createbst (Bstree *bst) {int key;*bst = NULL;SCANF ("%d", &key), while (Key! =-1) {Insertbst (BST, key); scanf ("%d", & Amp;key);}} /* * Print binary tree: * Middle sequence traversal * */void inorder (bstree root) {if (root = NULL) {inorder (root->lchild);p rintf ("%d", root->key); InO Rder (Root->rchild);}} /* * search * */bstree searchbst (bstree BST, int key) {Bstree q;q = bst;//recursive while (q) {if (Q->key = = key) return Q;if (Q->key & Gt key) Q=q->lchild;elseq=q->rchild;} RetuRN NULL; Lookup failed}int Main () {bstree t;int tag = 1;int m, n;printf ("build two-fork sort tree, enter sequence to end with-1: \ n"); Createbst (&t);p rintf ("Middle sequence traversal binary tree, sequence: \ n"), Inorder (T);p rintf ("\ n"), while (tag! = 0) {printf ("Please enter the element you are looking for: \ n"); scanf ("%d", &n), if (Searchbst (T, n) = = NULL) printf ("Sorry to find failed!\n"); elseprintf ("Find successful! The number found is%d\n ", Searchbst (t,n)->key);p rintf (" whether to continue looking for is: Press 1 otherwise press 0:\n "); scanf ("%d ", &tag);} return 0;}
Results:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Data structure---C language implementation two-fork sorting tree (binarysorttree)