程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 紅黑樹的介紹和實現(二)

紅黑樹的介紹和實現(二)

編輯:關於C語言

//file RBTree.h

//written by saturnman

#ifndef _RB_TREE_H_

#define _RB_TREE_H_

#include<iostream>

#include<string>

#include<sstream>

#include<fstream>

using namespace std;

template<class KEY,class U>

class RB_Tree

{

    private:

        RB_Tree(const RB_Tree& input){}

        const RB_Tree& operator=(const RB_Tree& input){}

    private:

        enum COLOR{RED,BLACK};

        class RB_Node

        {

            public:

                RB_Node()

                {

                    RB_COLOR = BLACK;

                    right = NULL;

                    left = NULL;

                    parent = NULL;

                }

            COLOR RB_COLOR;

            RB_Node* right;

            RB_Node* left;

            RB_Node* parent;

            KEY key;

            U data;

        };

    public:

        RB_Tree()

        {

            this->m_nullNode = new RB_Node();

            this->m_root = m_nullNode;

            this->m_nullNode->right = this->m_root;

            this->m_nullNode->left = this->m_root;

            this->m_nullNode->parent = this->m_root;

            this->m_nullNode->RB_COLOR = BLACK;

        }

        bool Empty()

        {

            if(this->m_root == this->m_nullNode)

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        //find node whos key equals to key,else find the insert point;

        RB_Node* find(KEY key)

        {

            RB_Node* index = m_root;

            while(index != m_nullNode)

            {

                if(key<index->key)

                {

                    index  = index->left;

                }

                else if(key>index->key)

                {

                    index = index->right;

                }

                else

                {

                    break;

                }

            }

            return index;

        }

        bool Insert(KEY key,U data)

        {

            RB_Node* insert_point = m_nullNode;

            RB_Node* index = m_root;

            while(index!=m_nullNode)

            {

                insert_point = index;

                if(key<index->key)

                {

                    index = index->left;

                }

                else if(key>index->key)

                {

                    index = index->right;

                }

                else

    &

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved