示例#1
0
Tree* TreeSearch(Tree* tree, void* element, int (*comp)(const void*, const void*))
{
  int res = (*comp)(element, tree->data); 
  if ( res < 0 )
    return tree->left ? TreeSearch(tree->left, element, comp) : NULL;
  else if ( res > 0 )
    return tree->right ? TreeSearch(tree->right, element, comp) : NULL;
  else
    return tree;
}
示例#2
0
/* TreeSearch: search for target starting at node root.
Pre:   The tree to which root points has been created.
Post: The function returns a pointer to a tree node that matches targetor
NULL if the target is not in the tree. */
   arv_bin * TreeSearch(arv_bin *root, char * target)
   {
      if (root)
         if (LT(target, root->info))
            root = TreeSearch(root->left, target);
         
         else if (GT(target, root->info))
            root = TreeSearch(root->right, target);
   
      return root;
   }
示例#3
0
//红黑树查询操作
Node& RBTree::TreeSearch(int key, Node* node, bool isFirstCall)
{
	if (isFirstCall == 1)
		node = Treepoint;
	if (node == NIL)
		throw 0;
	else if (key == node->getKey())
		return *(node);

	if (key < node->getKey())
		return TreeSearch(key, node->getLeft(), 0);
	else
		return TreeSearch(key, node->getRight(), 0);
}
int main()
{
    // create the graph given in above fugure
    int V = 0;
    int src;
    //printf("Enter the number of nodes\n");
    scanf("%d",&V);
    int *costMatrix=(int *) malloc(sizeof(int)*V*V);
    struct Graph* graph = createGraph(V);
    //printf("Enter the Number of Edges you want to add\n");
    int i=0,E=0;
    scanf("%d",&E);
    //printf("Enter the links and their costs\n");
    int n1,n2,cost;
    for(i=0;i<E;i++)
    {
        
        scanf("%d %d %d",&n1,&n2,&cost);
        addEdge(graph,n1,n2,cost,costMatrix,V);
    }
    //printf("Enter the source Node\n");
    scanf("%d",&src);
    int k=0,l=0;
    double x=omp_get_wtime();
    TreeSearch(graph,src,V,costMatrix);
    double y=omp_get_wtime();
    printf("%f\n",y-x);

    return 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;
}
/*
* This function is used to find the tree node with specified key
* from the subtree starting at searchNode
*
* @param searchNode node to start the search at main tree
* @param key key value to find in the subtree
* @return searchNode the node with the specified key, or NULL
*/
RBTNode *TreeSearch(RBTNode *searchNode, int key)
{
	//if searchNode is null or we are already at the node
	if (searchNode == NULL || key == searchNode->key)
	{
		return searchNode;
	}
	
	//otherwise, search the tree given its value
	if (key < searchNode->key)
	{
		return TreeSearch(searchNode->leftChild, key);
	}
	else
	{
		return TreeSearch(searchNode->rightChild, key);
	}
}
示例#7
0
int TreeDelete(Tree* tree, void* element, int (*comp)(const void*, const void*))
{
  Tree* toDelete = TreeSearch(tree, element, comp);
  if ( toDelete )
  {
    TreeDoDelete(toDelete);
    return 1;
  }
  return 0;
}
示例#8
0
Result AVLTree::TreeDelete(char key)
{
	TreeNode* delNode = TreeSearch(key);
	if (delNode == NULL) 
	{
		return NO_SUCH_WORD;
	}
	TreeNodeDelete(delNode);
	count--;
	return OK;
}
示例#9
0
//红黑树取前驱元素操作
Node& RBTree::TreePredecessor(int key)
{
	Node* x = &TreeSearch(key);
	if (x->getLeft() != NIL)
		return TreeMaximum(x->getLeft(), 0);
	Node* y = x->getParent();
	while (y != NIL && x == y->getLeft())
	{
		x = y;
		y = y->getParent();
	}
	if (y == NIL)
		throw 1;
	return *y;
}
示例#10
0
void MainProcess::Destroy(str PID)
{
	//Check for process existence
	std::vector<str>::iterator findIt = std::find(nameList.begin(), nameList.end(), PID);

	if (findIt == nameList.end()){
		std::cout << "error ";
		ss << "error ";

		return;
	}

	//Remove from name list
	nameList.erase(findIt);

	PCB* temp;

	//Search the ready list for the PCB with the specified PID
	for (int i = 1; i < 3; i++){
		//for (PCB* p : readyList[i])
		for (std::list<PCB*>::iterator it = readyList[i].begin(); it != readyList[i].end(); it++)		
		{
			if ((*it)->getPID() == PID){
				temp = *it;
				readyList[i].erase(it);
				KillTree(temp);
				
				Scheduler();
				return;
			}
		}
	}


	temp = TreeSearch(PID, &initProcess);

	
	if (temp->getPID() != PID){
		std::cout << "error ";
		ss << "error ";
		return;

	}
	
	KillTree(temp);
	//Scheduler();
	return;
}
示例#11
0
PCB* MainProcess::TreeSearch(str pID, PCB* p){

	if (p->getPID() != pID){

		PCB* temp;
		if (!p->children.empty()){
			for (PCB* q : p->children){
				temp = TreeSearch(pID, q);

				if (temp->getPID() == pID)
					return temp;
			}
		}
	}


	
	return p;
		

}
示例#12
0
//红黑树删除操作
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;
}
示例#13
0
// Функция main()
int main(){
 FieldOpen();
 TreeSearch();
}
示例#14
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;
        }
    }
}