Beispiel #1
0
void CObAvlTree::CheckTree( CNode* pNode )
{
	if( pNode==NULL )return;

	if( pNode->pLChild )CheckTree( pNode->pLChild );
	if( pNode->pRChild )CheckTree( pNode->pRChild );
	if( abs( NODEHEIGHT(pNode->pLChild)-NODEHEIGHT(pNode->pRChild) )>=2 ){
		TRACE( "Failed balance\n" );
	}
}
Beispiel #2
0
void InsertTree(TreePtr tree,void* key,void* value)
{
	NodePosition position;
	TreeNodePtr node;
	if ((NULL != tree) && (NULL != key))
	{
		node = CreateTreeNode(tree,key,value);
		position = InsertTreeNode(tree,node);
		//插入的节点为父节点的左孩子或者右孩子时,需要检查树的合法性
		//如果是覆盖式或者直接插入根节点则无需再考虑
		if (position == RootNode)
		{
			//根节点必须为黑色
			//node->color = BlackNode;
			tree->size++;
		}
		else if ((position == LeftNode) || (position == RightNode))
		{
			tree->size++;
			CheckTree(tree,node);
		}
		else
		{
			//覆盖式插入,什么也不做,原有的value都懒得返回
		}
	}
	return;
}
bool Parse(std::string const& _strexpr,
    creek::tree<std::string>& _tree)
{
    if(_strexpr == "") return false;
    
    typedef creek::tree<std::string>::pre_order_iterator pre_order_iterator;
    creek::tree<std::string> treetmp;
    pre_order_iterator curr = treetmp.pre_order_begin();
    
    std::string::const_iterator it_str = _strexpr.begin(), it_end = _strexpr.end();
    int rbracket_count = 0, lbracket_count = 0;
    
    while(1){
        std::string token = GetToken(it_str, it_end);
        if(token == "") break;
        
        if(token == "("){
            ++rbracket_count;
            token = GetToken(it_str, it_end);
            
            if(token == "(" || token == ")" || token == ""){
                return false;
            }
            
            if(treetmp.empty())
                curr = treetmp.insert(curr, token);
            else
                curr = treetmp.append_child(curr, token);
        }else if(token == ")"){
            if(treetmp.empty()){
                return false;
            }
            ++lbracket_count;
            curr = creek::parent(curr);
        }else{
            if(treetmp.empty()){
                return false;
            }
            treetmp.append_child(curr, token);
        }
        
    }
    
    if(rbracket_count != lbracket_count){
        return false;
    }
    
    if(!CheckTree(treetmp)){
        return false;
    }
    _tree = treetmp;
    return true;
}
Beispiel #4
0
void    BuildNameTree( conflict_node *conf )
/******************************************/
{
    name        *temp;
    reg_tree    *tree;

    temp = conf->name;
    if( temp->n.class == N_TEMP && temp->t.alias != temp ) {
        tree = BuildTree( temp, temp, temp->v.offset, temp->n.size, conf );
        tree = CheckTree( tree );
        if( tree != NULL ) {
            TrimTree( tree );
        }
    } else {
Beispiel #5
0
 void MonoSynth::BindModsInTree()
 {
     if(!CheckTree()) throw std::runtime_error("parse error");
     
     if((**(m_mod_tree.preorder_begin())).Name() == "SynthVCA"){
         m_root_vca = *(m_mod_tree.preorder_begin());
     }else{
         m_mod_tree.insert(m_mod_tree.preorder_begin(), SynthModFactory::Create("SynthVCA 0.1 0.02 0.1 0.4 0.2"));
         m_root_vca = *(m_mod_tree.preorder_begin());
     }
     
     creek::tree<std::function<Real(void)>> func_tree;
     {
         auto it_mod = m_mod_tree.preorder_begin();
         auto end_mod = m_mod_tree.preorder_end();
         auto it_func = func_tree.preorder_begin();
         
         it_func = func_tree.insert(it_func, std::function<Real(void)>());
         while(it_mod != end_mod){
             auto it_child = it_mod.begin();
             auto end_child = it_mod.end();
             while(it_child != end_child){
                 func_tree.append_child(it_func, std::function<Real(void)>());
                 ++it_child;
             }
             ++it_mod;
             ++it_func;
         }
     }
     {
         auto it_mod = m_mod_tree.postorder_begin();
         auto end_mod = m_mod_tree.postorder_end();
         auto it_func = func_tree.postorder_begin();
         while(it_mod != end_mod){
             (*it_func) = (**it_mod).MakeFunction(it_func.begin(), it_func.end());
             ++it_mod;
             ++it_func;
         }
         
         m_function = *(func_tree.preorder_begin());
     }
 }
Beispiel #6
0
void CObAvlTree::CheckTree()
{
	CheckTree( m_pNodeRoot );
}
Beispiel #7
0
static void CheckTree(TreePtr tree,TreeNodePtr node)
{
	//父节点,祖父节点,叔父节点
	TreeNodePtr parent,grand,uncle;//great

	//与父节点的关系(左孩子还是右孩子),父节点与祖父节点的关系
	NodePosition selfPosition,parentPosition;
	parent = node->parent;
	/*
	great = (NULL != parent) ? parent->parent : NULL;
	uncle = (NULL != great) ? ((great->left == parent) ? great->right : great->left) : NULL;
	TreeColor selfColor = node->color;
	TreeColor parentColor = (NULL != parent) ? parent->color : BlackNode;
	TreeColor uncleColor = (NULL != uncle) ? uncle->color : BlackNode;
	TreeColor greatColor = (NULL != great) ? great->color : BlackNode;
	*/
	//父节点为NULL,这表示是根节点
	if (NULL == parent)
	{
		SetRootNode(tree,node);
	}
	else if ((node->color == RedNode) && (parent->color == RedNode))
	{
		//自身节点 父节点为红色
		//这个时候一定会有一个祖父节点
		grand = parent->parent;
		uncle = (grand->left == parent) ? grand->right : grand->left;
		//自身节点 父节点 叔父节点都为红色
		if ((NULL != uncle) && (uncle->color == RedNode))
		{
			grand->color = RedNode;
			parent->color = BlackNode;
			uncle->color = BlackNode;
			CheckTree(tree,grand);
			//将祖父节点作为新插入的节点开始检测树的合法性
		}
		else  //叔父节点是黑色或者空节点
		{
			//自身节点 父节点为红色 叔父节点为黑色(NULL表示黑色)
			selfPosition = (node == parent->left) ? LeftNode : RightNode;
			parentPosition = (parent == grand->left) ? LeftNode : RightNode;
			//旋转分为 左左 左右 右左 右右
			if ((selfPosition == LeftNode) && (parentPosition == LeftNode))
			{
				/*
				G(b)                    P(b)
				/      \                 /     \
				P(r)     U(b)   -->      N(r)    G(r)
				/   \                            /    \
				N(r)    M                          M     U(b)
				*/
				RightRotate(tree,grand);
				//左左的情况需要更换节点颜色,可以知道的是祖父节点肯定是黑色
				parent->color = BlackNode;
				grand->color = RedNode;
			}
			else if ((parentPosition == RightNode) && (selfPosition == LeftNode))
			{
				//右左
				//以父节点右转,这个时候将变成右右的关系
				RightRotate(tree,parent);
				//要是减少递归,可以再转一次
				//CheckTree(tree,node);
				LeftRotate(tree,grand);
				parent->color = BlackNode;
				grand->color = RedNode;
			}
			else if ((parentPosition == LeftNode) && (selfPosition == RightNode))
			{
				//左右
				//以父节点左转,这个时候将变成左左的关系
				LeftRotate(tree,parent);
				//下次检测则会发现是左左关系
				//要是减少递归,可以再转一次
				//CheckTree(tree,node);
				RightRotate(tree,grand);
				parent->color = BlackNode;
				grand->color = RedNode;
			}
			else
			{
				//右右
				/*
				G(b)                    P(b)
				/      \                 /    \
				U(b)     P(r)   -->      G(r)   N(r)
				/     \          /   \
				M      N(r)     U(b)   M
				*/
				LeftRotate(tree,grand);
				parent->color = BlackNode;
				grand->color = RedNode;
			}
		}
		//这里发现祖父节点肯定被染成了红色,而父节点都被染成黑色
		//因为插入的是红色节点,而一个红节点必须包含两个黑色节点
		//·
	}
	else
	{
		//这样表示调整基本完毕
	}
}