1. 程式人生 > >C++實現紅黑樹(插入功能)

C++實現紅黑樹(插入功能)

lse space num 節點 fine 兩個 外部 type gre

practice1.h

#ifndef PRACTICE1_H_INCLUDED
#define PRACTICE1_H_INCLUDED

template<class Comparable>
class RedBlackTree;

template<class Comparable>
class RedBlackNode;

template<class Comparable>
class RedBlackTree
{
public:
    RedBlackTree(const Comparable& negInf);//偽根數據負無窮
    ~RedBlackTree();
    
enum{RED,BLACK}; typedef RedBlackNode<Comparable> Node; void insert(const Comparable & x); //private:為了測試臨時變成public public: Node *header; Node *nullNode;//空節點 Node *current; Node *parent;//父節點 Node *grand;//祖父節點 Node *great;//曾祖父節點 void rotateWithLeftChild(Node* &k2) const
; void rotateWithRightChild(Node* &k1) const; void doubleRotateWithLeftChild(Node * & k3) const; void doubleRotateWithRightChild(Node * & k1) const; void handleReorient(const Comparable & item ); RedBlackNode<Comparable> *rotate(const Comparable & item,Node *theParent) const
; }; template<class Comparable> class RedBlackNode { //為了測試寫成public 因為默認是私有的 public: Comparable element; RedBlackNode *left; RedBlackNode *right; int color; RedBlackNode(const Comparable& theElement=Comparable(),//該類型默認構造的對象 RedBlackNode *lt=NULL, RedBlackNode *rt=NULL, int c=RedBlackTree<Comparable>::BLACK) :element(theElement),left(lt),right(rt),color(c){} friend class RedBlackTree<Comparable>; }; template<class Comparable> RedBlackTree<Comparable>::RedBlackTree(const Comparable &negInf) { nullNode=new Node();//空節點 nullNode->left=nullNode->right=nullNode; header=new Node(negInf); //偽頭 包含一個負無窮數 header->left=header->right=nullNode; } template<class Comparable> RedBlackTree<Comparable>::~RedBlackTree()//構造函數 { delete nullNode; delete header; } template <class Comparable> void RedBlackTree<Comparable>::insert(const Comparable& x) { current=parent=grand=header; nullNode->element=x; while(current->element!=x) { great=grand; grand=parent; parent=current; current=x<current->element?current->left:current->right; if(current->left->color==RED&&current->right->color==RED) handleReorient(x); } if(current!=nullNode) throw "had exited"; current=new Node(x,nullNode,nullNode); if(x<parent->element) parent->left=current; else parent->right=current; handleReorient(x); //自動平衡->紅黑樹 } template<class Comparable> void RedBlackTree<Comparable>::rotateWithLeftChild(Node * & k2) const//向右轉 { Node *k1=k2->left; k2->left=k1->right; k1->right=k2; k2=k1; } template<class Comparable> void RedBlackTree<Comparable>::rotateWithRightChild(Node * & k1) const//向左轉 { Node *k2=k1->right; k1->right=k2->left; k2->left=k1; k1=k2; } template<class Comparable>//雙旋轉 void RedBlackTree<Comparable>::doubleRotateWithLeftChild(Node * & k3) const { rotateWithRightChild(k3->left); rotateWithLeftChild(k3); } template<class Comparable> void RedBlackTree<Comparable>::doubleRotateWithRightChild(Node * & k1) const { rotateWithLeftChild(k1->right); rotateWithRightChild(k1); } //單旋轉:新插入的節點是外部孫子 //雙旋轉:新插入的節點是內部孫子 template<class Comparable> void RedBlackTree<Comparable>::handleReorient(const Comparable & item ) { //變色 把有兩個紅色兒子的父親變成紅的 兩個兒子變成黑的 current->color=RED; current->left->color=BLACK; current->right->color=BLACK; if(parent->color==RED) { grand->color=RED; if(item<grand->element!=item<parent->element) { parent=rotate(item,grand);//內部孫子則多旋轉一次 } current=rotate(item,great); current->color=BLACK; } header->right->color=BLACK; //單旋轉 //雙旋轉 } template<class Comparable> RedBlackNode<Comparable> * RedBlackTree<Comparable>::rotate(const Comparable & item,Node *theParent) const { if(item<theParent->element) { item<theParent->left->element? rotateWithLeftChild(theParent->left): rotateWithRightChild(theParent->left); return theParent->left; } else { item<theParent->right->element? rotateWithLeftChild(theParent->right): rotateWithRightChild(theParent->right); return theParent->right; } } #endif // PRACTICE1_H_INCLUDED

practice.cpp

//c++ map和set是由紅黑樹實現的
#include<iostream>
#include"practice1.h"
using namespace std;

int main()
{

    const int Neg=-999888;
    RedBlackTree<int> t(Neg);
//    t.insert(30);
//    t.insert(15);
//    t.insert(70);
//    t.insert(20);
//    cout<<"ok"<<endl;
//    cout<<t.header->right->element<<endl;
//    cout<<t.header->right->left->element<<endl;
//    cout<<t.header->right->left->right->element<<endl;



//    cout<<"向右轉"<<endl;
//    t.rotateWithLeftChild(t.header->right);
//    cout<<t.header->right->element<<endl;
//    cout<<t.header->right->right->left->element<<endl;
//    cout<<"向左轉"<<endl;
//    t.rotateWithRightChild(t.header->right);
//    cout<<t.header->right->element<<endl;
//    cout<<t.header->right->right->left->element<<endl;
    t.insert(50);
    t.insert(40);
    t.insert(30);
    cout<<""<<t.header->right->element<<endl;
     t.insert(60);
    // cout<<"頭"<<t.header->right->left->right->element<<endl;
     t.insert(20);
    // cout<<"頭"<<t.header->right->left->left->element<<endl;
     t.insert(35);
     t.insert(31);
     cout<<""<<t.header->right->element<<endl;

//
//    cout<<t.header->right->left->element<<endl;
//     cout<<t.header->right->left->left->right->element<<endl;
//     t.doubleRotateWithLeftChild(t.header->right->left);
//     cout<<t.header->right->left->element<<endl;//6
//     cout<<t.header->right->left->left->right->element<<endl;//5
    cout<<""<<t.header->right->element<<endl;
    return 0;
}

C++實現紅黑樹(插入功能)