浙大数据结构习题笔记:二叉搜索树

本文深入探讨了二叉搜索树的基本操作,包括查找、插入、删除等关键算法,并通过递归与循环方式实现了查找最小值和最大值的功能。文章还提供了完整的C++代码实现,帮助读者理解二叉搜索树的内部机制。

二叉搜索树

感觉FindTailFindFor部分返回的内容不太好输出…

#include <iostream>
#include <malloc.h>
using namespace std;

typedef int ElemType;

typedef struct TreeNode *BinTree;
struct TreeNode{
    ElemType data;
    BinTree left;
    BinTree right;
};

BinTree FindTail(ElemType x,BinTree BST)
{
    if(!BST){
        return NULL;
    }
    if(x > BST->data){
        //尾递归
        return FindTail(x,BST->right);
    }else if(x < BST->data){
        return FindTail(x,BST->left);
    }else{
        return BST;
        //找到
    }
}

BinTree FindFor(ElemType x,BinTree BST)
{
    while(!BST){
        if(x > BST->data){
            BST = BST->right;
        }else if(x < BST->data){
            BST = BST->left;
        }else{
            return BST;
        }
    }
    return NULL;
}

//递归和循环,最大最小算法只需更换左右
BinTree FindMin(BinTree BST)
{
    if(!BST){
        return NULL;
    }else if(!BST->left){
        return BST;
    }else{
        return FindMin(BST->left);
    }
}

BinTree FindMax(BinTree BST)
{
    if(BST){
        while(BST->right){
            BST = BST->right;
        }
    }
    return BST;
}

BinTree Insert(ElemType x,BinTree BST)
{
    if(!BST){
        BST = (BinTree)malloc(sizeof(struct TreeNode));
        BST->data = x;
        BST->left = BST->right = NULL;
    }else{
        if(x < BST->data){
            BST->left = Insert(x,BST->left);
        }else if(x > BST->data){
            BST->right = Insert(x,BST->right);
        }
    }
    return BST;
}

BinTree Delete(ElemType x,BinTree BST)
{
    BinTree temp;
    if(!BST){
        printf("Not found");
    }else if(x < BST->data){
        BST->left = Delete(x,BST->left);
    }else if(x > BST->data){
        BST->right = Delete(x,BST->right);
    }else //节点已经找到
        if(BST->left && BST->right){    //两个子节点
            temp = FindMin(BST->right);
            //从右子树中找到最小值
            BST->data = temp->data;
            BST->right = Delete(BST->data,BST->right);
            //在右子树继续递归删除此值
        }else{  //只有一个或无子节点
            temp = BST;
            if(!BST->left){
                BST = BST->right;
            }else if(!BST->right){
                BST = BST->left;
            }
            free(temp);
        }
    return BST;
}

void InOrder(BinTree BT)
{
    if(BT){
        InOrder(BT->left);
        printf("%d ",BT->data);
        InOrder(BT->right);
    }
}

int main(){
	BinTree BST = NULL;
	BST = Insert(5,BST); 
	BST = Insert(7,BST); 
	BST = Insert(3,BST); 
	BST = Insert(1,BST); 
	BST = Insert(2,BST); 
	BST = Insert(4,BST); 
	BST = Insert(6,BST); 
	BST = Insert(8,BST); 
	BST = Insert(9,BST); 
	/*
			    5
			   /\
			  3  7
             /\	 /\
            1 4 6  8
			\      \
			 2      9
	*/
	printf("InOrder"); 
    InOrder(BST);
    printf("\n");
	printf("Min Value:%d\n",FindMin(BST)->data);
	printf("Max Value:%d\n",FindMax(BST)->data); 
	//printf("Find 3 Value:%d\n",FindFor(3,BST)->data);
	//printf("Find 7 Value:%d\n",FindTail(7,BST)->data);
	printf("Delete 5\n");
	Delete(5,BST);
	/*
			    6
			   /\
			  3  7
             /\	  \
            1 4    8
			\      \
			 2      9
	*/
	printf("InOrder:"); 
	InOrder(BST);
	printf("\n");
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值