Establishment of binary sorting tree

Source: Internet
Author: User

Design an algorithm that reads a whole string of integers to form a binary sort tree and find it.

Test data: 60 35 69 84 96 13 66 34 21 0

Output: 13 21 34 35 60 66 69 84 96

Input Find data: 40

Output: 13 21 34 35 60 66 69 84 96

Algorithm idea: The composition of the two-fork sorting tree, starting from an empty two-fork tree, each time a node data is entered, a new node is created to insert into the currently generated two-fork sort tree, so its main operation is a two-fork sort tree insert operation. Insert a new node in the binary sort tree, as long as it is guaranteed to conform to the definition of the binary sort tree after insertion. Binary sorting Tree search process: When the binary sorting tree is non-empty, first compares the given value with the root node, if equal, the search succeeds, if the small root node, the Zuozi continues to find, if the large root node value, then on the right subtree continue to find.

Code:

(1) Middle sequence traversal binary tree algorithm inorder (2) interpolation algorithm of binary sorting Tree Inserbst (3) Generating two fork sorting tree algorithm Creatord (4) Main function

#include <iostream>#include<stdio.h>#include<malloc.h>using namespaceStd;typedefstructnode{intkey; structNode *lchild, *Rchild;} Bstnode;voidInorder (Bstnode *t) {    if(t!=NULL) {inorder (t-lchild); printf ("%4d",t->key); Inorder (t-rchild); }//Middle order Lookup}bstnode* INSERTBST (Bstnode *t,intk) {Bstnode*p; if(t==NULL) {P= (Bstnode *)malloc(sizeof(Bstnode)); P->key=K; P->lchild=NULL; P->rchild=NULL; return(P); }    Else if(t->key==k)return(t); Else if(t->key>k) {T->lchild=insertbst (t->lchild,k);return(t);} Else{T->rchild=insertbst (t->rchild,k);return(t);}} Bstnode*Creatord () {Bstnode*T;intkey;t=null;scanf ("%d",&key); while(key!=0) {T=Insertbst (T,key); scanf ("%d",&key);} return(t);}intMain () {//cout << "Hello world!" << Endl;    intkey; Bstnode*Root; Root=Creatord ();    Inorder (root); printf ("\ n Input Search key:"); scanf ("%d",&key);    Insertbst (Root,key);    Inorder (root); printf ("\ n"); return 0;}

The malloc function allocates space dynamically;
The prototype is: void * malloc (int size);

The use method is generally:
Suppose you define a struct type named node, you define a pointer variable of node type named a, using the following statement:
Node * a= (node *) malloc (sizeof (node));
Where (node *) is cast, the return type void * is converted to Node *,sizeof (node) to take up the space occupied by the node type, as in the case of an int type of 4 bytes on my machine, and sizeof (int) returns 4;
Use malloc to include # include <malloc.h>

Establishment of binary 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.