sicily 1310. Right-Heavy Tree
2015-11-17 17:15
483 查看
1310. Right-Heavy Tree
Constraints
Time Limit: 1 secs, Memory Limit: 32 MBDescription
A right-heavy tree is a binary tree where the value of a node is greater than or equal to the values of the nodes in its left subtree and less than the values of the nodes in its right subtree. A right-heavy tree could be empty.Write a program that will create a right-heavy tree from a given input sequence and then traverse the tree and printing the value of the node each time a node is visited using inorder, preorder and postorder traversal.
The program should create the nodes in the tree dynamically. Thus, basically the tree can be of any size limited only by the amount of memory available in the computer.
Input
The input will contain several test cases, each of them as described below.The first number in the input indicates the number of nodes in the tree. Then, the input is followed by the integers comprising the values of the nodes of the tree.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. The output will be the sequence of node and labeled by the traversal method used, as shown in the sample output below.Sample Input
8 8 2 4 7 5 3 1 6 9 5 5 6 3 2 9 3 3 2 8 4 2 1 4 3 2 5 1 0
Sample Output
Inorder: 1 2 3 4 5 6 7 8 Preorder: 8 2 1 4 3 7 5 6 Postorder: 1 3 6 5 7 4 2 8 Inorder: 2 2 3 3 3 5 5 6 9 Preorder: 5 5 3 2 2 3 3 6 9 Postorder: 2 3 3 2 3 5 9 6 5 Inorder: 1 1 2 2 3 4 4 5 Preorder: 4 2 1 1 2 4 3 5 Postorder: 1 2 1 3 4 2 5 4 Inorder: Preorder: Postorder:
题目分析
构造树,使右节点的值>=根>左节点的值
#include <stdio.h>
struct Node {
Node *l, *r;
int id;
Node () {
}
Node (int id_) {
id = id_;
l = NULL;
r = NULL;
}
};
Node *nodes;
inline void insert(int key) {
Node *temp;
temp = nodes;
while (1) {
if (temp->id >= key) {
if (temp->l == NULL) {
temp->l = new Node(key);
return;
} else {
temp = temp->l;
}
} else {
if (temp->r == NULL) {
temp->r = new Node(key);
return;
} else {
temp = temp->r;
}
}
}
}
inline void inorder(Node *t) {
if (t->l != NULL)
inorder(t->l);
printf(" %d", t->id);
if (t->r != NULL)
inorder(t->r);
}
inline void preorder(Node *t) {
printf(" %d", t->id);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
inline void postorder(Node *t) {
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf(" %d", t->id);
}
int main() {
int num;
bool first = false;
while (scanf("%d", &num) != EOF) {
if (first)
printf("\n");
first = true;
if (num == 0) {
printf("Inorder: \nPreorder: \npostorder: \n");
break;
}
int digit[num];
for (int c = 0; c < num; ++c)
scanf("%d", &digit[c]);
nodes = new Node(digit[0]);
for (int c = 1; c < num; ++c) {
insert(digit[c]);
}
//
printf("Inorder:");
inorder(nodes);
printf("\n");
//
printf("Preorder:");
preorder(nodes);
printf("\n");
//
printf("Postorder:");
postorder(nodes);
printf("\n");
}
return 0;
}
相关文章推荐
- Android UI设计的基本元素有哪些
- LeetCode237:Delete Node in a Linked List
- 淘宝应对"双11"的技术架构分析
- 这篇文章看完,理解完,胜过你听三年培训课程
- svn cleanup failed–previous operation has not finished 解决方法
- oc中的小问题
- C# Winform打开网页(网址url)Process.Start
- git 上传提示需要离开工作空间
- 利用自定义的SQL语句生成类代替实体类的使用
- 并查集 + 拓扑排序 hdu1811 Rank of Tetris
- java程序员第十五课 -MySQL数据库和SQL语言
- Condition使用总结
- iOS开发使用自己添加的字体
- 20151117 Linux下磁盘管理(centos 6.5)1.添加硬盘 2.硬盘分区 3.格式化硬盘 4.挂载硬盘 5.设置开机自动挂载】
- dubbo源码学习(一)之ExtensionLoader
- 导数中的最小化日志记录:测试和分析
- 最大独立集求解
- axure淘宝购买记录Project 2013软件+
- 字体sp转px
- 【C语言】【面试题】使用main函数的参数,实现一个整数计算机,程序可以接受三个参数