记录失败的二叉搜索树的操作集代码。呜呜做了挺久的,感觉没啥问题但一个测试点都没过,全是段错误,我服了呜呜。
BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); Position FindMin( BinTree BST ); Position FindMax( BinTree BST ); BinTree Insert( BinTree BST, ElementType X ) { BinTree pre=NULL; BinTree work=BST; if(BST==NULL) { BST->Data=X; return BST; } while(work!=NULL) { if(X>work->Data) { pre=work; work=work->Right; } else if(X<work->Data) { pre=work; work=work->Left; } else { return; } } if(X>pre->Data) { // BinTree node=(BinTree)malloc(sizeof(struct TNode)); // node->Data=X; pre->Right=(BinTree)malloc(sizeof(struct TNode)); pre->Right->Right=NULL; pre->Right->Left=NULL; // node->Right=NULL; //node->Left=NULL; return BST; } if(X<pre->Data) { // BinTree node=(BinTree)malloc(sizeof(struct TNode)); // node->Data=X; pre->Left=(BinTree)malloc(sizeof(struct TNode)); pre->Left->Right=NULL; pre->Left->Left=NULL; // node->Right=NULL; //node->Left=NULL; return BST; } } BinTree Delete( BinTree BST, ElementType X ) { BinTree ghost; BinTree ghostpar; ghost=BST; while(ghost!=NULL) { if(X==BST->Data&&BST->Left==NULL&&BST->Right==NULL)//根节点为叶子节点 { BST=NULL; } if(X<ghost->Data)//小于就进左数 { ghostpar=ghost; ghost=ghost->Left; if(ghost->Data==X) { if(ghost->Left==NULL&&ghost->Right==NULL)//被删除节点是叶子节点 { ghost=NULL; } if(ghost->Right==NULL&&ghost->Left!=NULL)//被删除节点有左孩子无右孩子 { ghostpar->Left=ghost->Left; ghost=NULL; } if(ghost->Left==NULL&&ghost->Right!=NULL)//被删除节点有右孩子无左孩子 { ghostpar->Left=ghost->Right; ghost=NULL; } } } if(X>ghost->Data)//大于就进右树 { ghostpar=ghost; ghost=ghost->Right; if(ghost->Data==X) { if(ghost->Left==NULL&ghost->Right==NULL)//被删除为叶子节点 { ghost=NULL; } if(ghost->Right==NULL&&ghost->Left!=NULL)//被删除节点有左孩子无右孩子 { ghostpar->Right=ghost->Left; ghost=NULL; } if(ghost->Left==NULL&&ghost->Right!=NULL)//被删除节点有右孩子无左孩子 { ghostpar->Right=ghost->Right; ghost=NULL; } } } } //被删除节点有左右孩子 ghost=BST; BinTree node; BinTree par; while(ghost!=NULL) { if(BST->Data==X&&BST->Left!=NULL&&BST->Right!=NULL) { while(ghost!=NULL)//找到左树的最大节点 { ghostpar=ghost; ghost=ghost->Right; } if(ghost->Left!=NULL)//左数最大节点有左节点 { ghostpar->Right=ghost->Left; } BST->Data=ghost->Data;//交换节点值并重连节点 ghost->Left=BST->Left; ghost->Right=BST->Right; } else { if(X>ghost->Data) { ghostpar=ghost; ghost=ghost->Right; if(X==ghost->Data) { node=ghost; par=ghostpar; while(ghost!=NULL)//找到左树的最大节点 { ghostpar=ghost; ghost=ghost->Right; } if(ghost->Left!=NULL) { ghostpar->Right=ghost->Left; } par->Right=ghost; ghost->Left=node->Left; ghost->Right=node->Right; } } if(X<ghost->Data) { ghostpar=ghost; ghost=ghost->Left; if(X==ghost->Data) { node=ghost; par=ghostpar; while(ghost->Right!=NULL)//找到左树的最大节点 { ghostpar=ghost; ghost=ghost->Right; } if(ghost->Left!=NULL) { ghostpar->Right=ghost->Left; } par->Left=ghost; ghost->Left=node->Left; ghost->Right=node->Right; } } } } } Position Find( BinTree BST, ElementType X ) { BinTree ghost=BST; BinTree ghostpar; if(BST->Data==X) { return BST; } else { while(ghost!=NULL) { if(X>ghost->Data) { ghostpar=ghost; ghost=ghost->Right; if(ghost->Data==X) { return ghost; } } if(X<ghost->Data) { ghostpar=ghost; ghost=ghost->Left; if(ghost->Data==X) { return ghost; } } } } return NULL; } Position FindMin( BinTree BST ) { if(BST==NULL||BST->Left==NULL) { return BST; } else { BinTree min; min=BST; while(min->Left!=NULL) { min=min->Left; } return min; } } Position FindMax( BinTree BST ) { if(BST==NULL||BST->Right==NULL) { return BST; } else { BinTree max; max=BST; while(max->Right!=NULL) { max=max->Right; } return max; } }
记录失败的二叉搜索树的操作集代码
最新推荐文章于 2024-05-31 12:03:11 发布