• 二叉搜索树操作


    
    /* 你的代码将被嵌在这里 */
    BinTree Insert(BinTree BST, ElementType X)
    {
    	if (BST == NULL)
    	{	
    		BST = (BinTree)malloc(sizeof(struct TNode));
    		BST->Data = X;
    		BST->Left = NULL;
    		BST->Right = NULL;
    	}
    	else if (X < BST->Data)
    		BST->Left = Insert(BST->Left, X);
    	else if (X > BST->Data)
    		BST->Right = Insert(BST->Right, X);
    	return BST;	
    }
    Position Find(BinTree BST, ElementType X)
    {
    	if (BST==NULL)
    		return NULL;
    	if (X == BST->Data)
    		return BST;	
    	else if (X < BST->Data)
    		Find(BST->Left, X);
    	else
    		Find(BST->Right, X);
    }
    Position FindMin(BinTree BST)
    {
    	if (BST)
    	{
    		while (BST->Left != NULL)
    			BST = BST->Left;
    	}
    	return BST;
    }
    Position FindMax(BinTree BST)
    {
    	if (BST)
    	{
    		while (BST->Right != NULL)
    			BST = BST->Right;
    	}
    	return BST;
    }
    BinTree Delete(BinTree BST, ElementType X)
    {
    	BinTree p;
    	if (BST == NULL)
    	{
    		printf("Not Found
    ");
    		return BST;
    	}
    	if (X < BST->Data) {
    		BST->Left = Delete(BST->Left, X);
    	}
    	else if (X > BST->Data) {
    		BST->Right = Delete(BST->Right, X);
    	}
    	else
    	{
    		if (BST->Left && BST->Right) {
    			p = FindMax(BST->Left);
    			BST->Data = p->Data;
    			BST->Left = Delete(BST->Left, BST->Data);
    		}
    		else {
    			p = BST;
    			if (!BST->Left) {
    				BST = BST->Right;
    			}
    			else if (!BST->Right) {
    				BST = BST->Left;
    			}
    			free(p);
    		}
    	}
    	return BST;
    }
    
    
  • 相关阅读:
    PDO 数据访问抽象层
    注册审核、批量删除
    分页查询
    会话用法
    封装成类
    多条件查询(复选框条件)
    IP子网划分
    redhat 用yum安装的apache、mysql一般默认安装在哪个目录下?
    nslookup
    linux 设置时间
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811912.html
Copyright © 2020-2023  润新知