zoukankan      html  css  js  c++  java
  • 二叉排序树

    一、SearchBST(T, key)与InsertBST(T, key)

    typedef struct BiTNode {
    	ElemType data;
    	struct BiTNode* lchild, * rchild;
    }BiTNode, * BiTree;
    
    SearchBST(T, key){
        while(T不空){
            if(T的值等于key){
                返回T;
            }
            if(key小于T的值)T=T->lchild;
            else T=T->rchild;
        }
        返回NULL;
    }
    
    void SearchBST(BiTree T, ElemType key) {
    	while (T) {
    		if (T->data == key) {
    			return T;
    		}
    		else if (T->data > key) {
    			T = T->lchild;
    		}
    		else {
    			T = T->rchild;
    		}
    	}
    	return NULL;
    }
    
    InsertBST(T, key) {
        if (T为空) { //原树为空,插入节点为根节点
            T = new BiTNode;
            T的值为key;
            T的lchild和rchild都为空;
        }
        else if (T->data = key) { //存在相同节点,返回
            返回;
        }
        else if (key < T - data)
            返回InsertBST(T->lchild, key);
        else
            返回InsertBST(T->rchild, key);
    }
    
    void InsertBST(BiTree* T, ElemType key) {
        if (T==NULL) {
            T = new BiTNode;
            T->data = key;
            T->lchild = T->rchild = NULL;
        }
        else if (T->data = key) { 
            return;
        }
        else if (key < T - data)
            return InsertBST(T->lchild, key);
        else
            return InsertBST(T->rchild, key);
    }
    

    二、CreateBST(T)并中序输出

    CreateBST(T) {
        BiTNode* T = NULL;//初始时T空树
        int i=0;
        自定义类型key[Maxsize];
        输入key[];
        while (key[]不空) {
            InsertBST(T, key[i]);
            i++;
        }
        返回T;
    }
    
    void CreateBST(BiTree& T) {
        BiTNode* T = NULL;
        int i = 0;
        ElemType key[Maxsize];
        cin << key;
        while (key[i]) {
            InsertBST(T, key[i]);
            i++;
        }
        return T;
    }
    
    void InOrder(BiTree T) { //中序输出
    	if (T) {
    		InOrder(T->lchild);
    		cout << T->key<<' ';
    		InOrder(T->rchild);
    	}
    }
    

    三、DeleteBST(T, key)的伪代码

    DeleteBST(T, key) {
        if (T为空)返回;
        else
            找到对应节点;
        调用Delete(T)函数;
    }
    Delete(T) {
        BiTNode* q;
        if (T->rchild为空) {
            q = T;
            T = T->lchild;
            free(q);//左子树的根节点放在被删节点的位置上
        }
        else if (T->lchild为空) {
            q = T;
            T = T->rchild;
            free(q);//右子树的根节点放在被删节点的位置上
        }
        else 
            调用Delete1(T,T->lchild)//左右子树都存在
    }
    Delete1(T,r) {
        BiTNode* q;
        if (r->rchild不空)
            Delete1(T, r->rchild);//递归查找前驱
        else {
            T->data = r->data;//赋值
            q = r;r = r->lchild;
            free(q);//左子树的根节点放在被删节点的位置上
        }
    }
    

    四、DeleteBST(T, key)的函数实现

    void DeleteBST(BiTree& T, ElemType key) {
        if (T == NULL) return;
        else
        {
            if (key < T->data) return DeleteBST(T->lchild, key);
            else if (key > T->data) return DeleteBST(T->rchild, key);
            else Delete(T);
        }
    }
    void Delete(BiTree& T) {
        BiTNode* q;
        if (T->rchild == NULL) {
            q = T;
            T = T->lchild;
            free(q);
        }
        else if (T->lchild == NULL) {
            q = T;
            T = T->rchild;
            free(q);
        }
        else
            Delete1(T, T->lchild);
    }
    void Delete1(BiTree* T, BiTree& r) {
        BiTNode* q;
        if (r->rchild != NULL)
            Delete1(T, r->rchild);
        else {
            T->data = r->data;
            q = r;r = r->lchild;
            free(q);
        }
    }
    
  • 相关阅读:
    North North West
    HDU-5387 Clock
    HDU-1036 Average is not Fast Enough!
    Growling Gears
    HDU-5375 Gray code
    HDU-5373 The shortest problem
    hdu-5364 Distribution money
    UVA
    HDU-5363 Key Set
    HDU-5326 Work
  • 原文地址:https://www.cnblogs.com/2b-or-not-2b-/p/12733730.html
Copyright © 2011-2022 走看看