ALDS1_7_A-RootedTree.
Description:

A graph G = (V, E) is a data structure where V is a finite set of vertices and E is a binary relation on V represented by a set of edges. Fig. 1 illustrates an example of a graph (or graphs).

A free tree is a connnected, acyclic, undirected graph. A rooted tree is a free tree in which one of the vertices is distinguished from the others. A vertex of a rooted tree is called "node."

Your task is to write a program which reports the following information for each node u of a given rooted tree T:

node ID of u

parent of u

depth of u

node type (root, internal node or leaf)

a list of chidlren of u

If the last edge on the path from the root r of a tree T to a node x is (p, x), then p is the parent of x, and x is a child of p. The root is the only node in T with no parent.

A node with no children is an external node or leaf. A nonleaf node is an internal node

The number of children of a node x in a rooted tree T is called the degree of x.

The length of the path from the root r to a node x is the depth of x in T.

Here, the given tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Fig. 2 shows an example of rooted trees where ID of each node is indicated by a number in a circle (node). The example corresponds to the first sample input.

Input:

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of each node u is given in the following format:

id k c1 c2 ... ck

where id is the node ID of u, k is the degree of u, c1 ... ck are node IDs of 1st, ... kth child of u. If the node does not have a child, the k is 0.

Output:

Print the information of each node in the following format ordered by IDs:

node id: parent = p , depth = d, type, [c1...ck]

p is ID of its parent. If the node does not have a parent, print -1.

d is depth of the node.

type is a type of nodes represented by a string (root, internal node or leaf). If the root can be considered as a leaf or an internal node, print root.

c1...ck is the list of children as a ordered tree.

Please follow the format presented in a sample output below.

Constraints:

≤ n ≤ 100000

SampleInput1:

13

0 3 1 4 10

1 2 2 3

2 0

3 0

4 3 5 6 7

5 0

6 0

7 2 8 9

8 0

9 0

10 2 11 12

11 0

12 0

SampleOutput1:

node 0: parent = -1, depth = 0, root, [1, 4, 10]

node 1: parent = 0, depth = 1, internal node, [2, 3]

node 2: parent = 1, depth = 2, leaf, []

node 3: parent = 1, depth = 2, leaf, []

node 4: parent = 0, depth = 1, internal node, [5, 6, 7]

node 5: parent = 4, depth = 2, leaf, []

node 6: parent = 4, depth = 2, leaf, []

node 7: parent = 4, depth = 2, internal node, [8, 9]

node 8: parent = 7, depth = 3, leaf, []

node 9: parent = 7, depth = 3, leaf, []

node 10: parent = 0, depth = 1, internal node, [11, 12]

node 11: parent = 10, depth = 2, leaf, []

node 12: parent = 10, depth = 2, leaf, []

SampleInput2:

4

1 3 3 2 0

0 0

3 0

2 0

SampleOutput2:

node 0: parent = 1, depth = 1, leaf, []

node 1: parent = -1, depth = 0, root, [3, 2, 0]

node 2: parent = 1, depth = 1, leaf, []

node 3: parent = 1, depth = 1, leaf, []

Codes:
//#define LOCAL

#include <cstdio>

#define N -1
#define M 100010
struct Node { int p, l, r; };
Node T[M]; int A[M]; void print(int a) {
int i = 0, b = T[a].l;
printf("node %d: parent = %d, depth = %d, ", a, T[a].p, A[a]);
if(T[a].p == N) printf("root, [");
else if(T[a].l == N) printf("leaf, [");
else printf("internal node, [");
for(; b!=N; ++i, b=T[b].r) {
if(i) printf(", ");
printf("%d", b);
}
printf("]\n");
} void rec(int a, int b) {
A[a] = b;
if(T[a].r != N) rec(T[a].r, b);
if(T[a].l != N) rec(T[a].l, b+1);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int a, b, c, d, i, j, n, r;
scanf("%d", &n);
for(i=0; i<n; ++i) T[i].p = T[i].l = T[i].r = -1; for(i=0; i<n; ++i) {
scanf("%d%d", &a, &b);
for(j=0; j<b; ++j) {
scanf("%d", &c);
if(!j) T[a].l = c;
else T[d].r = c;
d = c; T[c].p = a;
}
}
for(i=0; i<n; ++i) {
if(T[i].p == N) { r = i; break; }
} rec(r, 0);
for(i=0; i<n; ++i) print(i); return 0;
}
ALDS1_7_B-BinaryTree.
Description:

A rooted binary tree is a tree with a root node in which every node has at most two children.

Your task is to write a program which reads a rooted binary tree T and prints the following information for each node u of T:

node ID of u

parent of u

sibling of u

the number of children of u

depth of u

height of u

node type (root, internal node or leaf)

