Binary sort tree (three-order output)

Source: Internet
Author: User

Binary sort Tree

time limit:1 seconds

Memory limit:32 MB

Special question: No

submitted:6039

Resolution:2538

Title Description:

Enter a series of integers, establish a two-fork sort number, and carry out pre-order, middle-order, post-sequence traversal.

Input:

Enter the first line to include an integer n (1<=n<=100). The next line consists of n integers.

Output:

There may be more than one set of test data, for each set of data, the problem of the data to create a binary sorting tree, and the two-fork sorting tree for the pre-order, middle order and post-sequence traversal. Each of the traversal results outputs a row. There is a space after the last data in each row.

Sample input:
51 6 5) 9 8
Sample output:
 
  
Tips:

There may be duplicate elements in the input, but the output of the two-tree traversal sequence repeats elements without output.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespaceStd;typedefstructnode{Node*l, *R; intv; Node () {L= NULL; R =NULL;}}*Tree, Node;tree build (tree p,intv) {    if(p = =NULL) {P=NewNode (); P->v =v; returnp; }    if(V < p->v) P->l = Build (p->L, v); Else if(V > p->v) P->r = Build (p->R, v); Else        returnp; returnp;}voidpreorder (tree p) {if(p = = NULL)return; printf ("%d", p->v); Preorder (P-M); Preorder (P-r);}voidinorder (tree p) {if(p = = NULL)return; Inorder (P-l); printf ("%d", p->v); Inorder (P-r);}voidpostorder (tree p) {if(p = = NULL)return; Postorder (P-l); Postorder (P-R); printf ("%d", p->v);}intMain () {intN;  while(~SCANF ("%d", &N))        {tree p; P=NULL; intv;  for(inti =0; i < N; i++) {scanf ("%d", &v); P=Build (P, v);        } preorder (P); Puts ("");        Inorder (P); Puts ("");        Postorder (P); Puts (""); }    return 0;}

Binary sort tree (three-order output)

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.