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)