Beispiel #1
0
Segment DocRecord::UpperCommonTangent(DT vl, DT vr)
{
	PointNumero x, y, z, z1, z2, temp;
	Segment s;

	x = vl.end;   // vu le tri, c'est le point le + a droite
	y = vr.begin; // idem, le + a gauche
	z = First(y);
	z1 = First(x);
	z2 = Predecessor(y, z);
	for(;;) {
		if(IsLeftOf(x, y, z2)) {
			temp = z2;
			z2 = Predecessor(z2, y);
			y = temp;
		}
		else if(IsLeftOf(x, y, z1)) {
			temp = z1;
			z1 = Successor(z1, x);
			x = temp;
		}
		else {
			s.from = x;
			s.to = y;
			return s;
		}
	}
}
Beispiel #2
0
Segment DocRecord::LowerCommonTangent(DT vl, DT vr)
{
	PointNumero x, y, z, z1, z2, temp;
	Segment s;

	x = vl.end;   // 
	y = vr.begin; // 
	z = First(y);
	z1 = First(x);
	//返回x邻居z1(如果存在的话)的邻居
	z2 = Predecessor(x, z1);//在X的邻接链表里,,向前找 ,找到z1前一位
	for(;;) {
		if(IsRightOf(x, y, z)) {
			temp = z;
			z = Successor(z, y);
			y = temp;
		}
		else if(IsRightOf(x, y, z2)) {
			temp = z2;
			z2 = Predecessor(z2, x);
			x = temp;
		}
		else {
			s.from = x;
			s.to = y;
			return s;
		}
	}
}
Beispiel #3
0
bool BST<T>::Remove(T item)
{
	if (this->size <= 0 || Search(item) == false) { return false; }

	Node<T>* node = root;

	while (node != NULL)
	{
		if (item == node->data) { break; }
		else if (item < node->data) { node = node->left; }
		else { node = node->right; }
	}

	if (this->size == 1) { delete root; root = NULL; this->size--; return true; }
	else if (this->size == 2) 
	{
		if (node == root)
		{
			if (root->left != NULL)
			{
				root->data = root->left->data;
				delete root->left;
				return true;
			}
			else if (root->right != NULL)
			{
				root->data = root->right->data;
				delete root->right;
				return true;
			}
		}

		delete node;
		node = NULL;
		root->left = NULL;
		root->right = NULL;
		this->size--;
		return true;
	}

	Node<T>* pred = Predecessor(node);

	node->data = pred->data;

	if (pred->p->right == pred) { pred->p->right = NULL; }
	else if (pred->p->left == pred) { pred->p->left = NULL; }

	delete pred;
	this->size--;
	return true;
}
Beispiel #4
0
bool RBTree<T>::Remove(T item){// Unable to find item
	if (size == 0 || !Contains(item)) {
		return false;
	}

	// Only node (root) node data does not match
	else if (size == 1 && root->data != item) {
		return false;
	}

	Node<T>* delete_this = root;
	// Finding the node for deletion
	while (delete_this != NULL) {
		if (item == delete_this->data) {
			break;
		}

		// Right
		else if (item > delete_this->data) {
			delete_this = delete_this->right;
		}

		//left
		else if (item < delete_this->data) {
			delete_this = delete_this->left;
		}
	}

	// Following algortihm taken from Lecture Slide
	Node<T>* y;
	Node<T>* x;

	// Both children are empty. Able to delete
	if (delete_this->left == NULL && delete_this->right == NULL) {
		delete delete_this;
		size--;
		delete_this = NULL; // setting empty pointer to NULL
		return true;
	}

	// There is at least one child
	if (delete_this->left == NULL || delete_this->right == NULL) {
		y = delete_this;
	}

	// delete_this has two children
	else {
		y = Predecessor(delete_this);
	}

	if (y->left != NULL) {
		x = y->left;
	}

	else {
		x = y->right;
	}

	x->p = y->p; // Detach x from y (if x is no NULL)

	// Delete node is root
	if (x->p == NULL) {
		root = x;
	}

	else {
		// Attach x to y's parent
		if (y == y->p->left) { // left child
			y->p->left = x;
		}

		else {
			y->p->right = x;
		}
	}

	if (y != delete_this) {
		delete_this->data = y->data; // y is the predessor
	}

	if (y->is_black) {
		RBRemoveFixUp(x, x->p, x == x->p->left); // x could be NULL
	}

	delete delete_this;
	delete_this = NULL; // Set empty pointer to NULL
	size--;
}
Beispiel #5
0
int DocRecord::Merge(DT vl, DT vr)
{
	Segment bt, ut;
	int a, b, out;
	PointNumero r, r1, r2, l, l1, l2;

	bt = LowerCommonTangent(vl, vr);
	ut = UpperCommonTangent(vl, vr);
	l = bt.from;  // left endpoint of BT
	r = bt.to;    // right endpoint of BT

	while((l != ut.from) || (r != ut.to)) {
		a = b = 0;
		if(!Insert(l, r))
			return 0;

		r1 = Predecessor(r, l);//候选点R1
		if(r1 == -1)
			return 0;
		if(IsRightOf(l, r, r1)) //已经到达最上的点
			a = 1;                //a 
		else {
			out = 0;
			while(!out) {              //开始寻找最终点 
				r2 = Predecessor(r, r1);
				if(r2 == -1)
					return 0;
				if(r2 < vr.begin)        //如果r2????
					out = 1;
				else if(Qtest(l, r, r1, r2))
					out = 1;              //如果r2在外接圆外,最终候选点为r1,跳出循环
				else {
					if(!Delete(r, r1))    //如果在圆内,删除R R1边
						return 0;
					r1 = r2;                           //r2成为新的R1
					if(IsRightOf(l, r, r1))           //如果新的R1已经过了最高点
						out = a = 1;        //                    //最终候选点是r1,并且a=1,跳出循环
					//没过最高点,则out=0,继续寻找
				}
			}
		}

		l1 = Successor(l, r);
		if(l1 == -1)
			return 0;
		if(IsLeftOf(r, l, l1))
			b = 1;
		else {
			out = 0;
			while(!out) {
				l2 = Successor(l, l1);
				if(l2 == -1)
					return 0;
				if(l2 > vl.end)
					out = 1;
				else if(Qtest(r, l, l1, l2))
					out = 1;
				else {
					if(!Delete(l, l1))
						return 0;
					l1 = l2;
					if(IsLeftOf(r, l, l1))
						out = b = 1;
				}
			}
		}

		if(a)
			l = l1;
		else if(b)
			r = r1;
		else {
			if(Qtest(l, r, r1, l1))  //如果l1在外接圆外
				r = r1;
			else
				l = l1;
		}
	}//直到上公切线




	if(!Insert(l, r))
		return 0;

	if(!FixFirst(ut.to, ut.from))         //????
		return 0;
	if(!FixFirst(bt.from, bt.to))
		return 0;
	return 1;
}
bool RedBlackTree<T>::Remove(T item) {
    Node<T>* x = NULL;
    Node<T>* y = NULL;
    Node<T>* z = getNodeFromTree(root, item); // The node to be removed (it's value
    //   will be gone, and it is going to be replaced
    //   by the predecessor's value if a predecessor exists
    //   for this node, and the predecessor's node will be
    //   deleted, or delete this node if no predecessor exists).


    if (z == NULL) { // If no such item was found in the tree, then return false.
        return false;
    }


    if (z->left == NULL || z->right == NULL) { // If z has at most one child.
        y = z;
    } else { // z has two children.
        y = Predecessor(z);
    }

    if (y != NULL && y->left != NULL) { // The two conditions below are to
        //   find whether is y's only child
        //   is left or right.
        x = y->left;
    } else {
        if (y != NULL) {
            x = y->right;
        }
    }

    bool xIsLeft = false;
    Node<T>* xParent = NULL;
    if (x != NULL && y != NULL) { // If x is not NULL, detach x from y.
        x->p = y->p;
        xParent = y->p;
    }

    if (y != NULL && y->p == NULL) { // Check if y is root (i.e. it has no parent).
        root = x;
    } else {
        // Attach x to y's parent.
        if (y == y->p->left) { // y is a left child.
            y->p->left = x;
            xIsLeft = true;
        } else { // y is a right child.
            y->p->right = x;
            xIsLeft = false;
        }
    }

    if (y != NULL && z != NULL && y != z) { // Check to see if y has been moved up.
        z->data = y->data;
    }

    if (y != NULL && y->is_black == true) {
        if (x == NULL) {
            if (xParent == NULL) {
                if (root != NULL && root->right != NULL && root->left != NULL && CalculateHeight(root->right) > CalculateHeight(root->left)) {
                    x = root->right;
                    xParent = root;
                    xIsLeft = false;
                } else if (root != NULL && root->right != NULL && root->left != NULL && CalculateHeight(root->left) > CalculateHeight(root->right)) {
                    x = root->left;
                    xParent = root;
                    xIsLeft = true;
                } else if (root != NULL && root->left == NULL) {
                    x = root->right;
                    xParent = root;
                    xIsLeft = false;
                } else if (root != NULL && root->right == NULL) {
                    x = root->left;
                    xParent = root;
                    xIsLeft = true;
                }
            }
        }
        RBDeleteFixUp(x, xParent, xIsLeft);
    }
    delete y; // It can be the original predecessor's node, since its
    //   value has been moved up, or it can be z itself.
    --size; // Decrement the size counter.

    if (root != NULL && root->left != NULL && root->right == NULL) {
        if (root->left->left == NULL && root->left->right == NULL) {
            root->is_black = true;
            root->left->is_black = false;
        }
    } else if (root != NULL && root->right != NULL && root->left == NULL) {
        if (root->right->left == NULL && root->right->right == NULL) {
            root->is_black = true;
            root->right->is_black = false;
        }
    }

    return true;
}
Beispiel #7
0
bool RedBlackTree<T>::Remove(T item) {
	//if item not found return false
	if (Search(item) == false) {
		return false;
	}
	//Find the node z that has the value item 
	Node<T>*z = root;
	while (z != NULL) {
		if (item == z->data) {
			break;
		}
		else if (item < z->data) {
			z = z->left;
		}
		else {
			z = z->right;
		}
	}
	//item has at most one child
	Node<T>* y;
	Node<T>* x;
	if (z->left == NULL || z->right == NULL) {
		y = z;
	}
	else {
		y = Predecessor(z);
	}
	if (y->left != NULL) {
		x = y->left;
	}
	else {
		x = y->right;
	}
	//x->p = y->p; cant do this because x could be NULL
	//check if y is root, replace it by x
	if (y->p == NULL) {
		root = x;
	}
	else {
		if (y == y->p->left) {
			y->p->left = x;
		}
		else {
			y->p->right = x;
		}
		//need to check x is not null ptr
		if (x != NULL) {
			x->p = y->p;
		}
	}
	if (y != z) {
		z->data = y->data;
	}
	if (y->is_black == true) {

		if (x == y->p->left) {
			RBDeleteFixUp(x, y->p, true);
		}
		//x is rightchold
		else {
			RBDeleteFixUp(x, y->p, false);
		}
	}
	delete y;
	size--;
	return true;
}
void ShortestPath::CalculateShortestPath() {

	//The number of vertices in the graph.
	const unsigned int NumVertices = m_Graph -> getNumVertices();

	//The graph's adjacency matrix.
	double** ParentMatrix = m_Graph -> getMatrix();

	//Vector that stores the distance between the current vertices and the remaing vertices in the graph.
	vector<double> Distance(NumVertices , c_INFINITY);

	//Vector that stores the predecesor of each vertex.  Used to determine the path.
	vector<unsigned int> Predecessor(NumVertices , -1);

	//Vector that states if a vertex has been traversed before in the path or not.
	vector<unsigned int> Used;

	//The current vertex.
	unsigned int CurrentVertex;

	//A flag that states if the algorithm changed the path or not.  If not then the algorithm terminates.
	bool Change;
				 
	//Initialization.
	Distance[m_Source] = 0.0;
	m_Cost = 0.0;
	Predecessor[m_Source] = m_Source;
	CurrentVertex = m_Source;
	Used.push_back(m_Source);
	m_Path.clear();
	
	
	do {
		Change = false;

		//DO IT FOR ADJACENT ONLY.
		for(unsigned int j = 0; j < NumVertices; ++j) {
			if( Distance[CurrentVertex] + ParentMatrix[CurrentVertex][j] <  Distance[j] ) {
				Change = true;
				Distance[j] = Distance[CurrentVertex] + ParentMatrix[CurrentVertex][j];
				Predecessor[j] = CurrentVertex;
			}
		}
		
		//Find the next current vertex which is the minimum vector in Distance and not in Used.
		vector<double>::iterator MinimumLoc;
		MinimumLoc = Distance.begin();

		bool FirstElement = true;
		do {  
			if( (!FirstElement))
				MinimumLoc++;
			MinimumLoc = min_element(MinimumLoc, Distance.end()-1);
			FirstElement = false;
		}
		while( find(Used.begin(), Used.end(), MinimumLoc - Distance.begin() ) != Used.end());
		
		CurrentVertex = MinimumLoc - Distance.begin();
		Used.push_back(CurrentVertex);

		//Reached the destination vertex.
		if(CurrentVertex == m_Destination)
			break;

		//If the current vertex is INIFINTELY away from the source vertex,
		//then no path exists between the source and destination vertices.
		if(Distance[CurrentVertex] == c_INFINITY)
			throw NoPathFound( GraphUtils::getVertexName(*m_Graph, m_Source), GraphUtils::getVertexName(*m_Graph, m_Destination) );
		
	} while( (Change == true) && (Used.size() != NumVertices -1) );

	//Find the path from the destination to the source, by following the
	//Predecessor of the destination until you reach the source.
	cout<<"Path is: ";
	unsigned int lol = m_Destination;
	m_Path.push_back(m_Destination);
	do {
		lol = Predecessor[lol];
		m_Path.push_back(lol);
	}while(lol != m_Source);
	
	//The path is stored in reverse order, so reverse it to get the right
	//order.
	reverse(m_Path.begin(), m_Path.end());

	vector<unsigned int>::const_iterator LookAhead;
	//Dump the path and the cost.
	for(vector<unsigned int>::const_iterator L = m_Path.begin(); L != m_Path.end(); ++L) {

		//Get the next vertex in the path.
		LookAhead = L;
		if( (++LookAhead) != m_Path.end() )
			m_Cost += GraphUtils::getEdgeWeight(*m_Graph, *L, *LookAhead);

		cout << *L << " ";
	}
#if DEBUG
	cout << endl << "Cost of path: " << m_Cost << endl;
#endif
		
}