Binary sort Tree

Source: Internet
Author: User

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:5 1 6 5 9 8Sample output:1 6 5 9 8 1 5 6 8 9 5 8 9 6 1Tips:There may be duplicate elements in the input, but the output of the two-tree traversal sequence repeats elements without output. #include <iostream> #include <stdio.h> #include <string.h>using namespace std; struct Node {node *lchild;node *rchild;int c;} TREE[110];int loc;node* creat () {Tree[loc].lchild = Tree[loc].rchild = NULL;loc++;return &tree[loc-1];}void Post (node *t) {if (T->lchild) {post (t->lchild);} if (T->rchild) {post (t->rchild);} printf ("%d", t->c);} void Mid (node *t) {if (T->lchild) {mid (t->lchild);} printf ("%d", t->c), if (T->rchild) {mid (T->rchild);}} void Pre (node *t) {printf ("%d", t->c), if (T->lchild) {pre (t->lchild);} if (t->rchild) {pre (t->rchild);}}node * INSERT (node *t, int x) { if (!t) { t=creat (); T->c=x;return T;} else if (t->c>x) {T->lchild=insert (t->lchild,x);} else if (t->c<x) {T->rchild=insert (t->rchild,x);} return t;} int main () {int N;while (cin>>n) {loc=0;int x; node *t=null; while (n--) {cin>>x; T=insert (t,x),//insert function has a return value, be sure to write T=intsert (), do not write only insert. }pre (t); Cout<<endl;mid (t); Cout<<endl;post (t); Cout<<endl;} return 0;} node *t=null; Just a null pointer was created, not a node was created. Create a node in the Insert function, reference the creat function. The principle of binary tree establishment: a 110-size node array is established, and then the creat function simply returns a pointer to each array element, forming a tree. LOC should be defined as a global variable, and LOC should be used for the creat function. And not just the main function, with Loc.

Binary 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.