树的基本操作
来源:互联网 发布:react browser.js下载 编辑:程序博客网 时间:2024/06/06 00:08
/* *tree.h *Compile tree.c and iltree.c *void create(tree_pointer ptr, int i)是手工写的一棵满二叉树,结点数为15 *Date: 2012/12/12 *NolanJian */#ifndef TREE#define TREE#include<stdio.h>#include<string>#define MAX_STACK_SIZE 100#define MAX_QUEUE_SIZE 100struct node;typedef struct node Node;typedef Node *tree_pointer;void create(tree_pointer ptr, int i);void preorder(tree_pointer ptr);void postorder(tree_pointer ptr);void inorder(tree_pointer ptr);void add(int* top, tree_pointer item);tree_pointer Delete(int* top);void iter_inorder(tree_pointer node);void addq(int front, int* rear, tree_pointer item);tree_pointer deleteq(int* front, int rear);void level_order(tree_pointer p);void iter_preorder(tree_pointer node);void show(void);struct node {int data;tree_pointer left_child, right_child;};#endif/*tree.h*/
/*tree.c *Date: 2012/12/12 *NolanJian */#include"tree.h"tree_pointer stack[MAX_STACK_SIZE];tree_pointer queue[MAX_QUEUE_SIZE];void preorder(tree_pointer p) {if(p) {printf("%d\n", p->data);preorder(p->left_child);preorder(p->right_child);}}void inorder(tree_pointer p) {if(p) {inorder(p->left_child);printf("%d\n", p->data);inorder(p->right_child);}}void postorder(tree_pointer p) {if(p) {postorder(p->left_child);postorder(p->right_child);printf("%d\n", p->data);}}void iter_inorder(tree_pointer node) {int top = -1;for(;;) {for(; node; node = node->left_child)add(&top, node); /*add to stack*/node = Delete(&top); /*delete from stack*/if(!node) break; /*empty stack*/printf("%d\n", node->data);node = node->right_child;}}void level_order(tree_pointer p) {int front = 0, rear = 0;if(!p) return; /*empty tree*/addq(front, &rear, p);for(;;) {p = deleteq(&front, rear);if(p) {printf("%d\n", p->data);if(p->left_child)addq(front, &rear, p->left_child);if(p->right_child)addq(front, &rear, p->right_child);}else break;}}void create(tree_pointer p, int i) /*create a tree*/ {if(15 > 2 * i) {p->left_child = (tree_pointer)malloc(sizeof(Node));p->left_child->data = 2 * i;p->right_child = (tree_pointer)malloc(sizeof(Node));p->right_child->data = 2 * i + 1;create(p->left_child, 2 * i);create(p->right_child, 2 * i + 1);}else {p->left_child = NULL;p->right_child = NULL;}}void add(int* top, tree_pointer item) {if(*top >= MAX_STACK_SIZE - 1) {return ;}stack[++*top] = item;}tree_pointer Delete(int* top) {if(*top == -1)return NULL;return stack[(*top)--];}void addq(int front, int* rear, tree_pointer item) {*rear = (*rear + 1) % MAX_QUEUE_SIZE;if(front == *rear) {return; /*full*/}queue[*rear] = item;}void iter_preorder(tree_pointer node) {int top = -1;for(;;) {for(; node; node = node->left_child) {printf("%d\n", node->data);if(node->right_child)add(&top, node->right_child);}node = Delete(&top);if(!node) break; /*如果树是空的,结束*/}}tree_pointer deleteq(int* front, int rear) {tree_pointer item;if(*front == rear)return NULL; /*empty queue*/*front = (*front + 1) % MAX_QUEUE_SIZE;return queue[*front];}void show(void) {Node root;tree_pointer p;root.data = 1;p = &root;create(p, 1);p = &root;printf("preorder:\n");preorder(p);p = &root;printf("inorder:\n");inorder(p);p = &root;printf("postorder:\n");postorder(p);p = &root;printf("iter_inorder:\n");iter_inorder(p);p = &root;printf("level_order:\n");level_order(p);printf("iter_preorder\n");p = &root;iter_preorder(p);} /*tree.c*/
/*iltree.c *Date: 2012/12/12 *NolanJian */#include"tree.h"int main() {show();return 0;}/*iltree.c*/
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 二叉树的基本操作
- 平衡树的基本操作
- 爱是瞬间的美,情是永恒的痛
- 易语言中的静态变量是什么意思
- PInvoke调用导致堆栈不对称 c#调用C++win32非托管dll的问题深度分析
- mx51 uboot启动感悟
- 爱是瞬间的美,情是永恒的痛
- 树的基本操作
- 用OleDb读取Excel数据
- 领域驱动设计之代码优先-领域层设计-4 (翻译)
- 使用JFreeChart来创建基于web的图表
- configure: error: no acceptable C compiler found in $PATH 问题解决
- ZOJ 1315 Excuses, Excuses!
- Linux中的中断处理
- printf("%d%d%d%d\n", a,b,c);
- 爱是瞬间的美,情是永恒的痛