#include <stdio.h>
#define RIGHT_SIBLING 0
#define PARENT 1
#define NIL 0
struct node {
int key;
struct node* left_child;
struct node* p;
int flag;
};
int visit_parent(struct node* node);
void visit_children(struct node* node, int children[], int* cp);
void set_node(struct node* node, int key, struct node* left_child, struct node* p, int flag);
void visit(struct node* node);
int visit_parent(struct node* node) {
if (node == NULL)
return NIL;
while (node->flag != PARENT)
node = node->p;
if (node->p == NULL)
return NIL;
else
return node->p->key;
}
void visit_children(struct node* node, int children[], int* cp) {
if (node == NULL)
return;
*cp = 0;
struct node* child = node->left_child;
while (child != NULL) {
children[*cp] = child->key;
*cp = (*cp) + 1;
if (child->flag == RIGHT_SIBLING)
child = child->p;
else
child = NULL;
}
}
void set_node(struct node* node,
int key,
struct node* left_child,
struct node* p,
int flag) {
node->key = key;
node->left_child = left_child;
node->p = p;
node->flag = flag;
}
void visit(struct node* node) {
int parent_key = visit_parent(node);
if (parent_key == NIL)
printf("parent: NIL, children: ");
else
printf("parent: %d, children: ", parent_key);
int children[10] = {NIL};
int count;
int i;
visit_children(node, children, &count);
for (i = 0; i < count; i++)
printf(" %d", children[i]);
printf("\n");
}
/*
* 1
* |
* 2
* | | |
* 3 4 5
*/
void test_1() {
struct node node1, node2, node3, node4, node5;
set_node(&node1, 1, &node2, NULL, PARENT);
set_node(&node2, 2, &node3, &node1, PARENT);
set_node(&node3, 3, NULL, &node4, RIGHT_SIBLING);
set_node(&node4, 4, NULL, &node5, RIGHT_SIBLING);
set_node(&node5, 5, NULL, &node2, PARENT);
visit(&node1);
visit(&node2);
visit(&node3);
visit(&node4);
visit(&node5);
}
int main(int argc, const char *argv[]) {
test_1();
return 0;
}
分享到:
相关推荐
In this, the third edition, we have once again updated the entire book. The changes cover a broad spectrum, including new chapters, revised pseudocode, and a more active writing style.
Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms.-.Introduction.to.Algorithms...
Algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The book contains 244 figures—many with multiple parts—illustrating how ...
《算法导论》第三版是一本广受欢迎的计算机科学教科书,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein共同撰写。本书详细介绍了算法的概念、设计、分析、以及应用,并且被视为计算机...
"Introduction to Algorithms, the 'bible' of the field, is a comprehensive textbook covering the full spectrum of modern algorithms: from the fastest algorithms and data structures to polynomial-time ...
《算法导论》第二版(Introduction_to_Algorithms_2nd_Edition)是计算机科学领域的一本经典教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein四位作者合作撰写,由麻省理工学院...
Mit Press - Introduction To Algorithms 2Nd Edition Incl Exercises Edition.part1
Introduction to Algorithms Second Edition Introduction to Algorithms Second Edition Introduction to Algorithms Second Edition
《Introduction to algorithms THIRD EDITION》(《算法导论 第三版》)是计算机科学领域中一本极具权威性的教科书,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein四位作者共同撰写...
Introduction to Algorithms, Second Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein ISBN:0262032937 The MIT Press © 2001 (1180 pages) A course in computer ...
Introduction to Algorithms, Third Edition [精装] ~ Thomas H. Cormen (作者), Charles E. Leiserson (作者), Ronald L. Rivest (作者), Clifford Stein (作者) 出版社: The MIT Press; 3 (2009年9月30日) 精装...
Algorithms are described in English and in a "pseudocode" designed to be readable by anyone who has done a little programming. The book contains over 230 figures illustrating how the algorithms work. ...
Mit Press - Introduction To Algorithms 2Nd Edition Incl Exercises Edition.part2.rar
Algorithm-Data-Structures-and-Algorithms-in-Java-2nd-Edition-by-Robert-Lafore.zip,Robert Lafore第二版的数据结构与算法,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
《Introduction to Algorithms, Third Edition》是托马斯·H·科曼、查尔斯·E·莱瑟森、罗纳德·L·里维斯特和克莱福德·斯坦共同撰写的一本经典算法教材。这本书自出版以来,就被广泛应用于大学计算机科学专业的...
Introduction to Algorithms, Third Edition 作者: Thomas H.Cormen / Charles E.Leiserson / Ronald L.Rivest / Clifford Stein
Introduction to Genetic Algorithms