A binary Search Tree (BST) is recursively defined as a Binary Tree which have the following properties:
Both the left and right subtrees must also is binary search trees.Given the structure of a binary tree and a sequence of distinct integer keys, there is only one-through to fill these keys int o The tree so, the resulting tree satisfies the definition of a BST. You is supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.
Input Specification:
each input file contains one test case. For each case, the first line gives a positive integer N (<=100) which are the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the format "Left_index Right_index", provided That the nodes was numbered from 0 to N-1, and 0 was always the root. If one child is missing, then-1 would represent the NULL child pointer. Finally N Distinct integer keys is given in the last line.
Output Specification:
For each test case, print in one line the level order traversal sequence of this tree. All the numbers must is separated by a space, with no extra space at the end of the line.
Sample Input:91 62 3-1-1-1 45-1-1-17-1-1 8-1-173 45 11 58 82 25 67 38 42
Sample Output:58 25 82 11 38 67 45 73 42
The topic requires the creation of a two-fork search tree for a given structure, and a sequence traversal. Because given the structure, therefore can not use the general method to create this tree, the topic specifies the use of array storage, an array of storage tree problem is not easy to determine whether the tree is empty, the previous use of the chain table storage tree, can be determined by whether the node is null, here is a simple and easy way.
"Methods of the array storage tree"
First, create an array of 1 more nodes than the tree tempt,0 location to store the data-1, and then let the tree T from position 1 to store, so that the empty tree index of 1, when access to t[-1]. The actual access is tempt[0], and the data stored here is exactly-1. This can therefore prevent the address from crossing out and convenient processing.
"Handling of element insertions"
Because the middle sequence traversal of the binary search tree is the ascending order of all the elements, all the elements that will be inserted are sorted to get the results of the middle order traversal of the tree.
Observing the increase of the array index is the root, left, and right order, that is, the tree's first order traversal, for the binary search tree, the left element must be less than the right side, so only need to design a calculation Saozi right sub-tree elements of the function, and then in the middle sequence traversal sequence interception, such as the first interception, There are 5 elements on the left side of the 0-position element, and the 6th is the root, followed by the right subtree, and then recursively resolves all subtrees, completing the insertion.
"Sequence traversal problem for elements"
The sequence traversal is basically the same as DFS, which requires only one queue and then a sequential traversal.
The AC code is as follows:
#include <iostream>using namespace std;typedef struct TreeNode *node;struct treenode{int left;int right;int data;}; Node t;int *table;typedef struct queue_struct *queue;struct queue_struct{treenode elements[101];int front;int rear;}; Queue Createqueue () {Queue q = (Queue) malloc (sizeof (queue_struct)); Q->front = Q->rear = 0;return q;} void EnQueue (Queue q, TreeNode Item) {if (Q->rear >=) {return;} q->elements[q->rear++] = Item;} TreeNode DeQueue (Queue q) {if (Q->front = = Q->rear) {exit (0);} return q->elements[q->front++];} void preorder (TreeNode node) {if (Node.left! =-2) {printf ("%d", Node.data);p reorder (T[node.left]);p Reorder (t[ Node.right]);}} void Countnodes (TreeNode node, int* count) {if (Node.left! =-2) {(*count) ++;countnodes (T[node.left], count); Countnodes ( T[node.right], count);}} int Countchilds (TreeNode node) {int count = 0;countnodes (node, &count); return count;} int compare (const void* A, const void* b) {return * (int*) A-* (int*) b;} void Insettobst (int stArt, int end, node node) {if (Node->left! =-2) {int begin = Start;int Stop = start + Countchilds (T[node->left]); Node ->data = Table[stop];insettobst (begin, Stop-1, T + node->left); begin = stop + 1;stop = begin + Countchilds (t[node- >right]); Insettobst (begin, Stop-1, T + node->right);}} void Mediaorder (Node T) {int count = 0;; Queue q = Createqueue (); EnQueue (q, t[0]); while (Q->front! = q->rear) {TreeNode T = DeQueue (q); if (count = = 0) {count = 1;printf ("%d", t.data); }else{printf ("%d", T.data);} if (t[t.left].data!=-1) {EnQueue (q, T[t.left]);} if (t[t.right].data!=-1) {EnQueue (q, T[t.right]);}} printf ("\ n");} int main () {int n;cin >> N; Node Temptree = (node) malloc (sizeof (TreeNode) * (n+1)); Temptree->left =-2; Temptree->right =-2; Temptree->data =-1; t = temptree + 1;for (int i = 0; i < N; i++) {node node = T + I;node->data = 0;scanf ("%d%d", &node->left, & ; node->right);} Table = (int*) malloc (sizeof (int) *n); for (int i = 0; i < N; i++) {scanf ("%d ", table + i);} Qsort (table, N, sizeof (int), compare), Insettobst (0, N-1, T + 0); Mediaorder (T); return 0;}
1099. Build A Binary Search Tree (30)