• 二叉排序树


    一、伪代码

    1.寻找

    	if(T为空){
    	    返回	
    	}
    	if (s->data > T->data){
    		T->rchild=s;
            返回;
    	}
    	递归T->rchild;
    	else if(s->data<T->data){
    		if(T->lchild为空){
    		   T->lchild=s;
    		   返回;
    		}
    		递归T->lchild;
    	}
    }
    

    2.插入

    	if(树为空){
    		s为根节点;
    	}
    	else{
    	    SearchNode(tree, s);	
    	}
    }
    

    3.创建

    	for(i=0;i<数组长度;i++){
    		s申请内存;
            s->data=a[i];
            s的左右孩子为NULL;
    	}
    } 
    

    4.删除

    注意事项:

    1.

    删除操作较复杂需要分三中情况:
    (1)删除节点的左孩子为空
    (2)删除节点的右孩子为空
    (3)删除节点有左右子树

    2.

    函数需要用到&符号,这需要对实参进行删除。

    3.

    申请空间后删除记得使用free()。

    伪代码:

    void DeleteNode(BTree &T, int x){
    	if(T为空){
    	    返回;
    	}
    	寻找元素位置;
    	else{
    	    if(T->lchild为空){
    	        申请temp;
                    T=T->rchild 
                    free(temp);
            } 
            else if(T->rchild为空){
                申请temp:
                temp等于其rchild;
                free(temp);
    	}
    	else{
                申请temp和其前驱pre;
    	    while(temp->rchild){
    	        pre=temp;
                    temp等于其rchild;
    	    }
    	    tree->data=temp->data;
    	    if(pre!=T){
    	        temp的lchild赋值给pre的rchild;
    	    }
    	    else{
    		temp的lchild赋值给pre的lchild;
    	    }
            free(temp);
    	}       
    }
    

    二、完整代码:

    #include<iostream>
    using namespace std;
    #define length 12							
    typedef struct BTNode
    {
    	int data;
    	struct BTNode* lchild, * rchild;
    }BTNode, * BTree;
    void SearchNode(BTree& T, BTree& s)
    {
    	if (T == NULL)                     
    		return;                             
    	if (s->data > T->data)             
    	{
    		if (T->rchild == NULL) {
    			T->rchild = s;
    			return;
    		}
    		SearchNode(T->rchild, s);
    	}
    
    	else if (s->data < T->data)
    	{
    		if (T->lchild == NULL) {
    			T->lchild = s;
    			return;
    		}
    		SearchNode(T->lchild, s);
    	}
    }
    void InsertNode(BTree& tree, BTree& s)
    {
    	if (tree == NULL)
    		tree = s;
    	else
    		SearchNode(tree, s);
    }
    void CreatBTree(BTree& tree, int* a)
    {
    	for (int i = 0; i < length; i++)
    	{
    		BTree s = new BTNode;
    		s->data = a[i];
    		s->lchild = NULL;
    		s->rchild = NULL;
    		InsertNode(tree, s);
    	}
    }
    void DeleteNode(BTree& T, int x)			
    {
    	if (T == NULL)
    		return;
    	if (x > T->data)
    		DeleteNode(T->rchild, x);
    	else if (x < T->data)
    		DeleteNode(T->lchild, x);
    	else
    	{
    		if (T->lchild == NULL)		
    		{
    			BTree temp = T;
    			T = T->rchild;
    			free(temp);
    		}
    		else if (T->rchild == NULL)			
    		{
    			BTree temp = T;
    			temp = temp->rchild;
    			free(temp);
    		}
    		else
    		{
    			BTree pre, temp;			
    			pre = T;
    			temp = T->lchild;
    			{
    				pre = temp;
    				temp = temp->rchild;
    			}
    			T->data = temp->data;
    			if (pre != T)
    				pre->rchild = temp->lchild;	
    			else
    				pre->lchild = temp->lchild;	
    			free(temp);
    		}
    	}
    }
    void Inorder(BTree tree)
    {
    	if (tree == NULL)
    		return;
    	Inorder(tree->lchild);
    	cout << tree->data << " ";
    	Inorder(tree->rchild);
    }
    int main()
    {
    	int n;
    	int a[length] = { 50,30,80,20,40,90,10,25,35,85,23,88 };
    	BTree tree = NULL;
    	CreatBTree(tree, a);
    	cout << "中序输出:"<<endl;
    	Inorder(tree);
    	cout << endl;
    	cout << "需删除的节点:" << endl;
    	cin >> n;
    	cout <<"中序输出:";
    	DeleteNode(tree, n);
    	Inorder(tree);
    	return 0;
    }
    

    三、运行截图


  • 相关阅读:
    ThinkPHP运算符 与 SQL运算符 对比表
    [Java 8] (6) Lambda与资源管理
    Codeforces Round #275 (Div. 2) C
    HOJ 2245 浮游三角胞(数学啊 )
    [UVALive 6663 Count the Regions] (dfs + 离散化)
    浅解ARC中的 __bridge、__bridge_retained和__bridge_transfer
    SpringMVC: web.xml中声明DispatcherServlet时一定要加入load-on-startup标签
    Unity3d 4.3.4f1执行项目
    更新Windows ActiveX,Ios
    C++11: final与override
  • 原文地址:https://www.cnblogs.com/yyzhu/p/12733367.html
Copyright © 2020-2023  润新知