If two nodes have the same parent, they are siblings. Here, if u and v have the same parent, we say u is a sibling of v (vice versa).

The height of a node in a tree is the number of edges on the longest simple downward path from the node to a leaf.

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input:

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n lines, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1.

Output:

Print the information of each node in the following format:

node id: parent = p , sibling = s , degree = deg, depth = dep, height = h, type

p is ID of its parent. If the node does not have a parent, print -1.

s is ID of its sibling. If the node does not have a sibling, print -1.

deg, dep and h are the number of children, depth and height of the node respectively.

type is a type of nodes represented by a string (root, internal node or leaf. If the root can be considered as a leaf or an internal node, print root.

Please follow the format presented in a sample output below.

Constraints:

1 ≤ n ≤ 25

SampleInput:

9

0 1 4

1 2 3

2 -1 -1

3 -1 -1

4 5 8

5 6 7

6 -1 -1

7 -1 -1

8 -1 -1

SampleOutput:

node 0: parent = -1, sibling = -1, degree = 2, depth = 0, height = 3, root

node 1: parent = 0, sibling = 4, degree = 2, depth = 1, height = 1, internal node

node 2: parent = 1, sibling = 3, degree = 0, depth = 2, height = 0, leaf

node 3: parent = 1, sibling = 2, degree = 0, depth = 2, height = 0, leaf

node 4: parent = 0, sibling = 1, degree = 2, depth = 1, height = 2, internal node

node 5: parent = 4, sibling = 8, degree = 2, depth = 2, height = 1, internal node

node 6: parent = 5, sibling = 7, degree = 0, depth = 3, height = 0, leaf

node 7: parent = 5, sibling = 6, degree = 0, depth = 3, height = 0, leaf

node 8: parent = 4, sibling = 5, degree = 0, depth = 2, height = 0, leaf

Codes:
//#define LOCAL

#include <cstdio>

#define MAX 10000
#define NIL -1
struct Node { int parent, left, right; };
Node T[MAX]; int n, D[MAX], H[MAX]; void setDepth(int u, int d) {
if(u == NIL) return;
D[u] = d;
setDepth(T[u].left, d+1);
setDepth(T[u].right, d+1);
} int setHeight(int u) {
int h1 = 0, h2 = 0;
if(T[u].left != NIL) h1 = setHeight(T[u].left)+1;
if(T[u].right != NIL) h2 = setHeight(T[u].right)+1;
return H[u] = h1>h2?h1:h2;
} int getSibling(int u) {
if(T[u].parent == NIL) return NIL;
if(T[T[u].parent].left!=u && T[T[u].parent].left!=NIL) return T[T[u].parent].left;
if(T[T[u].parent].right!=u && T[T[u].parent].right!=NIL) return T[T[u].parent].right;
return NIL;
} void print(int u) {
printf("node %d: parent = %d, sibling = %d, ", u, T[u].parent, getSibling(u));
int deg = 0;
if(T[u].left != NIL) ++deg;
if(T[u].right != NIL) ++deg;
printf("degree = %d, depth = %d, height = %d, ", deg, D[u], H[u]);
if(T[u].parent == NIL) printf("root\n");
else if(T[u].left==NIL && T[u].right==NIL) printf("leaf\n");
else printf("internal node\n");
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, v, l, r, root = 0;
scanf("%d", &n);
for(i=0; i<n; ++i) T[i].parent = NIL;
for(i=0; i<n; ++i) {
scanf("%d%d%d", &v, &l, &r);
T[v].left = l, T[v].right = r;
if(l != NIL) T[l].parent = v;
if(r != NIL) T[r].parent = v;
} for(i=0; i<n; ++i)
if(T[i].parent == NIL) root = i;
setDepth(root, 0); setHeight(root); for(i=0; i<n; ++i) print(i); return 0;
}
ALDS1_7_C-TreeWalk.
Description:

Binary trees are defined recursively. A binary tree T is a structure defined on a finite set of nodes that either

contains no nodes, or

is composed of three disjoint sets of nodes:

  • a root node.
  • a binary tree called its left subtree.
  • a binary tree called its right subtree.

    Your task is to write a program which perform tree walks (systematically traverse all nodes in a tree) based on the following algorithms:

Print the root, the left subtree and right subtree (preorder).

Print the left subtree, the root and right subtree (inorder).

Print the left subtree, right subtree and the root (postorder).

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input:

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n linen, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1

Output:

In the 1st line, print "Preorder", and in the 2nd line print a list of node IDs obtained by the preorder tree walk.

In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs obtained by the inorder tree walk.

In the 5th line, print "Postorder", and in the 6th line print a list of node IDs obtained by the postorder tree walk.

Print a space character before each node ID.

Constraints:

1 ≤ n ≤ 25

SampleInput1:

9

0 1 4

1 2 3

2 -1 -1

3 -1 -1

4 5 8

5 6 7

6 -1 -1

7 -1 -1

8 -1 -1

SampleOutput1:

Preorder

0 1 2 3 4 5 6 7 8

Inorder

2 1 3 0 6 5 7 4 8

Postorder

2 3 1 6 7 5 8 4 0

Codes:
//#define LOCAL

#include <cstdio>

#define MAX 10000
#define NIL -1
struct Node { int p, l, r; };
Node T[MAX]; void preParse(int u) {
if(u == NIL) return;
printf(" %d", u);
preParse(T[u].l);
preParse(T[u].r);
} void inParese(int u) {
if(u == NIL) return;
inParese(T[u].l);
printf(" %d", u);
inParese(T[u].r);
} void postParse(int u) {
if(u == NIL) return;
postParse(T[u].l);
postParse(T[u].r);
printf(" %d", u);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n, i, v, l, r, root;
scanf("%d", &n);
for(i=0; i<n; ++i) T[i].p = NIL;
for(i=0; i<n; ++i) {
scanf("%d%d%d", &v, &l, &r);
T[v].l = l, T[v].r = r;
if(l != NIL) T[l].p = v;
if(r != NIL) T[r].p = v;
}
for(i=0; i<n; ++i)
if(T[i].p == NIL) root = i; printf("Preorder\n"); preParse(root);
printf("\nInorder\n"); inParese(root);
printf("\nPostorder\n"); postParse(root);
printf("\n"); return 0;
}
ALDS1_7_D-ReconstructionOfTheTree.
Codes:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std; int n, pos;
vector<int> pre, in, post; void rec(int l, int r) {
if(l >= r) return;
int root = pre[pos++];
int m = distance(in.begin(), find(in.begin(),in.end(),root));
rec(1, m);
rec(m+1, r);
post.push_back(root);
} void solve() {
pos = 0;
rec(0, pre.size());
for(int i=0; i<n; ++i) {
if(i) cout << " ";
cout << post[i];
}
cout << endl;
} int main()
{
int k;
cin >> n;
for(int i=0; i<n; ++i) {
cin >> k;
pre.push_back(k);
}
for(int i=0; i<n; ++i) {
cin >> k;
in.push_back(k);
} solve(); return 0;
}
ALDS1_8_A-BinarySearchTreeI
Codes:
//#define LOCAL

#include <cstdio>
#include <cstdlib> struct Node {
int k;
Node *p, *l, *r;
};
Node *N, *R; void insert(int u) {
Node *a = R, *b = N, *c;
c = (Node *)malloc(sizeof(Node));
c->k = u, c->l = N, c->r = N; while(a != N) {
b = a;
if(c->k < a->k) a = a->l;
else a = a->r;
}
c->p = b;
if(b == N) {
R = c;
} else {
if(c->k < b->k) b->l = c;
else b->r = c;
}
} void inOrder(Node *u) {
if(u == N) return;
inOrder(u->l);
printf(" %d", u->k);
inOrder(u->r);
} void preOrder(Node *u) {
if(u == N) return;
printf(" %d", u->k);
preOrder(u->l);
preOrder(u->r);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int a, i, n;
char c[10];
scanf("%d", &n);
for(i=0; i<n; ++i) {
scanf("%s", c);
if(c[0] == 'i') {
scanf("%d", &a);
insert(a);
} else {
inOrder(R); printf("\n");
preOrder(R); printf("\n");
}
} return 0;
}
ALDS1_8_B-BinarySearchTreeII
Codes:
//#define LOCAL

