Binary tree structure exercises of data structure--sequencing traversal of binary tree (sort achievement + middle sequence traversal)

Source: Internet
Author: User

Tree structure Exercise--sequential traversal of ordered binary tree Time limit:1000ms Memory limit:65536k Topic description in the tree structure, there is a special two-fork tree called the sort binary tree, the intuitive understanding is--(1). Each node contains a key value (2). The key value of any node's Zuozi (if present) is less than the key value of that node (3). The key value of the right subtree (if present) of any one node is greater than the key value of that node. Now given a set of data, please set up a sort binary tree in the given order for this set of data, and output the result of the sequence traversal. The input input contains multiple sets of data, each set of data in the following format. The first line contains an integer n, which is the number of key values, and the key values are represented by integers. (n<=1000) The second line contains n integers, guaranteeing that each integer is within the range of int. The output establishes a sort binary tree for the given data, and outputs the sequence traversal results, one row for each output. Sample input
1221 20
Sample output
21 20


#include <iostream> #include <iomanip> #include <string> #include <string.h> #include < stdio.h> #include <algorithm> #include <queue> #include <vector>using namespace std;typedef struct    node{int data;    struct node *ll; struct node *rr;}        Binode, *bitree;void creat_sort_bitree (bitree &root, int key) {if (root==null) {root=new binode;        root->ll=null;        root->rr=null;        root->data=key;    return;        } else {if (Key < Root->data) {Creat_sort_bitree (root->ll, key);        } else {Creat_sort_bitree (ROOT-&GT;RR, key);        }}}int s[10000],e=0;void In_order (Bitree p) {if (p) {In_order (P-&GT;LL); s[e++]=p->data; The data to be accessed is stored in an array.        The first time to write the direct output, but the format is always wrong, had to give up the kind of writing!    In_order (P-&GT;RR);    }}int Main () {int n, DD;    int i;    Bitree Root;        while (cin>>n) {root=null; for (i= 0; i<n;            i++) {cin>>dd;        Creat_sort_bitree (Root, DD);        } e=0;        In_order (root);            for (i=0; i<e; i++) {if (i==0) cout<<s[i];        else cout<< "" <<s[i];    } cout<<endl; } return 0;}

See a blog in which a person's middle-order access data is written:

void visit (Bitree T) {if (t->data!=null) {if (f==1) {cout<< "" <<t->data;} else{cout<<t->data;f=1;}}} void Inorder (Bitree t) {if (t) {inorder (t->lchild); visit (t); inorder (T->rchild);}}

Binary tree structure exercises of data structure--sequencing traversal of binary tree (sort achievement + middle sequence traversal)

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.