#pragma once
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef int BSTDataType;
typedef struct BSTreeNode
{
struct BSTreeNode* _left;
struct BSTreeNode* _right;
BSTDataType _data;
}BSTreeNode;
///
// 非递归
BSTreeNode* BuyBSTreeNode(BSTDataType x);
int BSTreeInsert(BSTreeNode** pptree, BSTDataType x);
const BSTreeNode* BSTreeFind(BSTreeNode** pptree, BSTDataType x);
int BSTreeRemove(BSTreeNode** pptree, BSTDataType x);
void BSTreeInOrder(BSTreeNode** pptree);
///
// 递归
int BSTreeInsertR(BSTreeNode** pptree, BSTDataType x);
const BSTreeNode* BSTreeFindR(BSTreeNode** pptree, BSTDataType x);
int BSTreeRemoveR(BSTreeNode** pptree, BSTDataType x);
#include"BinarySearchTree.h"
///
// 非递归
BSTreeNode* BuyBSTreeNode(BSTDataType x)
{
BSTreeNode * node = (BSTreeNode *)malloc(sizeof(BSTreeNode));
node->_left = NULL;
node->_right = NULL;
node->_data = x;
return node;
}
int BSTreeInsert(BSTreeNode** pptree, BSTDataType x)
{
BSTreeNode* cur, *parent=NULL;
if (*pptree== NULL)
{
*pptree= BuyBSTreeNode(x);
return 1;
}
cur = *pptree;
while (cur)
{
if (cur->_data > x)
{
parent = cur;
cur = cur->_left;
}
else if (cur->_data < x)
{
parent = cur;
cur = cur->_right;
}
else
{
return 0;
}
}
if (parent->_data < x)
{
(parent)->_right = BuyBSTreeNode(x);
}
else
{
parent->_left = BuyBSTreeNode(x);
}
return 1;
}
const BSTreeNode* BSTreeFind(BSTreeNode** pptree, BSTDataType x)
{
BSTreeNode *cur = *pptree;
while (cur)
{
if (cur->_data > x)
{
cur = cur->_left;
}
else if (cur->_data<x)
{
cur = cur->_right;
}
else
{
return cur;
}
}
return NULL;
}
int BSTreeRemove(BSTreeNode** pptree, BSTDataType x)
{
BSTreeNode * cur, *parent;
parent = NULL;
cur = *pptree;
while (cur)
{
if (cur->_data > x)
{
cur = cur->_left;
}
else if (cur->_data<x)
{
cur = cur->_right;
}
else
{
//开始删除
//左为空/右为空
//左右都不为空
if (cur->_left == NULL)
{
if (parent == NULL)
{
*pptree= cur->_right;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_right;
}
else
{
parent->_right = cur->_right;
}
}
}
else if (cur->_right == NULL)
{
if (parent == NULL)
{
*pptree= cur->_left;
}
else
{
if (cur == parent->_left)
{
parent->_left = cur->_left;
}
else
{
parent->_right = cur->_left;
}
}
}
//两个孩子都不为空
else
{
BSTreeNode * replace = cur->_right;
while (replace->_left)
{
replace = replace->_left;
}
cur->_data = replace->_data;
return BSTreeRemove(&cur->_right, replace->_data);
}
free(cur);
return 1;
}
}
return 0;
}
void BSTreeInOrder(BSTreeNode** pptree)
{
if (*pptree== NULL)
{
return;
}
BSTreeInOrder(&(*pptree)->_left);
printf("%d", (*pptree)->_data);
BSTreeInOrder(&(*pptree)->_right);
}
///
// 递归
int BSTreeInsertR(BSTreeNode** pptree, BSTDataType x)
{
if (*pptree== NULL)
{
*pptree= BuyBSTreeNode(x);
return 1;
}
if ((*pptree)->_data > x)
return BSTreeInsertR(&(*pptree)->_left, x);
else if ((*pptree)->_data < x)
return BSTreeInsertR(&(*pptree)->_right, x);
else
return 0;
}
const BSTreeNode* BSTreeFindR(BSTreeNode** pptree, BSTDataType x)
{
if (*pptree== NULL)
{
return NULL;
}
if ((*pptree)->_data > x)
{
return BSTreeFindR(&(*pptree)->_left, x);
}
else if ((*pptree)->_data < x)
{
return BSTreeFindR(&(*pptree)->_right, x);
}
else
{
return *pptree;
}
}
int BSTreeRemoveR(BSTreeNode** pptree, BSTDataType x)
{
if (*pptree== NULL)
{
return -1;
}
if ((*pptree)->_data < x)
{
return BSTreeRemoveR((&(*pptree)->_right), x);
}
else if ((*pptree)->_data > x)
{
return BSTreeRemoveR((&(*pptree)->_left), x);
}
else
{
BSTreeNode* del = *pptree;
if ((*pptree)->_left == NULL)
{
*pptree= (*pptree)->_right;
free(del);
}
else if ((*pptree)->_right == NULL)
{
*pptree= (*pptree)->_left;
free(del);
}
else
{
BSTreeNode* replace = (*pptree)->_right;
while (replace->_left)
{
replace = replace->_left;
}
(*pptree)->_data = replace->_data;
return BSTreeRemoveR(&(*pptree)->_right, replace->_data);
}
}
return 1;
}
#include"BinarySearchTree.h"
void TestBSTree()
{
int i = 0;
BSTreeNode* ppTree = NULL;
int a[] = {5,3,4,1,7,8,2,6,0,9};
for (; i < sizeof(a)/sizeof(int); ++i)
{
BSTreeInsert(&ppTree, a[i]);
}
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 2);
BSTreeRemove(&ppTree, 1);
BSTreeRemove(&ppTree, 8);
BSTreeRemove(&ppTree, 5);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 6);
BSTreeRemove(&ppTree, 5);
BSTreeRemove(&ppTree, 0);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 1);
BSTreeInOrder(&ppTree);
printf("\n");
/*BSTreeRemove(&ppTree, 2);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 3);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 4);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 5);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 6);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 7);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 8);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 9);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeRemove(&ppTree, 10);
BSTreeInOrder(&ppTree);
printf("\n");
BSTreeInOrder(&ppTree);
printf("\n");*/
}
void TestBSTreeR()
{
int i = 0;
BSTreeNode* ppTree = NULL;
int a[] = {5,3,4,1,7,8,2,6,0,9};
for (; i < sizeof(a)/sizeof(int); ++i)
{
BSTreeInsertR(&ppTree, a[i]);
}
BSTreeRemoveR(&ppTree, 0);
BSTreeRemoveR(&ppTree, 1);
BSTreeRemoveR(&ppTree, 2);
BSTreeRemoveR(&ppTree, 3);
BSTreeRemoveR(&ppTree, 4);
BSTreeRemoveR(&ppTree, 5);
BSTreeRemoveR(&ppTree, 6);
BSTreeRemoveR(&ppTree, 7);
BSTreeRemoveR(&ppTree, 8);
BSTreeRemoveR(&ppTree, 9);
BSTreeRemoveR(&ppTree, 10);
BSTreeInOrder(&ppTree);
printf("\n");
}
int main()
{
TestBSTree();
//TestBSTreeR();
system("pause");
return 0;
}