zoukankan      html  css  js  c++  java
  • 二叉搜索树操作

    
    /* 你的代码将被嵌在这里 */
    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;
    }
    
    
  • 相关阅读:
    C# 中对COOKIES的操作
    guruguru
    异或序列
    最优贸易
    farm
    Fruit Ninja
    商务旅行
    Generation I
    Heritage of skywalkert
    Bichrome Tree
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13811912.html
Copyright © 2011-2022 走看看