zoukankan      html  css  js  c++  java
  • 区间树的简易实现

    区间树结点的定义:(叶子节点即左右子树为NULL)

    代码
     1 #include <iostream>
     2 
     3 template<class T>
     4 class SegementTreeNode{
     5 public:
     6     T lValue;
     7     T rValue;
     8     int count;
     9     SegementTreeNode(){count=0;};
    10     SegementTreeNode(T lValue,T rValue):lValue(lValue),rValue(rValue),count(0){};
    11     void Output(){std::cout<<"["<<lValue<<","<<rValue<<"]"<<""<<count<<"\n";};
    12     ~SegementTreeNode(){};
    13 };

     区间树为完全二叉树结构,三种递归遍历方式:

    代码
      1 #include "SegmentTreeNode.h"
      2 
      3 template<class T>
      4 class SegementTree
      5 {
      6 public:
      7     /************************************************************************/
      8     /* 数据成员                                                             */
      9     /************************************************************************/
     10     SegementTreeNode<T> *pRoot;
     11     SegementTree<T> *pLCTree;
     12     SegementTree<T> *pRCTree;
     13     /************************************************************************/
     14     /* 构造函数                                                             */
     15     /************************************************************************/
     16     SegementTree()
     17     {
     18         pRoot = NULL;
     19         pLCTree = NULL;
     20         pRCTree = NULL;
     21     }
     22     SegementTree(T lValue, T rValue);
     23     // 遍历方法 [2010/10/4 20:31:00 Leo-823]
     24     void PreOrderShow();
     25     void InOrderShow();
     26     void PostOrderShow();
     27     // 插入某个值 [2010/10/4 20:31:14 Leo-823]
     28     void Insert(T value);
     29     // 销毁树 [2010/10/4 20:31:27 Leo-823]
     30     void Destroy();
     31 };
     32 
     33 template<class T>
     34 void SegementTree<T>::PreOrderShow()
     35 {
     36     if(pRoot == NULL)
     37         return;
     38     if(pLCTree != NULL && pRCTree != NULL)
     39     {
     40         pRoot->Output();
     41         pLCTree->PreOrderShow();
     42         pRCTree->PreOrderShow();
     43     }
     44     else
     45     {
     46         pRoot->Output();
     47     }
     48 }
     49 
     50 template<class T>
     51 void SegementTree<T>::InOrderShow()
     52 {
     53     if(pRoot == NULL)
     54         return;
     55     if(pLCTree != NULL && pRCTree != NULL)
     56     {
     57         pLCTree->InOrderShow();
     58         pRoot->Output();
     59         pRCTree->InOrderShow();
     60     }
     61     else
     62     {
     63         pRoot->Output();
     64     }
     65 }
     66 
     67 template<class T>
     68 void SegementTree<T>::PostOrderShow()
     69 {
     70     if(pRoot == NULL)
     71         return;
     72     if(pLCTree != NULL && pRCTree != NULL)
     73     {
     74         pLCTree->PostOrderShow();
     75         pRCTree->PostOrderShow();
     76         pRoot->Output();
     77     }
     78     else
     79     {
     80         pRoot->Output();
     81     }
     82 }
     83 
     84 template<class T>
     85 void SegementTree<T>::Insert(T value)
     86 {
     87     if(value > pRoot->rValue || value < pRoot->lValue)
     88         return;
     89     if(value == pRoot->lValue && value == pRoot->rValue)
     90     {
     91         pRoot->count += 1;
     92         return;
     93     }
     94     if(value <= pLCTree->pRoot->rValue)
     95         pLCTree->Insert(value);
     96     if(value >= pRCTree->pRoot->lValue)
     97         pRCTree->Insert(value);
     98     
     99 }
    100 
    101 template<class T>
    102 void SegementTree<T>::Destroy()
    103 {
    104     if(pRoot == NULL)
    105         return;
    106     else
    107         pRoot = NULL;
    108     // 叶子节点 [2010/10/4 19:57:24 Leo-823]
    109     if(pLCTree == NULL && pRCTree == NULL)
    110     {
    111         delete pRoot;
    112         pRoot = NULL;
    113         return;
    114     }
    115     pLCTree->Destroy();
    116     pLCTree = NULL;
    117     {
    118         pRCTree->Destroy();
    119         pRCTree = NULL;
    120     }
    121 }
    122 
    123 template<class T>
    124 SegementTree<T>::SegementTree(T lValue, T rValue)
    125 {
    126     pRoot = new SegementTreeNode<T>(lValue,rValue);
    127     if(lValue == rValue)
    128     {
    129         pLCTree = NULL;
    130         pRCTree = NULL;
    131         return;
    132     }
    133     T midValue = (rValue - lValue) / 2;
    134     pLCTree = new SegementTree<T>(lValue,midValue+lValue);
    135     pRCTree = new SegementTree<T>(lValue + midValue+1,rValue);
    136 }
  • 相关阅读:
    hdu5269 ZYB loves Xor I
    UVA 11181 Probability|Given
    UVA 10277 Boastin' Red Socks
    CodeForces 566B Replicating Processes
    UVA 10714 Ants
    CodeForces 567B Berland National Library hdu-5477 A Sweet Journey
    UVA 10759 Dice Throwing
    UVA 11027
    「CF241E」Flights
    「CF891C」Envy
  • 原文地址:https://www.cnblogs.com/wxfasdic/p/1844240.html
Copyright © 2011-2022 走看看