有点问题,不细究。
以后填
二叉搜索树定义:左边的小于右边的。
所以无论是增加,查找,删除,都秉承这一点,就可以大大优化速度。
// searchTree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define ERROR printf("out of space!");
struct ElementType;
struct MySearchTree;
typedef struct MySearchTree * Tree;
//函数体
Tree Find(ElementType element, Tree T);
Tree FindMax(Tree T);
Tree FindMin(Tree T);
Tree Insert(ElementType element, Tree T);
Tree Delete(ElementType element, Tree T);
void visit(Tree T, int depth);
struct ElementType
{
int num;
};
struct MySearchTree
{
ElementType element;
Tree left;
Tree right;
};
int main()
{
srand((unsigned)time(NULL));
Tree T = NULL;
struct ElementType em;
em.num = 2500;
T = Insert(em, T);
int *a = (int *)malloc(100 * sizeof(int));
for (int i = 0; i < 100; i++)
{
a[i] = i*i;
}
for (int i = 0; i < 20; i++)
{
int number = rand() % 100;
em.num = a[number];
Insert(em, T);
}
em.num = 99;
Insert(em, T);
visit(T, 0);
printf("MAX:%d\n", FindMax(T)->element.num);
printf("MIN:%d", FindMin(T)->element.num);
Delete(em, T);
printf("\n");
visit(T, 0);
return 0;
}
Tree Find(ElementType element, Tree T)
{
if (T == NULL)
{
return NULL;
}
//注意这里是if else 所以时间复杂度不是很高(即每次递归FInd(right)或者Find(left)只是选择)
if (T->element.num > element.num)
{
return Find(element, T->left);
}
else if (T->element.num < element.num)
{
return Find(element, T->right);
}
else {
return T;
}
}
Tree FindMax(Tree T)
{
if (T == NULL)
{
return NULL;
}
else
{
if (T->right == NULL)
{
return T;
}
else {
return FindMax(T->right);
}
}
}
Tree FindMin(Tree T)
{
if (T == NULL)
{
return NULL;
}
else
{
if (T->left == NULL)
{
return T;
}
else {
return FindMin(T->left);
}
}
}
Tree Insert(ElementType element, Tree T)
{
if (T == NULL)
{
T = (Tree)malloc(sizeof(struct MySearchTree));
T->element = element;
T->left = NULL;
T->right = NULL;
}
else if (T->element.num > element.num)
{
T->left = Insert(element, T->left);
}
else if (T->element.num < element.num)
{
T->right = Insert(element, T->right);
}
return T;
}
void visit(Tree T, int depth)
{
if (T != 0)
{
if (T->element.num != -1)
{
printf("em: %d-------depth:%d\n", T->element.num, depth);
depth++;
visit(T->left, depth);
visit(T->right, depth);
}
if (T->left != NULL)
{
if (T->left->element.num == -1) {
free(T->left);
T->left = NULL;
}
}
if (T->right != NULL)
{
if (T->right->element.num == -1) {
free(T->right);
T->right = NULL;
}
}
}
}
Tree Delete(ElementType element, Tree T)
{
Tree TNode = Find(element, T);
Tree TNode2 = NULL;
//判断是否存在元素
if (TNode == NULL)
{
ERROR
return NULL;
}
else
{
//存在则进行删除操作,这里有三种情况。
//no1.该节点有两个子节点
//no2.该节点有两个以下的子节点
//no3.没有子节点
if (TNode->left == NULL&&TNode->right == NULL)
{
//
//TNode = NULL;
TNode->element.num = -1;
return TNode;
}
if (TNode->left != NULL&&TNode->right != NULL)
{
//两个子节点,则找到右支脉的最小值,代替其
TNode2 = FindMin(TNode->right);
printf("MMMMMIN:%d\n", TNode2->element.num);
TNode->element = TNode2->element;
return Delete(TNode->element, TNode->right);
}
else if (TNode->left != NULL)
{
TNode->element = TNode->left->element;
TNode->left = Delete(TNode->element, TNode->left);
}
else if (TNode->right != NULL)
{
TNode->element = TNode->right->element;
TNode->right = Delete(TNode->element, TNode->right);
}
}
}