回复 20楼 九转星河
嗯……找时间吧。还是那个字……忙。
09:30 05/21 种下琵琶种子,能种活么?等待中……
21:50 05/27 没有发芽。
20:51 05/28 没有发芽。
23:03 05/29 没有发芽。
23:30 06/09 我有预感,要发芽了。
[此贴子已经被作者于2017-5-11 00:28编辑过]
[此贴子已经被作者于2017-5-11 05:49编辑过]
[此贴子已经被作者于2017-5-11 06:18编辑过]
[此贴子已经被作者于2017-5-11 18:32编辑过]
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> #include<assert.h> #define LEN sizeof(TREE) typedef int ElemType; typedef struct TREE { ElemType Value; struct TREE* left; struct TREE* right; struct TREE* prior; }TREE,*PTREE; void Init(PTREE* t); void Creat_Node(void** t,size_t size); int Insert_Value(PTREE* t,ElemType Value); void Insert_Node(PTREE t,ElemType Value); void Pre_Order_Traversal(PTREE t); void In_Order_Traversal(PTREE t); void Post_Order_Traversal(PTREE t); PTREE Find_Min(PTREE t); PTREE Find_Max(PTREE t); PTREE Find_Node(PTREE t,ElemType Value); int Del_Node(PTREE* t,ElemType Value); int Del_Root(PTREE* t); int Del_Left(PTREE* t); int Del_Right(PTREE* t); int Del_Double(PTREE* t); int Visit(PTREE t); void Free_Tree(PTREE* t); void Free_Node(void** t); int main() { PTREE t=NULL; PTREE tm=NULL; int a=0; Init(&t); Pre_Order_Traversal(t); puts(""); In_Order_Traversal(t); puts(""); Post_Order_Traversal(t); puts(""); printf("请输入要删除的节点:"); while ( scanf("%d",&a)!=EOF) { Del_Node(&t,a); Pre_Order_Traversal(t); puts(""); In_Order_Traversal(t); puts(""); Post_Order_Traversal(t); puts(""); printf("请输入要删除的节点:"); } Free_Tree(&t); return 0; } void Init(PTREE* t) { ElemType Value=0; int i=10; srand((unsigned)time(NULL)); while (i--) { Value=rand()%100; Insert_Value(t,Value); } } void Creat_Node(void** t,size_t size) { *t=malloc(size); assert(*t!=NULL); memset(*t,0,size); } int Insert_Value(PTREE* t,ElemType Value) { PTREE* pt=t; PTREE s=NULL; while (*pt!=NULL) if (Value<(*pt)->Value) { s=*pt; pt=&((*pt)->left); } else if (Value>(*pt)->Value) { s=*pt; pt=&((*pt)->right); } else return 0; Creat_Node(pt,LEN); (*pt)->prior=s; Insert_Node(*pt,Value); return 1; } void Insert_Node(PTREE t,ElemType Value) { if (t==NULL) return ; t->Value=Value; } int Visit(PTREE t) { if (t==NULL) return 0; printf("%-4d",t->Value); return 1; } void Pre_Order_Traversal(PTREE t) { if (t==NULL) return ; Visit(t); Pre_Order_Traversal(t->left); Pre_Order_Traversal(t->right); } void In_Order_Traversal(PTREE t) { if (t==NULL) return ; In_Order_Traversal(t->left); Visit(t); In_Order_Traversal(t->right); } void Post_Order_Traversal(PTREE t) { if (t==NULL) return ; Post_Order_Traversal(t->left); Post_Order_Traversal(t->right); Visit(t); } PTREE Find_Min(PTREE t) { if (t==NULL) return t; while (t->left!=NULL) t=t->left; return t; } PTREE Find_Max(PTREE t) { if (t==NULL) return t; while (t->right!=NULL) t=t->right; return t; } PTREE Find_Node(PTREE t,ElemType Value) { if (t==NULL) return t; while (t!=NULL) if (Value<t->Value) t=t->left; else if (Value>t->Value) t=t->right; else return t; return t; } int Del_Node(PTREE* t,ElemType Value) { PTREE pt=*t; if ((pt=Find_Node(*t,Value))==NULL) return 0; if (pt==*t) Del_Root(t); else if (pt->prior->left==pt) Del_Left(&pt); else Del_Right(&pt); return 1; } int Del_Root(PTREE* t) { PTREE ps=NULL; if ((*t)->left==NULL&&(*t)->right==NULL) { Free_Node(t); return 1; } if ((*t)->left!=NULL&&(*t)->right==NULL) { ps=(*t)->left; ps->prior=NULL; Free_Node(t); *t=ps; return 1; } if ((*t)->right!=NULL&&(*t)->left==NULL) { ps=(*t)->right; ps->prior=NULL; Free_Node(t); *t=ps; return 1; } ps=Find_Min((*t)->right); ps->left=(*t)->left; (*t)->left->prior=ps; if (ps->prior==*t) { ps->prior=NULL; Free_Node(t); *t=ps; return 1; } ps->prior->left=ps->right; if (ps->right!=NULL) ps->right->prior=ps->prior; ps->right=(*t)->right; (*t)->right->prior=ps; ps->prior=NULL; Free_Node(t); *t=ps; return 1; } int Del_Left(PTREE* t) { PTREE pt=(*t)->prior; if ((*t)->left==NULL&&(*t)->right==NULL) { pt->left=NULL; Free_Node(t); return 1; } if ((*t)->left==NULL) { pt->left=(*t)->right; (*t)->right->prior=pt; Free_Node(t); return 1; } if ((*t)->right==NULL) { pt->left=(*t)->left; (*t)->left->prior=pt; Free_Node(t); return 1; } Del_Double(t); return 1; } int Del_Right(PTREE* t) { PTREE pt=(*t)->prior; if ((*t)->left==NULL&&(*t)->right==NULL) { pt->right=NULL; Free_Node(t); return 1; } if ((*t)->left==NULL) { pt->right=(*t)->right; (*t)->right->prior=pt; Free_Node(t); return 1; } if ((*t)->right==NULL) { pt->right=(*t)->left; (*t)->left->prior=pt; Free_Node(t); return 1; } Del_Double(t); return 1; } int Del_Double(PTREE* t) { PTREE ps=Find_Min((*t)->right); if ((*t)->prior->left==*t) (*t)->prior->left=ps; else (*t)->prior->right=ps; ps->left=(*t)->left; (*t)->left->prior=ps; if (ps->prior==*t) { ps->prior=(*t)->prior; Free_Node(t); return 1; } ps->prior->left=ps->right; if (ps->right!=NULL) ps->right->prior=ps->prior; ps->right=(*t)->right; (*t)->right->prior=ps; ps->prior=(*t)->prior; Free_Node(t); return 1; } void Free_Tree(PTREE* t) { if (*t==NULL) return ; Free_Tree(&(*t)->left); Free_Tree(&(*t)->right); Free_Node(t); *t=NULL; } void Free_Node(void** t) { if (*t==NULL) return ; free(*t); *t=NULL; }
[此贴子已经被作者于2017-5-12 04:41编辑过]
Insert_Value(&t,INT_MAX); int Visit(PTREE t) { if (t==NULL||t->prior==NULL) return 0; printf("%-4d",t->Value); return 1; }