#include <cstdio>
#include <cstdlib> struct Node {
int k;
Node *p, *l, *r;
};
Node *N, *R; Node* find(Node *u, int a) {
while(u!=N && a!=u->k) {
if(a < u->k) u = u->l;
else u = u->r;
}
} void insert(int u) {
Node *a = R, *b = N, *c;
c = (Node *)malloc(sizeof(Node));
c->k = u, c->l = N, c->r = N; while(a != N) {
b = a;
if(c->k < a->k) a = a->l;
else a = a->r;
}
c->p = b;
if(b == N) {
R = c;
} else {
if(c->k < b->k) b->l = c;
else b->r = c;
}
} void inOrder(Node *u) {
if(u == N) return;
inOrder(u->l);
printf(" %d", u->k);
inOrder(u->r);
} void preOrder(Node *u) {
if(u == N) return;
printf(" %d", u->k);
preOrder(u->l);
preOrder(u->r);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int a, i, n;
char c[10];
scanf("%d", &n);
for(i=0; i<n; ++i) {
scanf("%s", c);
if(c[0] == 'f') {
scanf("%d", &a);
Node *t = find(R, a);
if(t != N) printf("yes\n");
else printf("no\n");
} else if(c[0] == 'i'){
scanf("%d", &a);
insert(a);
} else {
inOrder(R); printf("\n");
preOrder(R); printf("\n");
}
} return 0;
}

