这篇文章介绍的是经典的数据结构--二叉树,在这篇文章里介绍了几乎二叉树的所有操作。
二叉树给我们最重要的印象莫过于递归,因为这棵树就是递归的,所以,我在解决各个问题时大部分都用到了递归,代码简单且易于理解,好吧,这篇文章的代码有点长,贴出来吧:
头文件:
/* * dlut_bitree.h * * Created on: 2014年1月13日 * Author: DLUTBruceZhang */ #ifndef DLUT_BITREE_H_ #define DLUT_BITREE_H_ #include <stdio.h> typedef int need; #define EMPTY 1 #define NOT_EMPTY 0 typedef struct _bitree { need data; struct _bitree *lchild; struct _bitree *rchild; }bitree; void dlut_bitree_init(bitree **t); void dlut_bitree_create(bitree **t); int dlut_bitree_empty(bitree *t); void dlut_bitree_recursion_preorder_traverse(bitree *t); void dlut_bitree_recursion_inorder_traverse(bitree *t); void dlut_bitree_recursion_backorder_traverse(bitree *t); void dlut_bitree_preorder_traverse(bitree *t); void dlut_bitree_inorder_traverse(bitree *t); void dlut_bitree_backorder_traverse(bitree *t); void dlut_bitree_backorder2_traverse(bitree *t); void dlut_bitree_level_traverse(bitree *t); int dlut_bitree_node_count(bitree *t); int dlut_bitree_leaf_node_count(bitree *t); int dlut_bitree_depth(bitree *t); void dlut_bitree_find_data(bitree *t, need, bitree **); int dlut_bitree_find_max(bitree *t); int dlut_bitree_find_min(bitree *t); void dlut_bitree_find_its_lchild(bitree *t, need); void dlut_bitree_find_its_rchild(bitree *t, need); void dlut_bitree_find_its_parent(bitree *t, need); void dlut_bitree_find_its_lbrother(bitree *t, need); void dlut_bitree_find_its_rbrother(bitree *t, need); bitree * dlut_bitree_copy_the_bitree(bitree *t); void dlut_bitree_exchange_l_r(bitree **t); int dlut_bitree_is_equal(bitree *t1, bitree *t2); void dlut_bitree_destory_left(bitree **t); void dlut_bitree_destory_right(bitree **t); void dlut_bitree_destory(bitree **t); #endif /* DLUT_BITREE_H_ */
C文件:
/* * dlut_bitree.c * * Created on: 2014年1月13日 * Author: DLUTBruceZhang */ #include "dlut_bitree.h" #include "dlut_stack.h" #include <stdlib.h> void dlut_bitree_init(bitree **t) { *t = NULL; return; } void dlut_bitree_create(bitree **t) { need data; printf("please input the prev data : \n"); scanf("%d", &data); if (data == -1) *t = NULL; else { *t = (bitree *)malloc(sizeof(bitree)); if (!*t) return; (*t) -> data = data; dlut_bitree_create(&((*t) -> lchild)); dlut_bitree_create(&((*t) -> rchild)); } } int dlut_bitree_empty(bitree *t) { return t == NULL ? EMPTY : NOT_EMPTY; } void dlut_bitree_recursion_preorder_traverse(bitree *t) { if (t) { printf("%d ", t -> data); dlut_bitree_recursion_preorder_traverse(t -> lchild); dlut_bitree_recursion_preorder_traverse(t -> rchild); } return; } void dlut_bitree_recursion_inorder_traverse(bitree *t) { if (t) { dlut_bitree_recursion_inorder_traverse(t -> lchild); printf("%d ", t -> data); dlut_bitree_recursion_inorder_traverse(t -> rchild); } return; } void dlut_bitree_recursion_backorder_traverse(bitree *t) { if (t) { dlut_bitree_recursion_backorder_traverse(t -> lchild); dlut_bitree_recursion_backorder_traverse(t -> rchild); printf("%d ", t -> data); } return; } void dlut_bitree_preorder_traverse(bitree *t) { bitree *stack[20]; int top = -1; bitree *_t = t; while (_t || top != -1) { while (_t) { printf("%d ", _t -> data); stack[++top] = _t; _t = _t -> lchild; } if (top != -1) { _t = stack[top--]; _t = _t -> rchild; } } printf("\n"); return; } void dlut_bitree_inorder_traverse(bitree *t) { bitree *stack[20]; int top = -1; bitree *_t = t; while (_t || top != -1) { while (_t) { stack[++top] = _t; _t = _t -> lchild; } if (top != -1) { _t = stack[top--]; printf("%d ", _t -> data); _t = _t -> rchild; } } printf("\n"); return; } void dlut_bitree_backorder_traverse(bitree *t) { bitree *_t = t; bitree *have_visited = NULL; bitree *stack[20]; int top = -1; while (_t || top != -1) { while (_t) { stack[++top] = _t; _t = _t -> lchild; } _t = stack[top]; if (_t -> rchild == NULL || _t -> rchild == have_visited) { printf("%d ", _t -> data); have_visited = _t; top--; _t = NULL; } else { _t = _t -> rchild; } } printf("\n"); return; } void dlut_bitree_backorder2_traverse(bitree *t) { bitree *_t = t; bitree *stack1[20], *stack2[20]; int top1 = -1, top2 = -1; stack1[++top1] = _t; while (top1 != -1) { _t = stack1[top1--]; stack2[++top2] = _t; if (_t -> lchild) { stack1[++top1] = _t -> lchild; } if (_t -> rchild) { stack1[++top1] = _t -> rchild; } } while (top2 != -1) { printf("%d ", stack2[top2--] -> data); } printf("\n"); return; } void dlut_bitree_level_traverse(bitree *t) { bitree *_t = t; bitree *queue[20]; int count = -1; int cur_pos = -1; if (_t) { printf("%d ", _t -> data); queue[++count] = _t; } while (count != cur_pos) { _t = queue[++cur_pos]; if (_t -> lchild) { printf("%d ", _t -> lchild -> data); queue[++count] = _t -> lchild; } if (_t -> rchild) { printf("%d ", _t -> rchild -> data); queue[++count] = _t -> rchild; } } printf("\n"); return; } int dlut_bitree_node_count(bitree *t) { static int count = 0; if (t) { count++; dlut_bitree_node_count(t -> lchild); dlut_bitree_node_count(t -> rchild); } return count; } int dlut_bitree_leaf_node_count(bitree *t) { static int count = 0; if (t) { if (!t -> lchild && !t -> rchild) count++; dlut_bitree_leaf_node_count(t -> lchild); dlut_bitree_leaf_node_count(t -> rchild); } return count; } int dlut_bitree_depth(bitree *t) { int d1 = 0, d2 = 0; if (!t) return 0; d1 = dlut_bitree_depth(t -> lchild); d2 = dlut_bitree_depth(t -> rchild); return d1 > d2 ? d1 + 1 : d2 + 1; } void dlut_bitree_find_data(bitree *t, need data, bitree **td) { if (!t) return ; else { if (t -> data == data) { *td = t; } dlut_bitree_find_data(t -> lchild, data, td); dlut_bitree_find_data(t -> rchild, data, td); } } int dlut_bitree_find_max(bitree *t) { static int max; static int flag = 0; if (!t) return -1; if (!flag || t) { if (!flag) { max = t -> data; flag++; } if (t -> data > max) { max = t -> data; } dlut_bitree_find_max(t -> lchild); dlut_bitree_find_max(t -> rchild); } return max; } int dlut_bitree_find_min(bitree *t) { static int min; static int flag; if (!flag || t) { if (!flag) { min = t -> data; flag++; } if (t -> data < min) { min = t -> data; } dlut_bitree_find_min(t -> lchild); dlut_bitree_find_min(t -> rchild); } return min; } void dlut_bitree_find_its_lchild(bitree *t, need data) { if (!t) { return ; } if (t -> data == data) { if (t -> lchild) { printf("%d ‘s lchild is %d\n", data, t -> lchild -> data); } else { printf("%d don‘t have lchild\n", data); } } dlut_bitree_find_its_lchild(t -> lchild, data); dlut_bitree_find_its_lchild(t -> rchild, data); return; } void dlut_bitree_find_its_rchild(bitree *t, need data) { if (!t) { return ; } if (t -> data == data) { if (t -> rchild) { printf("%d ‘s rchild is %d\n", data, t -> rchild -> data); } else { printf("%d don‘t have rchild\n", data); } } dlut_bitree_find_its_rchild(t -> lchild, data); dlut_bitree_find_its_rchild(t -> rchild, data); return; } void dlut_bitree_find_its_parent(bitree *t, need data) { if (!t) { return; } if (t -> lchild || t -> rchild) { if (t -> lchild && t -> lchild -> data == data) { printf("%d ‘s parent is %d\n", data, t -> data); } if (t -> rchild && t -> rchild -> data == data) { printf("%d ‘s parent is %d\n", data, t -> data); } } dlut_bitree_find_its_parent(t -> lchild, data); dlut_bitree_find_its_parent(t -> rchild, data); } void dlut_bitree_find_its_lbrother(bitree *t, need data) { if (!t) { return; } if (t -> rchild && t -> rchild -> data == data) { if (t -> lchild) { printf("%d ‘s lbrother is %d\n", data, t -> lchild -> data); } else { printf("%d don‘t have lbrother\n", data); } } dlut_bitree_find_its_lbrother(t -> lchild, data); dlut_bitree_find_its_lbrother(t -> rchild, data); } void dlut_bitree_find_its_rbrother(bitree *t, need data) { if (!t) { return; } if (t -> lchild && t -> lchild -> data == data) { if (t -> rchild) { printf("%d ‘s rbrother is %d\n", data, t -> rchild -> data); } else { printf("%d don‘t have rbrother\n", data); } } dlut_bitree_find_its_rbrother(t -> lchild, data); dlut_bitree_find_its_rbrother(t -> rchild, data); } bitree *dlut_bitree_copy_the_bitree(bitree *t) { bitree *_t, *lchild, *rchild; if (!t) return NULL; lchild = dlut_bitree_copy_the_bitree(t -> lchild); rchild = dlut_bitree_copy_the_bitree(t -> rchild); _t = (bitree *)malloc(sizeof(bitree)); _t -> data = t -> data; _t -> lchild = lchild; _t -> rchild = rchild; return _t; } void dlut_bitree_exchange_l_r(bitree **t) { bitree *tmp; if (t) { dlut_bitree_exchange_l_r(&(*t) -> lchild); dlut_bitree_exchange_l_r(&(*t) -> rchild); tmp = (*t) -> lchild; (*t) -> lchild = (*t) -> rchild; (*t) -> rchild = tmp; } return; } int dlut_bitree_is_equal(bitree *t1, bitree *t2) { int _t1, _t2; if (t1 == NULL && t2 == NULL) { return 1; } else if (t1 == NULL || t2 == NULL) { return 0; } else { _t1 = dlut_bitree_is_equal(t1 -> lchild, t2 -> lchild); _t2 = dlut_bitree_is_equal(t1 -> rchild, t2 -> rchild); return _t1 && _t2; } } void dlut_bitree_destory_left(bitree **t) { if (!t) { return; } dlut_bitree_destory_left(&((*t) -> lchild)); free(*t); (*t) -> lchild = NULL; return; } void dlut_bitree_destory_right(bitree **t) { if (!t) { return; } dlut_bitree_destory_right(&((*t) -> lchild)); free(*t); (*t) -> rchild = NULL; return; } void dlut_bitree_destory(bitree **t) { if (*t) { dlut_bitree_destory(&((*t) -> lchild)); dlut_bitree_destory(&((*t) -> rchild)); free(*t); *t = NULL; } return; }