コード例 #1
0
int main(void){

  int i=0, array[]={34,12,65,23,2,100,120};
  Node* root = NULL;
  root = TreeInit(49);

  for (i=0;i<7;++i){
    NodeInsert(root,array[i]);
  }

  InorderPrintTree(root);

  putchar('\n');

  PostorderPrintTree(root);

  printf("\nAddress of node with key=13 is %p\n",(void *)TreeSearch(root,13));

  printf("Minimum : %d \n", (TreeMinimum(root))->key);

  printf("Maximum : %d \n", (TreeMaximum(root))->key);

  printf("Treesize : %d \n", TreeSize(root));
  
  printf("Max depth : %d \n", MaxDepth(root));

  TreeDelete(root);

getchar();
return 0;
}
コード例 #2
0
ファイル: Tree.c プロジェクト: Adamwuming/paho.mqtt.c
Node* TreeNextElementIndex(Tree* aTree, Node* curnode, int index)
{
	if (curnode == NULL)
		curnode = TreeMinimum(aTree->index[index].root);
	else
		curnode = TreeSuccessor(curnode);
	return curnode;
}
コード例 #3
0
ファイル: RedBlackTree.cpp プロジェクト: cfanchk/Code
//红黑树取后继元素操作
Node& RBTree::TreeSuccessor(int key)
{
	Node* x = &TreeSearch(key);
	if (x->getRight() != NIL)
		return TreeMinimum(x->getRight(), 0);
	Node* y = x->getParent();
	while (y != NIL && x == y->getRight())
	{
		x = y;
		y = y->getParent();
	}
	if (y == NIL)
		throw 1;
	return *y;
}
コード例 #4
0
ファイル: Tree.c プロジェクト: Adamwuming/paho.mqtt.c
Node* TreeSuccessor(Node* curnode)
{
	if (curnode->child[RIGHT])
		curnode = TreeMinimum(curnode->child[RIGHT]);
	else
	{
		Node* curparent = curnode->parent;
		while (curparent && curnode == curparent->child[RIGHT])
		{
			curnode = curparent;
			curparent = curparent->parent;
		}
		curnode = curparent;
	}
	return curnode;
}
コード例 #5
0
ファイル: RedBlackTree.cpp プロジェクト: cfanchk/Code
//红黑树删除操作
void RBTree::TreeDelete(int key)
{
	Node* z = &TreeSearch(key);
	Node* y = z;
	Node* x;
	bool y_original_colour = y->getColour();

	if (z->getLeft() == NIL)
	{
		x = z->getRight();
		transplant(z, z->getRight());
	}
	else if (z->getRight() == NIL)
	{
		x = z->getLeft();
		transplant(z, z->getLeft());
	}
	else
	{
		y = &(TreeMinimum(z->getRight(), 0));
		y_original_colour = y->getColour();
		x = y->getRight();
		if (y->getParent() == z)
			x->setParent(y);
		else
		{
			transplant(y, y->getRight());
			y->setRight(z->getRight());
			y->getRight()->setParent(y);
		}
		transplant(z, y);
		y->setLeft(z->getLeft());
		y->getLeft()->setParent(y);
		y->setColour(z->getColour());
	}
	if (y_original_colour == BLACK)
		deleteFixup(x);
	delete z;
}
コード例 #6
0
/*
* This function is used to find the tree node that is the successor
* of the node searchNode
*
* @param searchNode node to start the search at main tree
* @return auxNode the node succeeding searchNode
*/
RBTNode *Successor(RBTNode *searchNode)
{
	if (searchNode == NULL)
	{
		return NULL;
	}

	//find the minimum value of the right subtree if it exists
	if (searchNode->rightChild != NULL)
	{
		return TreeMinimum(searchNode->rightChild);
	}
	
	RBTNode *auxNode = searchNode->parent;

	//go up the tree to find the successor
	while (auxNode != NULL && searchNode == auxNode->rightChild)
	{
		searchNode = auxNode;
		auxNode = auxNode->parent;
	}

	return auxNode;
}
コード例 #7
0
ファイル: co_tree.c プロジェクト: Strongc/proview
main ()
{
    tree_sTable	*tp;
    sNode		*np;
    int		key;
    int		i;
    char		s[256];
    char		*cp;
    char		c;

    tp = tree_CreateTable(sizeof(int), offsetof(sNode, key), sizeof(sNode), 100, tree_eComp_int32, NULL);

    for (i = 0; i < 1000; i += 10) {
        tree_Insert(tp, &i);
    }

    for ( ;;) {
        printf("Command: ");
        cp = gets(s);
        c = s[0];
        if (cp == NULL || c == '\0') {
            printf("\nGoodbye\n");
            return;
        }
        switch (c) {
        case 'i':
        case 'I':
            printf("Insert, Key: ");
            gets(s);
            key = atoi(s);
            if (tree_Find(tp, &key) == NULL) {
                tree_Insert(tp, &key);
            } else
                printf("\nKey allready exists!\n");
            break;
        case 'd':
        case 'D':
            printf("Delete, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_Find(tp, &key)) != NULL) {
                tree_Remove(tp, &key);
            } else
                printf("\nKey does not exist!\n");
            break;
        case 'f':
        case 'F':
            printf("Find, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_Find(tp, &key)) == NULL) {
                printf("\nKey does not exist!\n");
            } else
                printf("\nKey exists! %d\n", np->key);
            break;
        case 's':
        case 'S':
            printf("Find successor, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_FindSuccessor(tp, &key)) == NULL) {
                printf("\nKey does not exist!\n");
            } else
                printf("\nKey exists! %d\n", np->key);
            break;
        case 'p':
        case 'P':
            printf("Find predecessor, Key: ");
            gets(s);
            key = atoi(s);
            if ((np = tree_FindPredecessor(tp, &key)) == NULL) {
                printf("\nKey does not exist!\n");
            } else
                printf("\nKey exists! %d\n", np->key);
            break;
#if 0
        case 't':
        case 'T':
            printf("Start: ");
            gets(s);
            start = atoi(s);
            printf("Stop: ");
            gets(s);
            stop = atoi(s);
            printf("Order: ");
            gets(s);
            c = s[0];
            switch (c) {
            case 's':
            case 'S':
                printf("\navl-tree\n");
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    if (TreeSearch(tp,i) == tp->Null) {
                        np = TreeAlloc(tp, i);
                        TreeInsert(tp, np);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);

                i = start;
                j = stop;
                printf("\nlib$tree\n");
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    sts = lib$lookup_tree(&ltp, i, Compare, &lnp);
                    if (!(sts & 1)) {
                        lib$insert_tree(&ltp, i, &0, Compare, Alloc, &lnp, 0);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'd':
            case 'D':
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    if ((np = TreeSearch(tp,i)) != tp->Null) {
                        TreeDelete(tp, np);
                    } else {
                        printf("Could not find %d\n", i);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'f':
            case 'F':
                printf("\navl-tree\n");
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    if ((np = TreeSearch(tp,i)) != tp->Null) {
                    } else {
                        printf("Could not find %d\n", i);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);

                i = start;
                j = stop;
                printf("\nlib$tree\n");
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    sts = lib$lookup_tree(&ltp, i, Compare, &lnp);
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'b':
            case 'B':
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; j--) {
                    if (TreeSearch(tp,j) == tp->Null) {
                        np = TreeAlloc(tp, j);
                        TreeInsert(tp, np);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            case 'r':
            case 'R':
                i = start;
                j = stop;
                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                cpu = cputim;
                page = pageflts;

                for (; i <= j; i++) {
                    k = 65535 & rand();
                    if (TreeSearch(tp,k) == tp->Null) {
                        np = TreeAlloc(tp, k);
                        TreeInsert(tp, np);
                    }
                }

                sts = sys$getjpiw(0, &pid, 0,item_list, 0, 0, 0);
                printf("Cputim: %d, Pageflts: %d\n", cputim - cpu, pageflts - page);
                break;
            default:
                printf("Illegal order!\n");
                break;
            }
            break;
        case 'p':
        case 'P':
            tp->ErrorCount = 0;
            tp->HZCount = 0;
            tp->HNCount = 0;
            tp->HPCount = 0;
            maxlevel = 0;
            count = 0;
            hight = 0;
            TreePrint(tp, tp->Root, NULL, NULL,0);
            TreeCheck (tp, tp->Root, &count, &maxlevel, &hight, 0);
            printf("Hight: %d\n", hight);
#if 0
            TreePrintInorder(tp, tp->Root, 0);
#endif
            sp = TreeMinimum(tp, tp->Root);
            ep = TreeMaximum(tp, tp->Root);
            op = sp;
            for (np = TreeSuccessor(tp, op); op != ep; np = TreeSuccessor(tp, np)) {
#if 0
                printf("Key: %d\n", op->Key);
#endif
                if (op->Key >= np->Key)
                    tp->ErrorCount++;
                op = np;
            }
            printf("Hight.......: %d\n", hight);
            printf("Insert......: %d\n", tp->Insert);
            printf("Search......: %d\n", tp->Search);
            printf("Delete......: %d\n", tp->Delete);
            printf("NodeCount...: %d\n", tp->NodeCount);
            printf("FreeCount...: %d\n", tp->FreeCount);
            printf("MaxDepth....: %d\n", tp->MaxDepth);
            printf("HZCount.....: %d\n", tp->HZCount);
            printf("HNCount.....: %d\n", tp->HNCount);
            printf("HPCount.....: %d\n", tp->HPCount);
            printf("LLCount.....: %d\n", tp->LLCount);
            printf("LRCount.....: %d\n", tp->LRCount);
            printf("RLCount.....: %d\n", tp->RLCount);
            printf("RRCount.....: %d\n", tp->RRCount);
            printf("AllocCount..: %d\n", tp->AllocCount);
            printf("MallocCount.: %d\n", tp->MallocCount);
            printf("ErrorCount..: %d\n", tp->ErrorCount);
            count = maxlevel = 0;
            Print(ltp, &count, &maxlevel, 0);
            printf("\nlib$tree\n");
            printf("Count.......: %d\n", count);
            printf("MaxDepth....: %d\n", maxlevel);
            break;
        case 'l':
        case 'L':
            TreePrintInorder(tp, tp->Root, 0);
            break;
#endif
        case 'q':
        case 'Q':
            printf("\nGoodbye\n");
            return;
            break;
        default:
            printf("Illegal command!\n");

            break;
        }
    }
}