AOJ/树二叉搜索树习题集的更多相关文章

  1. 树-二叉搜索树-AVL树

    树-二叉搜索树-AVL树 树 树的基本概念 节点的度:节点的儿子数 树的度:Max{节点的度} 节点的高度:节点到各叶节点的最大路径长度 树的高度:根节点的高度 节点的深度(层数):根节点到该节点的路 ...

  2. 数据结构学习笔记_树(二叉搜索树,B-树,B+树,B*树)

    一.查找二叉树(二叉搜索树BST) 1.查找二叉树的性质 1).所有非叶子结点至多拥有两个儿子(Left和Right): 2).所有结点存储一个关键字: 3).非叶子结点的左指针指向小于其关键字的子树 ...

  3. AOJ/树与二叉搜索树习题集

    ALDS1_7_A-RootedTree. Description: A graph G = (V, E) is a data structure where V is a finite set of ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. 二叉搜索树、B树

    二叉搜索树又叫二叉排序树. B树又可写为B-树,“B-树”种的“-”无区分意义. 此外,还有B+树,B*树.

  6. 高度平衡的二叉搜索树(AVL树)

    AVL树的基本概念 AVL树是一种高度平衡的(height balanced)二叉搜索树:对每一个结点x,x的左子树与右子树的高度差(平衡因子)至多为1. 有人也许要问:为什么要有AVL树呢?它有什么 ...

  7. 数据结构中很常见的各种树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)

    数据结构中常见的树(BST二叉搜索树.AVL平衡二叉树.RBT红黑树.B-树.B+树.B*树) 二叉排序树.平衡树.红黑树 红黑树----第四篇:一步一图一代码,一定要让你真正彻底明白红黑树 --- ...

  8. 树&二叉树&二叉搜索树

    树&二叉树 树是由节点和边构成,储存元素的集合.节点分根节点.父节点和子节点的概念. 二叉树binary tree,则加了"二叉"(binary),意思是在树中作区分.每个 ...

  9. Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

    什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T ...

随机推荐

  1. 第一篇Active Directory疑难解答概述(1)

    后期预告:从接下来我会给大家讲解,Active Directory 活动目录重要性,以及在日常管理运维中如和去排查问你题.俗话说,一个不健康的Active Directory环境是不健康的Exchan ...

  2. JQ鼠标右键点击功能 兼容IE8

    //阻止浏览器当前DIV默认右键事件 $("div").unbind("mousedown").bind("contextmenu", fu ...

  3. apache 配置多个版本的 php

    注:这里说的是windows环境下的配置 我们在配置apache+php的时候,是在apache的配置文件httpd.conf里加载php的模块并指定php.ini路径 LoadModule php5 ...

  4. @ModelAttribute--SpringMVC 注解系列文章(二)

    概述 在日常的开发工作中,更新操作是经常会涉及到的一个功能,但是对于一条记录的更新,往往只会更新一部分的信息,而不会整条记录都更新,比如修改用户年龄的时候,用户密码是不需要修改的,针对这种情况,处理的 ...

  5. 自动部署Nginx和nfs并架设Nginx集群脚本

    本人经过多次尝试,简单完成了自动部署Nginx和nfs脚本,并且能够自动部署web反向代理集群,下面详细的阐述一下本人的思路.(以下脚本本人处于初学阶段,写的并不是很完善,所以需要后期进行整理和修正, ...

  6. pyqt样式表语法笔记(中)--原创

    pyqt样式表语法笔记(中) pyqt QSS python 样式表 一.弹窗 在日常的各种桌面软件的使用中,我们都会碰到弹窗.例如注册,登录的时候,会有相应的信息弹窗,这里就以信息收集弹窗为例进行弹 ...

  7. 转:Java中finally和return的执行关系

    finally可以分两方面理解 1.执行时机问题.finally总会执行(除非是System.exit()),正常情况下在try后执行,抛异常时在catche后面执行 2.返回值问题.可以认为try( ...

  8. Unix环境编程基础下

    Unix出错处理 当UNIX系统的函数出错时,通常会返回一个负值.我们判断函数的返回值小于0表示出错了,注意我们并不知道为什么出错.例如我们open一个文件,返回值-1表示打开失败,但是为什么打开失败 ...

  9. XJOI1689相连的城市

    相连的城市 n个城市中,某些城市间有道路互相连接.给出与每个城市相邻的城市有多少个,请输出城市间的邻接矩阵. 输入格式: 第一行输入一个正整数n,表示城市的个数. 第二行输入n个用空格隔开的非负整数, ...

  10. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...