// MS: 2003
void 
AstSuccessorsSelectors::selectReverseBranchSuccessors(SgNode* node, SuccessorsContainer& succContainer) {
  // pre condition
  ROSE_ASSERT(node!=0);
  ROSE_ASSERT(succContainer.size()==0);

  SgNode* ls=leftSibling(node);
  if(ls!=0)
    succContainer.push_back(ls);
  else
    succContainer.push_back(node->get_parent()); // is null if root

  // post condition
  ROSE_ASSERT(succContainer.size()<=1);

  /*
  cout << "SUCCESSORS @" << node->sage_class_name() << ":";
  for(SuccessorsContainer::iterator i=succContainer.begin();i!=succContainer.end();i++) {
    cout << (*i)->sage_class_name() << " ";
  }
  cout << endl;
  */
}
Beispiel #2
0
/*
 *	Method:
 *	void BTree::removeAtLeaf( Btree_Node* pNode, int i )
 *
 *	Purpose: 
 *	Removes a key-value pair indexed by i located at a leaf node pNode.
 *
 *	Input:
 *	pNode,		leaf node
 *	i,			index of the pair within the node
 *
 *	Output:
 *	none
 */
void BTree::removeAtLeaf( Btree_Node* pNode, int i )
{
	Btree_Node* pLeftSibling;
	Btree_Node* pRightSibling;
	int h;

	// for safety
	if( NULL==pNode )
		throw "removeAtLeaf, pNode is NULL";

	// check if leaf
	if( NULL != (pNode->child)[0] )
		throw "removeAtLeaf, not a leaf";

	// check if deleting an existing key
	if( i<0 || i>=pNode->nkeys )
		throw "removeAtLeaf, i out of bound";

	// now we know that pNode is a leaf, and we must delete a key with index i

	// delete key k[i] from a leaf node pNode (shift and adjust nkeys)
	for( h=i+1; h<pNode->nkeys; h++ )
		(pNode->pair)[h-1] = (pNode->pair)[h];
	(pNode->nkeys) -- ;

	// if pNode is the root and it carries no keys any more, then set the tree to empty
	if( NULL==(pNode->p) && 0==(pNode->nkeys) ) {
		delete root;
		root = NULL;
		return;
	};

	// repeat while not root and has insufficient number of keys
	while( (pNode->nkeys) < t-1 && NULL!=pNode->p ) {
		// if the node has a sibling on the left, 
		pLeftSibling = leftSibling(pNode);
		if( NULL!=pLeftSibling ) {
			//and the sibling has at least t keys, then transfer one from the sibling, and stop propagation
			if( pLeftSibling->nkeys>=t ) {
				keyTransferRight(pLeftSibling,pNode);
				return;
			}
			else {
				// the sibling has t-1 keys, and the pNode has t-2, so merge the two nodes
				//(note that we are attaching pNode to its left sibling, not vice versa
				//we could attach the left sibling to pNode, but then we will have to 
				//write a second function Btree_attachSibling
				attachSibling(pLeftSibling,pNode);

				// after the attachemnt, the parent may have less than t-1 keys
				// set pNode to the parnet (pNode pointer is not longer valid, as it was deleted 
				// by the attachSibling procedure invoked above)
				pNode = pLeftSibling->p;

				// if pNode is the root with no keys, then set pLeftSibling as the root
				if( NULL==pNode->p && 0==pNode->nkeys ) {
					root = pLeftSibling;
					root->p = NULL;
					delete pNode;
					return;
				};
			};
		}
		else {
			// since pNode does not have a left sibling, and is not the root, then it must have a right sibling
			pRightSibling = rightSibling(pNode);
			if( NULL==pRightSibling ) {printf("not possible, must have right sibling\n"); return;}

			// if the sibling has at least t keys, then transfer one from the sibling
			if( pRightSibling->nkeys >= t ) {
				keyTransferLeft(pNode,pRightSibling);
				return;
			};

			//so the right sibling has t-1 keys and the node has t-2, so merge them
			attachSibling(pNode,pRightSibling);

			pNode = pNode->p;

			// if pNode is the root with no keys, then set its only child to be the root
			if( NULL==pNode->p && 0==pNode->nkeys ) {
				root = (pNode->child)[0];
				root->p = NULL;
				delete pNode;
				return;
			};
		};

	};
}
Beispiel #3
0
void main(){
	int i;
	BiTreeLink T,p,c;
	TElemType e1,e2;
	initBiTree(&T);
	//判断数是否为空,求树的深度
	printf("\nInit a Binary Tree!\nThe tree is empty or not?%d(1:yes 0:no);The tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T));
	//寻找根结点
	e1 = root(T);
	if(e1!=Nil)
		printf("\nThe root of the Binary Tree is:'%c'\n",e1);
	else
		printf("\nThe Binary Tres has no root\n");
		
	//请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)
	printf("\nBuild a Binary Tree!like abc@@de@g@@f@@hi@j@@k@@,@stand for space\n");
	createBiTree(&T);
	//判断数是否为空,求树的深度
	printf("\nThe tree is empty or not?%d(1:yes 0:no);The tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T));
	//寻找根结点
	e1 = root(T);
	if(e1!=Nil)
		printf("\nThe root of the Binary Tree is:'%c'\n",e1);
	else
		printf("\nThe Binary Tres has no root\n");
	
	//层序递归遍历(Of sequence to traverse the binary tree)
	printf("\nlevel order Traverse the Binary Tree:\n");
	levelOrderTraverse(T,visitT);
	
	//先序遍历
	printf("\npreOrder Traverse the Binary Tree:\n");
	preOrderTraverse(T,visitT);
	
	//中序递归遍历
	printf("\n\ninOrder recursion Traverse the Binary Tree:\n");
	inOrderTraverse(T,visitT);
	//中序非递归遍历1
	printf("\ninOrder non-recursion Traverse the Binary Tree 1:\n");
	inOrderTraverse1(T,visitT);
	//中序非递归遍历2
	printf("inOrder non-recursion Traverse the Binary Tree 2:\n");
	inOrderTraverse2(T,visitT);
	
	//后序递归遍历
	printf("\npostOrder recursion Traverse the Binary Tree:\n");
	postOrderTraverse(T,visitT);
	
	//修改结点
	e1 = 'd';//原值
	//scanf("%c",&e1);
	p = point(T,e1);//获得e1对应的指针
	//获得对应结点的值
	printf("\n\nKnow the previous vaule is:'%c'\nEnter the new vlaue:\n",value(p));
	e2 = 'r';//新结点的值
	//scanf("%c",&e2);
	assign(p,e2);//赋新值
	//先序遍历
	printf("preOrder Traverse the Binary Tree:\n");
	preOrderTraverse(T,visitT);
	
	//寻找双亲
	e1 = parent(T,e2);
	if(e1!=Nil) printf("\n\nthe parent of '%c' is : '%c'\n",e2,e1);
	else printf("'%c' has no parent\n",e2);
	
	//寻找左孩子
	e1 = leftChild(T,e2);
	if(e1!=Nil) printf("\nthe left child of '%c' is : '%c'\n",e2,e1);
	else printf("'%c' has no left child\n",e2);
	
	//寻找右孩子
	e1 = rightChild(T,e2);
	if(e1!=Nil) printf("\nthe right child of '%c' is : '%c'\n",e2,e1);
	else printf("'%c' has no right child\n",e2);
	
	//寻找左兄弟
	e1 = leftSibling(T,e2);
	if(e1!=Nil) printf("\nthe left sibling of '%c' is : '%c'\n",e2,e1);
	else printf("'%c' has no left sibling\n",e2);
	
	//寻找右兄弟
	e1 = rightSibling(T,e2);
	if(e1!=Nil) printf("\nthe right sibiling of '%c' is : '%c'\n",e2,e1);
	else printf("'%c' has no right sibiling\n",e2);
	
	//初始化需要插入的树
	initBiTree(&c);//s=jk   //这里有三个空格
	printf("\nBuild the Binary Tree c which has no right child:\n");
	c = (BiTreeLink)malloc(sizeof(BiTNode));
	p = (BiTreeLink)malloc(sizeof(BiTNode));
	c->lchild = p;
	c->rchild = NULL;
	c->data = 'm';
	p->lchild = p->rchild = NULL;
	p->data = 'n';
	
	//createBiTree(&c);
	//先序递归遍历
	printf("\npreOrder Traverse the Binary Tree:\n");
	preOrderTraverse(c,visitT);
	
	//树s插到树T中,请输入树T中树s的双亲结点 s为左(0)或右(1)子树:
	printf("\n\nInsert the Tree s to the Tree T,enter the parent of the Tree c in the Tree T,left Tree(0) and right Tree(1):\n");
	e1= 'b';i = 0;//将子树c作为结点'b'的左子树
	//scanf("%c%d",&e1,&i);
	p = point(T,e1);//p是T中树c的双亲结点指针
	insertChild(p,i,c);
	//先序递归遍历
	printf("\npreOrder Traverse the Binary Tree:\n");
	preOrderTraverse(T,visitT);
	
	// 删除子树,请输入待删除子树根结点 左(0)或右(1)子树
	printf("\n\nDelete the Tree s,enter the root of the deleting Child Tree, left Tree(0) and right Tree(1):\n");
	e1= 'b';i = 1;//删除父结点为'b'的右子树
	p = point(T,e1);//p是T中树c的双亲结点指针
	deleteChild(p,i);
	//先序递归遍历
	printf("\npreOrder Traverse the Binary Tree:\n");
	preOrderTraverse(T,visitT);
	
	//清空子树
	clearBiTree(&T);
	printf("\n\nEmpty the Binary Tree?%d(1:yes 0:no)\nThe tree depth=%d\n",biTreeEmpty(T),biTreeDepth(T));
	//寻找根结点
	e1 = root(T);
	if(e1!=Nil)
		printf("\nThe root of the Binary Tree is:'%c'\n",e1);
	else
		printf("\nThe Binary Tres has no root\n");
}