(programming Training) Looking back, data structure--establishment of two-fork sort tree

Source: Internet
Author: User

Recently in reviewing data structure, by the way look at the time of the freshman write code, after reading more than at the beginning of a deeper experience.


I hope these can provide some reference for beginners.


Under the vc++6.0 can be run, at the beginning also wrote a lot of comments.



"Problem description"

Constructs its corresponding two-fork sort tree with the input set of integers as the value of the keyword, and finds the value of the given point on the two-fork sort tree


"Basic Requirements"

• Input: Enter a set of keywords (integers) and the values you want to find

• Output: Sorted keywords and search results


Sample input

60 35 69 84 96 13 66 34 21-1

40

Sample output

13 21 34 35 60 66 69 84 96

the ... didn't find

"Module Partitioning"

1. Bstinorder () function: Middle sequence traversal binary sort tree

2. Btsinsert () function: Insert a node on the binary sort tree

3. Createbst () function: Generate two fork sort tree

4. Bstsearch (): Find on binary sort tree, find, return pointer to this node, otherwise return null pointer

5. Main function: Call each function



#include <stdio.h> #include <malloc.h>typedef struct bstnode{<span style= "White-space:pre" ></ Span>int key;<span style= "White-space:pre" ></span>struct Bstnode *lchild,*rchild;} Bstnode,*bstree; Bstree f;//sequence traversal binary sort tree void Bstinorder (Bstree t) {<span style= "white-space:pre" ></span>if (t) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>bstinorder (T->lchild); <span style= "White-space:pre" ></span>printf ("%-4d", T->key); <span style= "White-space:pre" > </span>bstinorder (t->rchild); <span style= "White-space:pre" ></span>}}//find node int with k in binary tree Bstsearch (Bstree t, int k) {<span style= "white-space:pre" ></span>bstree p;<span style= "White-space:pre "></span>p=t;<span style=" White-space:pre "></span>f=null;<span style=" White-space:pre " ></span>while (p) <span style= "White-space:pre" ></span>{<span style= "WHITE-SPACE:PRE "></span>if (k==p->key) <span style=" White-space:pre "></span>return 1;<span style=" White-space:pre "></span>else if (k<p->key) <span style=" White-space:pre "></span>{< Span style= "White-space:pre" ></span>f=p;<span style= "White-space:pre" ></span>p=p->lchild ; <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>else<span Style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>f=p;<span style= " White-space:pre "></span>p=p->rchild;<span style=" White-space:pre "></span>}<span style = "White-space:pre" ></span>}<span style= "White-space:pre" ></span>return 0;} Find the node with the key value K in the binary tree T, insert kvoid Bstinsert (bstree& t, int k) {<span style= "White-space:pre" If no k is found) ></span >bstree s;<span style= "White-space:pre" ></span>int p;<span style= "White-space:pre" ></span >p=bstsearcH (t,k); <span style= "White-space:pre" ></span>if (!p) <span style= "White-space:pre" ></span>{ <span style= "White-space:pre" ></span>s= (bstnode*) malloc (sizeof (Bstnode)); <span style= "White-space :p Re "></span>s->key=k;<span style=" White-space:pre "></span>s->lchild=s->rchild= Null;<span style= "White-space:pre" ></span>if (!t) <span style= "White-space:pre" ></span>t=s ; <span style= "White-space:pre" ></span>else if (k<f->key) <span style= "White-space:pre" >< /span>f->lchild=s;<span style= "White-space:pre" ></span>else<span style= "White-space:pre" ></span>f->rchild=s;<span style= "White-space:pre" ></span>}}//Create two fork sort Tree Bstree CreateBST () { <span style= "White-space:pre" ></span>bstree t;<span style= "White-space:pre" ></span>int key ; <span style= "White-space:pre" ></span>//<span style= "White-space:pre" ></span>BSTreeBstinsert (Bstree t, int k); <span style= "White-space:pre" ></span>t=null;<span style= "White-space:pre" ></span>printf ("input data \ n"), <span style= "White-space:pre" ></span>scanf ("%d", &key);< Span style= "White-space:pre" ></span>while (key!=-1) <span style= "White-space:pre" ></span>{ <span style= "White-space:pre" ></span>bstinsert (t, key);//<span style= "White-space:pre" ></ span>printf ("input data \ n"), <span style= "White-space:pre" ></span>scanf ("%d", &key); <span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>return t;} int main () {<span style= "white-space:pre" ></span>bstree root;<span style= "White-space:pre" ></ Span>int p,key;<span style= "White-space:pre" ></span>root=createbst (); <span style= "White-space: Pre "></span>printf (" binary sort tree list \ n "); <span style=" White-space:pre "></span>bstinorder (Root);< Span style= "white-Space:pre "></span>printf (" \ n Enter the value to find \ n "), <span style=" White-space:pre "></span>scanf ("%d ", &key); <span style= "White-space:pre" ></span>p=bstsearch (Root,key); <span style= "White-space:pre "></span>if (p) <span style=" White-space:pre "></span>printf ("%d ... able to find \ n ", key); <span style=" White-space:pre "></span>else<span style=" White-space:pre "></span >printf ("%d ... Not found \ n ", key); <span style=" White-space:pre "></span>return 0;}





(programming Training) Looking back, data structure--establishment of two-fork 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.