bool BST::insertToTree(PTree *pTree, DATA data)
{
    if (*pTree == NULL)
    {
        *pTree = new(std::nothrow) Tree();
        (*pTree)->data = (*pTree)->pLeft = (*pTree)->pRight = NULL;
    }
    
    if ((*pTree)->data == NULL)
    {
        return m_fCreate(&(*pTree)->data, data);
    }
    
    int result = m_fCmp((*pTree)->data, data);
    if (result == 0)
    {
        return true;
    }
    else if (result > 0)
    {
        return insertToTree(&(*pTree)->pLeft, data);
    }
    else
    {
        return insertToTree(&(*pTree)->pRight, data);
    }
}
Exemple #2
0
// Inserts the value provided into the given tree. Should
// follow the rule that smaller values are inserted into the
// left side of the tree; all other values are inserted into the
// right side of the tree.
//
// INPUT: root - The root of a tree to insert into, or
//               NULL if no tree yet exists.
//        data - The data to be inserted into the tree.
//
// OUTPUT: Returns the root of a tree containing the
//         newly inserted value.
Tree* insertToTree(Tree* root, int data)
{
    if(root == NULL)
    {
        return createTreeNode(data);
    }
    else
    {
        if(data < root->data)
        {
            if(root->left == NULL)
            {
                root->left = createTreeNode(data);
            }
            else
            {
                insertToTree(root->left,data);
            }
        }
        else if(data >= root->data)
        {
            if(root->right == NULL)
            {
                root->right = createTreeNode(data);
            }
            else
            {
                insertToTree(root->right,data);
            }
        }
        return root;
    }
}
Exemple #3
0
/* Entfernt einen Knoten aus einem Baum
 */
void removeFromTree(node * myNode, node ** tree)
{
	if (myNode == 0) { puts("ERROR! Node to remove is NULL!"); return;}
	if (*tree == 0) { puts("ERROR! Node to remove not found!"); return;}
	if (myNode == *tree)
	{
		/* myNode ist Wurzel von Baum.
		 * Nehme rechten Knoten als neue Root
		 * oder linken Knoten, wenn rechter = 0
		 */
		if (myNode->right != 0)
		{
			*tree = myNode->right;
			if (myNode->left != 0)
				insertToTree(myNode->left, tree);
		}
		else
			*tree = myNode->left; /* Darf 0 sein */
		myNode->left = 0;
		myNode->right = 0;
	}
	else if (myNode > *tree)
		removeFromTree(myNode, &((*tree)->right));
	else
		removeFromTree(myNode, &((*tree)->left));
}
Exemple #4
0
// Main function.
int main(int argc, char** argv)
{
    if(argc != 2)
    {
        puts("Invalid number of arguments!");
        return -1;
    }
    else
    {
        FILE *fptr = NULL;
        if((fptr = fopen(argv[1],"r")) != NULL)
        {
            char buffer[BUFSIZ];
            Tree* root = NULL;

            // Read data into a new binary tree and print it and it's height
            while(fgets(buffer,BUFSIZ,fptr) != NULL)
            {
                int data = atoi(buffer);
                root = insertToTree(root,data);
            }
            fclose(fptr);
            printf("Original tree has height %d:\n\n",getBstHeight(root));
            printTree(root);

            // Search tree for key
            printf("Enter key to search for: ");
            fgets(buffer,BUFSIZ,stdin);
            int searchKey = atoi(buffer);
            int result = searchTree(root,searchKey);
            if(result == 1) puts("\nTarget found in tree");
            else puts("\nTarget not found in tree");

            // Flatten tree to list
            puts("Flattened list:\n\n");
            List* newList = flattenTreeToList(root);
            printList(newList);
            freeList(newList);

            // Reflect tree, print it, and free it
            puts("Reflected Tree:\n\n");
            reflectTree(root);
            printTree(root);
            freeTree(root);
        }
        else
        {
            puts("Unable to open file!");
            return -1;
        }
    }
    return 0;
}
Exemple #5
0
/* Fuegt einen Knoten in einen Baum ein
 */
void insertToTree(node * myNode, node ** tree)
{
	if (myNode == 0) { puts("ERROR! Node to insert is NULL!"); return;}
	if (*tree == 0)
	{
		*tree = myNode;
	}
	else if (myNode > *tree)
	{
		if ((*tree)->right == 0)
			(*tree)->right = myNode;
		else
			insertToTree(myNode, &((*tree)->right));
	}
	else
	{
		if ((*tree)->left == 0)
			(*tree)->left = myNode;
		else
			insertToTree(myNode, &((*tree)->left));
	}
}
Exemple #6
0
/* Fuege einen neuen Freispeicher-Knoten
 * in die Liste der freien Knoten und
 * in den Freispeicherbaum ein
 */
void insertFreeNode(node * myNode)
{
	if (myNode == 0) return;

	int index = -1;
	if (myNode->size > MAXSIZE) index = 32;
	else index = myNode->size / 8 - 1;

	myNode->next = freeMem[index];
	freeMem[index] = myNode;

	insertToTree(myNode, &freeTree);
}
/*
parses curJSON and draws the tree.
marks the error location in case of a parsing error
*/
void JSONDialog::drawTreeSaxParse()
{
	HTREEITEM tree_root;
	tree_root = initTree(this->getHSelf());

	if (strlen(curJSON) == 0) 
	{
		insertToTree(this->getHSelf(), tree_root, "Error:Please select a JSON String.");
		TreeView_Expand(GetDlgItem(this->getHSelf(), IDC_TREE), tree_root, TVE_EXPAND);
		return;
	}
	
	populateTreeUsingSax(this->getHSelf(), tree_root, curJSON);
	TreeView_Expand(GetDlgItem(this->getHSelf(), IDC_TREE), tree_root, TVE_EXPAND);
}
Exemple #8
0
void* my_alloc(size_t size)
{
	/**********************************/
	/* Hole einen Freispeicher Knoten */
	/**********************************/
	node* freeNode = findFreeNode(size);
	if (freeNode == 0)
	{
		char* data = get_block_from_system();
		if (data == 0) return 0; /* System out of memory */
		freeNode = (node*)data;
		freeNode->size = BLOCKSIZE - offset;
		freeNode->left = 0;
		freeNode->right = 0;
        freeNode->next = 0;
	}
	else
		removeFreeNode(freeNode);

	/***********************************/
	/* Belege Platz im freien Speicher */
	/***********************************/
	/* Schritt 1: Pruefe ob Speicher uebrig bleibt */
	size_t leftover = freeNode->size - size;
    node* nLeftover = 0;

	if (leftover >= offset + ALLIGN)
	{
		/* Es ist noch Platz für mindestens 8 Byte
		 * erzeuge neuen Freispeicherknoten */
        nLeftover = (node*)( ((char*)freeNode) + offset + size);
        nLeftover->size = (int)leftover - offset;
        nLeftover->left = 0;
        nLeftover->right = 0;
        insertFreeNode(nLeftover);
        freeNode->size = (int) size;
	}

	/* Schritt 2: Freispeicherknoten
     durch Belegtspeicherknoten ersetzen */
	/* freeNode Pointer kann jetzt als Belegtspeicherpointer genutzt werden: */
	freeNode->left = 0;
	freeNode->right = 0;
	insertToTree(freeNode, &allocTree);

	/* Schritt 3: Neu belegten Speicher zurueckgeben */
	return ((char*)freeNode)+offset;
}
bool BST::insert(DATA data)
{
    return insertToTree(&m_pTree, data);
}