您的位置:首页 > 其它

csuoj1005: Binary Search Tree analog

2018-04-05 00:30 399 查看

1005: Binary Search Tree analog  

Description

Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following property:
each node has a Key value, which can be used to compare with each other.
For every node in the tree, every Key value in its left subtree is smaller than its own Key value.
For every node in the tree, every Key value in its right subtree is equal to or larger than its own Key value.
Now we need to analog a BST, we only require one kind of operation: inserting.
First, we have an empty BST. Input is a sequence of numbers. We need to insert them one by one flowing the rules below:
If the inserted value is smaller than the root's value, insert it to the left subtree.
If the inserted value is larger than or equal to the value of the root's value, insert it to the right subtree.
After each input, we need to output the preorder, inorder, postorder traversal sequences.
About tree traversal, the following is from Wikipedia:

Depth-first Traversal

To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:
Visit the root.
Traverse the left subtree.
Traverse the right subtree.
To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:
Traverse the left subtree.
Visit the root.
Traverse the right subtree.
To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:
Traverse the left subtree.
Traverse the right subtree.
Visit the root.
Look at the folowing example:
Intput is a sequence of 5 integers: 3 6 9 5 1
After each integer inserted the structure of the tree is illustrated in the flowing:
3
/   \
1      6
/  \
5     9

Input

The first integer of the input is
T
, the number of test cases. Each test case has two lines. The first line contain an integer
N
,(1<=
N
<=1000), the number of numbers need to be inserted into the BST. The second line contain
N
integers separated by space, each integer is in the range of [0,230].

Output

Each test case, output must contain three lines: the preorder, inorder and postorder traversal sequence. The numbers in each line should be separated by a single space and you should not output anything at the end of the line! Output a blank line after each case.

Sample Input

1
5
3 6 9 5 1

Sample Output

3 1 6 5 9
1 3 5 6 9
1 5 9 6 3
#include<cstdlib>
#include<cstdio>

struct Node{
long val;
struct Node* left;
struct Node* right;
};
const int MAX = 1005;
long A[MAX];

Node* root = NULL;  //根节点

//插入二叉树节点
void insert_node(long val)
{
if (root == NULL)
{
root = (Node*)malloc(sizeof(Node*));
root->val = val;
root->left = NULL;
root->right = NULL;
}
else
{
Node* pos = root;
Node* pre = root;   //保留前驱结点
bool flag = true;   //true表示插在左边,false表示插在右边
while (pos != NULL)
{
pre = pos;
if (val < pos->val)
{
pos = pos->left;
flag = true;
}
else
{
flag = false;
pos = pos->right;
}
}

pos = (Node*)malloc(sizeof(Node*));
pos->val = val;
pos->left = NULL;
pos->right = NULL;
if (flag)
pre->left = pos;
else
pre->right = pos;
}
}

//先序遍历二叉树
void preorder(Node* p)
{
if (p != NULL)
{
printf("%ld ", p->val);
preorder(p->left);
preorder(p->right);
}
}

//中序遍历二叉树
void inorder(Node* p)
{
if (p != NULL)
{
inorder(p->left);
printf("%ld ", p->val);
inorder(p->right);
}
}

//后序遍历二叉树
void postorder(Node* p)
{
if (p != NULL)
{
postorder(p->left);
postorder(p->right);
printf("%ld ", p->val);
}
}

//释放二叉树节点
void free_node(Node** p)
{
if (*p == NULL)
return;
free_node(&((*p)->left));
free_node(&((*p)->right));
free(*p);
*p = NULL;
}

int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
long key;
scanf("%ld", &key);
insert_node(key);
}
preorder(root);
printf("\n");
inorder(root);
printf("\n");
postorder(root);
free_node(&root);
printf("\n\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: