예제 #1
0
void Modify()
{
	NODE * cur;
	char ch;

	Printall();
	printf("대문자 또는 소문자를 변경할 문자 : ");
	fflush(stdin);
	scanf("%c", &ch);
	cur = _Search(ch);

	if(cur == NULL)
	{
		printf("해당 문자를 찾지 못했습니다.\n");
		return;
	}
	else
	{
		if(cur->data >= 'a' && cur->data <= 'z')
			cur->data -= 32;
		else if(cur->data >= 'A' && cur->data <= 'Z')
			cur->data += 32;
	}
	printf("\n");
	Printall();
}
예제 #2
0
Elem *TTFTree<Elem>::Insert(const Elem &element) {
    Elem *searchResult = nullptr;
    bool found = _Search(element, &searchResult, nullptr);
    if (found) {
        return searchResult;
    }

    /* else not found */
    if (m_head == nullptr) {
        m_head = new TTFNode<Elem>(&element);

        return m_head->GetFirstElement();
    }

    else { // contains value
        TTFNode<Elem> *currentNode = m_head;
        TTFNode<Elem> *nextNode = m_head;
        bool found = false;

        found = _CheckForInsert(currentNode, element, nullptr, &nextNode);
        while (nextNode != nullptr) {
            assert(found == false);
            currentNode = nextNode;
            found = _CheckForInsert(currentNode, element, nullptr, &nextNode);
        }
        assert(currentNode->_IsLeaf() && !currentNode->Is4Node());

        return currentNode->Insert(element);
    }
}
예제 #3
0
bool TTFTree<Elem>::Delete(const Elem &element) {
    Elem *retElem = nullptr;
    
    // modify : lipengfei 2015-06-28
    // here do some change, if found and is leaf, and is not 3/4node or head, delete.
    // bool found = _Search(element, &retElem, nullptr); changed to bellow
    TTFNode<Elem> *foundNode = nullptr;
    bool found = _Search(element, &retElem, &foundNode);
    if (found && foundNode->_IsLeaf()
        && (foundNode == m_head || foundNode->Is3Node() || foundNode->Is4Node())) {
        foundNode->Delete(element);
        return true;
    }

    // else traval with the invaiant: current node is not 2-node(except for head);
    if (found) {
        TTFNode<Elem> *currentNode = m_head;
        TTFNode<Elem> *nextNode = m_head;
        bool findInLeaf = false;
        Elem *retElem2 = nullptr;    // used to check

        while (nextNode != nullptr) {
            findInLeaf = _CheckForDelete(currentNode, element, &retElem2, &nextNode);
            if (findInLeaf) {
                assert(retElem == retElem2); // check 
                assert(!nextNode->Is2Node() || nextNode == m_head);

                nextNode->Delete(element);
                return true;
            }
            else if (!findInLeaf && retElem2 != nullptr) {// find but not in leaf
                assert(retElem == retElem2); // check 

                currentNode = _GetSuccessor(nextNode);

                // exchange and delete
                *retElem = *currentNode->GetFirstElement();
                delete currentNode->GetFirstElement();
                currentNode->SetFirstElement(currentNode->GetSecondElement());
                currentNode->SetSecondElement(currentNode->GetThirdElement());
                currentNode->SetThirdElement(nullptr);

                return true;
            }
            else { // not found
                currentNode = nextNode;
            }
        }
        assert(false);
        return false;
    }
    else {
        return false;
    }
}
예제 #4
0
int BinarySearch::_Search(int lo, int hi, int value)
{
    if (lo > hi)
    {
        return -1;
    }
    else
    {
        int mid = (lo + hi) / 2;
        if (value > m_arrayPtr[mid])
        {
            _Search(mid + 1, hi, value);
        }
        else if (value < m_arrayPtr[mid])
        {
            _Search(lo, mid - 1, value);
        }

        return mid;
    }
}
예제 #5
0
void Search()
{
	NODE * cur;
	char ch;

	Printall();
	
	fflush(stdin);
	printf("검색문자 : ");
	scanf("%c", &ch);

	cur = _Search(ch);
	
	if( cur == NULL)
	{
		printf("못 찾았습니다.\n");
	}
	else
	{
		printf("검색결과 = 주소값 : %p, 데이터 : %c, : 다음 주소값 : %p\n", 
			cur, cur->data, cur->next);
	}
}
예제 #6
0
int BinarySearch::RecursiveFind(int a)
{
    return _Search(0, m_size, a);
}