Tree structure exercises--sequential traversal of ordered binary tree (binary search tree)

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 <stdio.h>#include<stdlib.h>typedefstructnode{intdata; structNode *Lchild; structNode *Rchild;} Tree;intI, N, key, cnt =0; Tree*insert (Tree *t,intkey) {    if(T = =NULL) {Tree*p; P= (Tree *) malloc (sizeof(Tree)); P->data =key; P->lchild =NULL; P->rchild =NULL; T=p; }    Else    {        if(Key < T->data) T->lchild = Insert (t->lchild, key); ElseT->rchild = Insert (t->rchild, key); }    returnt;}voidInorder (Tree *6) {    if(t!=NULL) {inorder (t-lchild); CNT++; if(cnt==N) printf ("%d\n", t->data); Elseprintf ("%d", t->data); Inorder (t-rchild); }}intMain () { while(~SCANF ("%d", &N)) {Tree* t =NULL; CNT=0;  for(i=0; i<n; i++) {scanf ("%d", &key); T=Insert (t, key);    } inorder (t); }    return 0;}

Tree structure exercises--sequential traversal of ordered binary tree (binary search 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.