#include <stdio.h>
#define TRAVERSE 0
#define LEFT_BACKTRACK 1
#define RIGHT_BACKTRACK 2
struct node {
int key;
struct node* p;
struct node* left;
struct node *right;
};
void traverse(struct node* node);
void set_node(struct node* node, int key, struct node* p, struct node* left,
struct node* right);
void set_node(struct node* node, int key, struct node* p, struct node* left,
struct node* right) {
node->key = key;
node->p = p;
node->left = left;
node->right = right;
}
void traverse(struct node* node) {
int pos = TRAVERSE;
printf("Traversing tree...\n");
while (node != NULL) {
if (pos == TRAVERSE)
printf("node.key: %d\n", node->key);
if (node->left != NULL && pos == TRAVERSE) {
pos = TRAVERSE;
node = node->left;
} else if (node->right != NULL && (pos == TRAVERSE || pos ==
LEFT_BACKTRACK)) {
pos = TRAVERSE;
node = node->right;
} else if (node->p != NULL) {
pos = node->p->left == node ? LEFT_BACKTRACK : RIGHT_BACKTRACK;
node = node->p;
} else {
node = NULL;
}
}
}
/*
* Tree:
* 1
*/
void test_1() {
struct node node1;
set_node(&node1, 1, NULL, NULL, NULL);
traverse(&node1);
}
/*
* Tree:
* 1
* / \
* 2 3
*/
void test_2() {
struct node node1;
struct node node2;
struct node node3;
set_node(&node1, 1, NULL, &node2, &node3);
set_node(&node2, 2, &node1, NULL, NULL);
set_node(&node3, 3, &node1, NULL, NULL);
traverse(&node1);
}
/*
* Tree:
* 1
* / \
* 2 3
* / \ \
* 4 5 6
*/
void test_3() {
struct node node1;
struct node node2;
struct node node3;
struct node node4;
struct node node5;
struct node node6;
set_node(&node1, 1, NULL, &node2, &node3);
set_node(&node2, 2, &node1, &node4, &node5);
set_node(&node3, 3, &node1, NULL, &node6);
set_node(&node4, 4, &node2, NULL, NULL);
set_node(&node5, 5, &node2, NULL, NULL);
set_node(&node6, 6, &node3, NULL, NULL);
traverse(&node1);
}
/*
* Tree:
* 1
* / \
* / \
* / \
* 2 3
* \ / \
* \ / \
* 4 5 6
* / \ \ /
* 7 8 9 10
* /
* 11
*/
void test_4() {
struct node node1, node2, node3, node4, node5, node6, node7, node8, node9,
node10, node11;
set_node(&node1, 1, NULL, &node2, &node3);
set_node(&node2, 2, &node1, NULL, &node4);
set_node(&node3, 3, &node1, &node5, &node6);
set_node(&node4, 4, &node2, &node7, &node8);
set_node(&node5, 5, &node3, NULL, &node9);
set_node(&node6, 6, &node3, &node10, NULL);
set_node(&node7, 7, &node4, NULL, NULL);
set_node(&node8, 8, &node4, NULL, NULL);
set_node(&node9, 9, &node5, &node11, NULL);
set_node(&node10, 10, &node6, NULL, NULL);
set_node(&node11, 11, &node9, NULL, NULL);
traverse(&node1);
}
int main(int argc, const char *argv[]) {
test_1();
test_2();
test_3();
test_4();
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.... - Charles E.... - Ronald L.... - Clifford Stein:哥伦比亚大学工业工程与运筹学副教授。...
- **书名**:《算法导论》第三版(Introduction to Algorithms, Third Edition) - **作者**:Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein - **出版社**:MIT Press - **出版日期**...
《算法导论》第三版是一本广受欢迎的计算机科学教科书,由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四位作者共同撰写...
Fulkerson style “augmenting paths.” We benchmark these algorithms on a number of typical graphs in the contexts of image restoration, stereo, and segmentation. In many cases, our new algorithm works...
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·里维斯特和克莱福德·斯坦共同撰写的一本经典算法教材。这本书自出版以来,就被广泛应用于大学计算机科学专业的...