C语言二叉排序(搜索)树实例

  

本文实例为大家分享了C语言二叉排序(搜索)树实例代码,供大家参考,具体内容如下

     /* * 1。实现了递归非递归插入(创建)二叉排序(搜索)树;   分别对应Insert_BinSNode (TBinSNode * T, int k), NonRecursion_Insert_BinSNode (TBinSNode * T, int k);   2.实现了递归非递归查找二叉排序(搜索)树;   分别对应Find_BinSNode (int TBinSNode * T, s), NonRecursion_Find_BinSNode (int TBinSNode * T, s);   3.实现了非递归删除二叉排序(搜索)树;   分别对应Delete_BinSNode ();   4. 实现了递归先序,中序,后序遍历二叉排序(搜索)树;   分别对应Pre_Print_BinSNode (TBinSNode T) In_Print_BinSNode (TBinSNode T) Post_Print_BinSNode (TBinSNode T);   */# include   # include   typedef struct BinSTreeNode {   int num;   struct BinSTreeNode * lchild, * rchild;   * TBinSNode} BinSNode;   int Empty_Tree (TBinSNode T) {   如果(T) {   返回1;   其他}{   返回0;   }   }/* - - - - - - - - - - - - - - - - - - - - - - - -非递归方法二叉排序树的删除- - - - - - - - - - - - - - - - - - */空白Delete_BinSNode (TBinSNode * T, int del) {   rblast TBinSNode cur, pre, lt;   坏蛋=* T;   pre=零;//如果二叉排序树为空   如果(Empty_Tree(坏蛋)){   printf(“对不起,树是没有”);   返回;   }//如果二叉排序树不为空,先找到即将删除的元素德尔。这里再次实现一遍查找,当然也可以修改一下找到类的函数   而(坏蛋,,坏蛋→num !=▽) {   如果(del> cur→num) {   pre=坏蛋;   坏蛋=cur→rchild;   其他}{   pre=坏蛋;   坏蛋=cur→lchild;   }   }   如果(! cur) {   printf(“对不起,你想要删除的节点,这是不存在的”);   返回;   }   如果(cur→num==▽) {   printf("找到删除节点,等等....... \ n”);   }//如果找到待删除的结点,立刻判断该结点有无左子树//情况一:待删除结点无左子树   如果(!坏蛋→lchild) {   如果(pre==NULL) {   坏蛋=* T;   * T=(* T)→rchild;   免费(坏蛋);   返回;   }   如果(pre→lchild,,前→lchild→num==▽) {   前→lchild=cur→rchild;   免费(坏蛋);   返回;   }   如果(pre→rchild,,前→rchild→num==▽) {   前→rchild=cur→rchild;   免费(坏蛋);   返回;   }   }//情况二:待删除的结点有左子树。   如果(cur→lchild) {   lt=cur→lchild;//情况2.1:该左子树无右子树   如果(! lt→rchild) {   如果(pre→lchild,,前→lchild→num==▽) {   前→lchild=lt;   免费(坏蛋);   返回;   }   如果(pre→rchild,,前→rchild→num==▽) {   前→rchild=lt;   免费(坏蛋);   返回;   }   }//情况2.2:该左子树有右子树   如果(lt→rchild) {   而(lt→rchild) {   rblast=lt;//该左子树中最大的结点的前一个结点。   lt=lt→rchild;   }   坏蛋→num=lt→num;   rblast→rchild=lt→lchild;   免费(lt);   返回;   }   }   }/* - - - - - - - - - - - - - - - - - - - - - -递归方法查找二叉排序树- - - - - - - - - - - - - - - - - - - */空白Find_BinSNode (int TBinSNode T, s) {   如果(s==T→num) {   printf (" % d \ n”T→num);   返回;   }   如果(s> T→num) {   Find_BinSNode (T→rchild,年代);   其他}{   Find_BinSNode (T→lchild,年代);   }   }/* - - - - - - - - - - - - - - - - - - -非递归方法查找二叉排序树- - - - - - - - - - - - - - - - - - - - - */空白NonRecursion_Find_BinSNode (int TBinSNode T, s) {   如果(Empty_Tree (T)) {   printf(“树”);   返回;   }   而(T,,年代!=T→num) {   如果(s> T→num) {   T=T→rchild;   其他}{   T=T→lchild;   }   }   如果(T) {   printf("对不起,没有找到! ");   退出(0);   }   如果(s==T→num) {   printf (" % d \ n”T→num);   }      }/* - - - - - - - - - - - - - - - - - -递归方法创建/插入二叉排序树- - - - - - - - - - - - - - - - - - */空白Insert_BinSNode (TBinSNode * T, int k) {//整数n;   TBinSNode节点;//scanf (“% d”,和n);   如果(Empty_Tree (* T)) {   * T=(TBinSNode) malloc (sizeof (BinSNode));   (* T)→num=k;   (* T)→lchild=(* T)→rchild=零;   返回;   其他}{   如果(k> (* T)→num) {   Insert_BinSNode (, (* T)→rchild, k);   其他}{   Insert_BinSNode (, (* T)→lchild, k);   }   }   }/* - - - - - - - - - - - - - - - - - - - - - - -先序遍历二叉排序树- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */空白Pre_Print_BinSNode (TBinSNode T) {   如果(T) {   printf (" % d, T→num);   Pre_Print_BinSNode (T→lchild);   Pre_Print_BinSNode (T→rchild);   }   }/* - - - - - - - - - - - - - - - - - - - - - - - -中序遍历二叉排序树- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */空白In_Print_BinSNode (TBinSNode T) {   如果(T) {   In_Print_BinSNode (T→lchild);   printf (" % d, T→num);   In_Print_BinSNode (T→rchild);   }   }/* - - - - - - - - - - - - - - - - - - - - - - - -后序遍历二叉排序树- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */空白Post_Print_BinSNode (TBinSNode T) {   如果(T) {   Post_Print_BinSNode (T→lchild);   Post_Print_BinSNode (T→rchild);   printf (" % d, T→num);   }   }/* - - - - - - - - - - - - - - - - - - - - - - - -非递归创建/插入二叉排序树- - - - - - - - - - - - - - - - - - - - - - - - - - - */空白NonRecursion_Insert_BinSNode (TBinSNode * T, int k) {//如果为空的二叉排序树   TBinSNode cur, p、t;   t=* t;   如果(! * T) {   * T=(TBinSNode) malloc (sizeof (BinSNode));   (* T)→num=k;   (* T)→lchild=(* T)→rchild=零;   返回;   其他}{//二叉排序树不为空。   而(t) {   如果(k> t→num) {   坏蛋=t;   t=t→rchild;   其他}{   坏蛋=t;   t=t→lchild;   }   }   p=(TBinSNode) malloc (sizeof (BinSNode));   p→num=k;   p→lchild=p→rchild=零;   如果(k> cur→num) {   坏蛋→rchild=p;   }   如果(k

C语言二叉排序(搜索)树实例