• 二叉查找树实现


    二叉查找树声明

    #ifndef _Tree_H
    
    struct TreeNode;
    typedef struct TreeNode *Position;
    typedef struct TreeNode *SearchTree;
    
    SearchTree MakeEmpty(SearchTree T);
    Position Find( ElementType X,SearchTree T);
    Position FindMin(SearchTree T);
    Position FindMax( SearchTree T);
    SearchTree Insert(ElementType X,SearchTree T);
    SearchTree Delete(ElementType X,SearchTree T);
    ElementType Retrieve( Position P);
    
    #endif
    
    srtuct TreeNode
    {
    	ElementType Element;
    	SearchTree Left;
    	SearchTree Right;
    };
    

    建立一颗空树

    SearchTree MakeEmpty(SearchTree T)
    {
    	if(T != NULL)
    	{
    		MakeEmpty(T->Left);
    		MakeEmpty(T->Right);
    		Free(T);
    	}
    	return NULL;
    }
    

    二叉查找树的Find操作

    Position Find(ElementType X,SearchTree T)
    {
    	if(T == NULL)
    		return NULL;
    	if( X < T->Element)
    		return Find(X,T->Left)
    	else if( X > T->Element)
    		return Find(X,T->Right)
    	else return T;
    }
    

    二叉查找树的FindMin递归实现

    Position FindMin(SearchTree T)
    {
    	if(T == NULL)
    		return NULL;
    	else if( T->Left == NULL)
    		return T;
    	else
    		return FindMin(T->Left);
    }
    

    二叉查找树的FindMin非递归实现

    Position FindMin(SearchTree T)
    {
    	if(T != NULL)
    		while(T->Left != NULL)
    			T = T->Left;
    	return T;
    }
    

    二叉查找树的FindMax递归实现

    Position FindMax(SearchTree T)
    {
    	if(T == NULL)
    		return NULL;
    	else if( T->Right == NULL)
    		return T;
    	else
    		return FindMax(T->Right);
    }
    

    二叉查找树的FindMax非递归实现

    Position FindMax(SearchTree T)
    {
    	if(T != NULL)
    		while(T->Right != NULL)
    			T = T->Right;
    	return T;
    }
    

    二叉查找树的插入操作

    SearchTree Insert(ElementType X,SearchTree T)
    {
    	if(T == NULL)
    	{
    		T = malloc(sizeof(struct TreeNode));
    		if(T == NULL)
    			FatalError("Out of space!!!");
    		else
    		{
    			T->Element = X;
    			T->Left = T->Right = NULL;
    		}
    	}
    	else if(X < T->Element)
    		T->Left = Insert(X,T->Left);
    	else if( X > T->Element)
    		T->Right = Insert(X,T->Right);
    	return T;
    }
    

    二叉查找树的删除操作

    SearchType Delete(ElementType X,SearchTree T)
    {
    	Position TmpCell;
    	if(T == NULL)
    		Error("Element not found");
    	else if(X < T->Element)
    		T->Left = Delete(X,T->Left);
    	else if(X > T->Element)
    		T->Right = Delete(X,T->Right);
    	else if(T->Left && T->Right)
    	{
    		TmpCell = FindMin(T->Right);
    		T->Element = TmpCell->Element;
    		T->Right = Delete(T->Element,T->Right);
    	}
    	else
    	{
    		TmpCell = T;
    		if(T->Left == NULL)
    			T = T->Right;
    		else if(T->Right == NULL)
    			T = T->Left;
    		free(TmpCell);
    	}
    	return T;
    }
  • 相关阅读:
    非常可乐
    Find The Multiple
    盲点集锦
    Fliptile
    Catch That Cow
    Dungeon Master
    hdoj 1045 Fire Net
    hdoj 1342 Lotto【dfs】
    zoj 2100 Seeding
    poj 3620 Avoid The Lakes【简单dfs】
  • 原文地址:https://www.cnblogs.com/y3w3l/p/6363506.html
Copyright © 2020-2023  润新知