C语言之二叉树

规定:根节点的值大于左节点但小于右节点的值,所以二叉树的值插入是唯一的,最后形成的树只跟根节点有关

定义节点:

struct tree_node {
      TypeElem  elem;

      struct tree_node *fa;
      struct tree_node *left;
      struct tree_node *right;
};

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 typedef int TypeElem;
  5 
  6 struct tree_node {
  7     TypeElem  elem;
  8     struct tree_node *fa;
  9     struct tree_node *left;
 10     struct tree_node *right;
 11 };
 12 
 13 struct tree_node *root_ptr;    //指向根节点的指针
 14 
 15 #define IS_ROOT_NODE(ptree_fa, ptree, x)  do{ if(ptree_fa) {   \
 16                                                     if (ptree == ptree_fa->left)   \
 17                                                          ptree_fa->left = x;    \
 18                                                     else if (ptree == ptree_fa->right)   \
 19                                                          ptree_fa->right = x;   \
 20                                           }else                                 \
 21                                               root_ptr = x; }while (0)       //如果删除的是根节点root_ptr就变了
 22 
 23 
 24 /* 在父节点下插入子节点(有可能是孙子节点了) */
 25 struct tree_node *Insert(TypeElem  elem, struct tree_node *T_fa, struct tree_node *T)
 26 {
 27     if (T == NULL) {
 28         T = (struct tree_node *)malloc(sizeof(struct tree_node));
 29         if (!T) {
 30             printf("malloc error\n");
 31             return NULL;
 32         }
 33         T->fa = T_fa;
 34         T->elem = elem;
 35         T->left = T->right = NULL;
 36     }
 37     else if (elem < T->elem)
 38         T->left = Insert(elem, T, T->left);
 39     else if (elem > T->elem)
 40         T->right = Insert(elem, T, T->right);
 41 
 42     return T;
 43 }
 44 
 45 struct tree_node *find_val(struct tree_node *rootp, TypeElem  elem)
 46 {
 47     if (!rootp) {
 48         printf("don't find\n");
 49         return NULL;
 50     }
 51     //遍历:前序、中序、后序
 52     if (rootp->elem == elem)
 53         return rootp;
 54     else if (rootp->elem > elem) {
 55         return find_val(rootp->left, elem);   //递归
 56     }
 57     else if (rootp->elem < elem) {
 58         return find_val(rootp->right, elem);  //递归
 59     }
 60     return NULL;
 61 }
 62 
 63 struct tree_node *find_min_val(struct tree_node *rootp)
 64 {
 65     while (rootp->left) {
 66         rootp = rootp->left;
 67     }
 68     return rootp;
 69 }
 70 
 71 struct tree_node *find_max_val(struct tree_node *rootp)
 72 {
 73     while (rootp->right) {
 74         rootp = rootp->right;
 75     }
 76     return rootp;
 77 }
 78 
 79 int delete_tree(TypeElem  elem, int isfree)
 80 {
 81     struct tree_node *ptree = find_val(root_ptr, elem);
 82     struct tree_node *ptree_fa = ptree->fa;
 83     struct tree_node *min_tree;
 84 
 85     if (!ptree)
 86         return -1;
 87     if (!(ptree->left) && !(ptree->right)) {
 88          IS_ROOT_NODE(ptree_fa, ptree, NULL);
 89     }
 90     else if (!(ptree->left) && ptree->right) {
 91          IS_ROOT_NODE(ptree_fa, ptree, ptree->right);
 92          ptree->right->fa = ptree_fa;
 93     }
 94     else if (ptree->left && !(ptree->right)) {
 95         IS_ROOT_NODE(ptree_fa, ptree, ptree->left);
 96         ptree->left->fa = ptree_fa;
 97     }
 98     else if (ptree->left && ptree->right) {
 99         min_tree = find_min_val(ptree->right);
100         delete_tree(min_tree->elem, 0);   //递归
101         IS_ROOT_NODE(ptree_fa, ptree, min_tree);
102         min_tree->left = ptree->left;
103         min_tree->right = ptree->right;
104         min_tree->fa = ptree->fa;
105     }
106     if (isfree)
107         free(ptree);
108     return 0;
109 }
110 
111 int main()
112 {
113     int i;
114     root_ptr = Insert(6, NULL, root_ptr);    //得到指向根节点的指针 
115 TypeElem a[10] = { 2,8,7,9,0,3,4,1,5 }; //依次插入9个节点 116 for (i = 0; i < 9; i++) 117 Insert(a[i], root_ptr->fa, root_ptr); 118 if (!delete_tree(6, 1)) 119 printf("delete_tree ok\n"); 120 if (root_ptr) { 121 if (find_val(root_ptr, 9)) 122 printf("find node\n"); 123 } 124
125 return 0; 126 }

 

 

形成的树结构:

如果删除根节点6,依然能找到节点9

 

posted @ 2019-03-10 18:30  一条水煮鱼  阅读(435)  评论(0编辑  收藏  举报