平衡树生成

#ifndef BANLANCE_TREE_H
#define BANLANCE_TREE_H
#include 
#include
#include
#include
template 
class banlance_tree{
    struct banlance_tree_node{
        int index;
        T data;
        //同级结点
        banlance_tree_node *next;
        banlance_tree_node *child_head;
        banlance_tree_node *child_next;
        banlance_tree_node(T _data,int _index=0):index(_index),data(_data),next(nullptr),child_head(nullptr),child_next(nullptr){

        }
    };

public:
    //scale为每个节点下级最多的节点数
    banlance_tree(const std::vector&input,uint32_t scale=2)
    {
        if(input.empty())return;
        if(scale<1)scale=1;
        if(scale==input.size())scale=input.size();
        int index=0;
        m_node_lists.emplace_back(new banlance_tree_node(input[0],index));
        for(auto i :input)
        {
            index++;
            m_node_lists.emplace_back(new  banlance_tree_node(i,index));
        }
        std::vectorlast_node_status;
        last_node_status.push_back(m_node_lists[0]);
        int floor=1;
        int counts=1;
        bool is_over=false;

        while(!is_over)
        {
            int move_index=counts;
            for(int i=0;ichild_head){
                        node->child_head=m_node_lists[move_index];
                        node->child_next=m_node_lists[move_index];
                    }
                    else {
                        node->child_next->next=m_node_lists[move_index];
                        node->child_next=m_node_lists[move_index];
                    }
                    move_index++;
                    if(move_index>=m_node_lists.size()){
                        is_over=true;
                        break;
                    }
                }
            }
            std::vectortmp;
            if(!is_over)
            {
                for(auto i:last_node_status)
                {
                    auto begin=i->child_head;
                    while(begin)
                    {
                        tmp.push_back(begin);
                        begin=begin->next;
                    }
                }
            }
            std::swap(tmp,last_node_status);
            counts+=pow(scale,floor);
            floor++;
        }
#ifdef DEBUG
        for(auto i :m_node_lists)
        {
            printf("node %d data %d \r\n",i->index,i->data);
            auto node=i->child_head;
            printf("child info -----");
            while(node)
            {
                printf("%d ",node->data);
                node=node->next;
            }
            printf("\r\n");
        }
#endif
    }
    banlance_tree_node * search_node(int index=0){
        std::lock_guardlocker(m_mutex);
        if(index<0||index>m_node_lists.size())return nullptr;
        else {
            return m_node_lists[index];
        }
    }
    ~banlance_tree()
    {
        for(auto i:m_node_lists)
        {
            delete i;
        }
    }
private:
    std::mutex m_mutex;
    std::vectorm_node_lists;
};
#endif // BANLANCE_TREE_H

 

你可能感兴趣的:(C/C++)