コード例 #1
0
ファイル: main.c プロジェクト: hudecek/algorithmsHW
void testSearch() {
    printf("Test 3. search: ");
    BinarySearchTree *tree = initTestTree();

    Node *node = searchNode(tree, 3);

    if ((node == NULL) || (node->key != 3)) {
        printf("NOK - chybne hledani korene s hodnotou 3\n");
    }else{
        node = searchNode(tree, 2);
        if ((node == NULL) || (node->key != 2)) {
            printf("NOK - chybne hledani listu s hodnotou 2\n");
        }else{
            node = searchNode(tree, 7);
            if (node != NULL) {
                printf("NOK - hledany prvek 7 se ve strome nevyskytuje\n");
            }else{
                printf("OK\n");
            }
        }
    }

    makeGraph(tree, "search.dot");
    printf("Vykresleny strom najdete v souboru search.dot\n");

    freeTree(tree);
}
コード例 #2
0
ファイル: serveraux.c プロジェクト: Raniato/ServerClient
int addTran(char *name1,char *name2, int amount)
{
	hashNode *node = searchNode(name2);
	hashNode *nodeInit = searchNode(name1);
	if(node==NULL || nodeInit==NULL) return 0;
	if(nodeInit->amount<amount) return 0;
	nodeInit->amount -=amount;
	node->amount+=amount;
	if(node->list==NULL){				//this account hasn't received any amounts from other accounts
		node->list = malloc(sizeof(listNode));
		node->list->name = malloc(strlen(name1)*sizeof(char));
		node->list->amount = amount;
		strcpy(node->list->name,name1);
		node->list->next=NULL;
	}
	else{						//this account has already received amounts from other accounts
		if(!strcmp(node->list->name,name1)){
			node->list->amount+=amount;	//already received amounts from this account
			return 1;
		}
		while(node->list->next!=NULL){
			if(!strcmp(node->list->name,name1)){
				node->list->amount+=amount;	//already received amounts from this account
				return 1;
			}
			node->list=node->list->next;
		}
		node->list->next = malloc(sizeof(listNode));
		node->list->next->name = malloc(strlen(name1)*sizeof(char));
		node->list->next->amount = amount;
		strcpy(node->list->next->name,name1);
		node->list->next->next=NULL;
	}
	return 1;
}
コード例 #3
0
ファイル: segmentTreeRmq.cpp プロジェクト: xiyanxiyan10/acm
const T * segmentTree<T>::searchNode(const ssize_t nodeIdx, const ssize_t lftIdx, const ssize_t rhtIdx, \
                                     const ssize_t lftQuery, const ssize_t rhtQuery)
{
    if(lftIdx > rhtQuery || rhtIdx < lftQuery)
    {
        return NULL;
    }
    if( lftQuery <= lftIdx && rhtQuery >= rhtIdx)
    {
        return (&tree[nodeIdx]);
    }
    ssize_t midIdx = (lftIdx + rhtIdx) >> 1;
    ssize_t curNode = nodeIdx;
    ssize_t lftNode = getLftNode(curNode);
    ssize_t rhtNode = getRhtNode(curNode);
    const T  *lftans = searchNode(lftNode, lftIdx, midIdx, lftQuery, rhtQuery);
    const T  *rhtans = searchNode(rhtNode, midIdx + 1, rhtIdx, lftQuery, rhtQuery);
    if(!lftans)
    {
        return rhtans;
    }
    if(!rhtans)
    {
        return lftans;
    }
    return (*lftans  < *rhtans ? lftans : rhtans);
}
void fun(AdjGraph *adjGraph,char a[M],char p,char q)
{
	int i,j,dist[M];
	int minIndex,minle,temp;
//	int	minLen[M]={0};
	int qDex=findIndex(a,adjGraph->vexnum,q);
	ArcNode *head=adjGraph->arcGraph;
	ArcList *arcList=NULL;
	for(i=0;i<adjGraph->vexnum;i++)
	{
		dist[i]=MAXL;
	}
	dist[findIndex(a,adjGraph->vexnum,p)]=-1;
	arcList=searchNode(head,p);
	updateDist(dist,adjGraph->vexnum,arcList,qDex,p,0);
	for(j=1;j<adjGraph->vexnum;j++)
	{		
		minIndex=findMinDist(dist,adjGraph->vexnum);
//		minLen[minIndex]=dist[minIndex];
		temp=dist[minIndex];
		if(minIndex==qDex)
		{
			minle=dist[minIndex];
			break;
		}
		dist[minIndex]=-1;
		arcList=searchNode(head,a[minIndex]);
		updateDist(dist,adjGraph->vexnum,arcList,qDex,a[minIndex],temp);
	}
	printf("%d\n",minle);

}
コード例 #5
0
void network::addLink(string parent, string child) {
    node* p = searchNode(parent);
    node* c = searchNode(child);

    (*c).addParent(p);
    (*p).addChild(c);
}
コード例 #6
0
ファイル: driver.c プロジェクト: AjaatShatru/DSA-BITS-Masti
node searchNode(node p, book *f){
    if(p==NULL)
        return p;
    else if(p->b->acc == f->acc)
        return p;
    else if(p->b->acc < f->acc)
        searchNode(p->right, f);
    else
        searchNode(p->left, f);
}
コード例 #7
0
struct node* searchNode(struct node *root, int value){
    if (root == NULL) return NULL;
    else {
         if (root->value == value) return root;
         else {
              if (value < root->value) return searchNode(root->left,value);
              else return searchNode(root->right,value);
         }
    }
}
コード例 #8
0
ファイル: main.c プロジェクト: FrankHXW/CUNIX
int main(void)
{
	printf("following is the test of binary tree:\n");
	while(1){
		struct btree *ptr_root=malloc(sizeof(struct btree));
		init_btree(ptr_root);
		printf("\nplease input binary tree in pre-order:\n");
		ptr_root=createBTree();	
		
		printf("\n\nprint btree in pre-order:\n");
		preOrder(ptr_root);
		
		printf("\n\nprint btree in mid-order:\n");
		midOrder(ptr_root);
		
		printf("\n\nprint btree in post-order:\n");
		postOrder(ptr_root);

		printf("\n\nthis btree's nodes' amount: \n%d",countAllNodes(ptr_root));		
		
		printf("\n\nthis btree's height: \n%d",getHeight(ptr_root));

		printf("\n\nthis btree's leaf's amount:\n%d",countLeaf(ptr_root));
				
		printf("\n\nprint btree's leaf:\n");
		displayLeaf(ptr_root);
		
		printf("\n\nfind 'a' and insert 'x' in left,then display all node in pre-oreder:\n");
		struct btree *foundRoot=searchNode(ptr_root,'a');
		insertLeftNode(foundRoot,'x');
		preOrder(ptr_root);

		printf("\n\nfind 'a' and insert 'y' in left,then display all node in pre-oreder:\n");
		foundRoot=searchNode(ptr_root,'a');
		insertRightNode(foundRoot,'y');
		preOrder(ptr_root);


		printf("\n\ndelete leftTree and display left node in pre-order:\n");
		deleteLeftTree(ptr_root);
		preOrder(ptr_root);

		printf ("\n\ndelete rightTree and display left node in pre-order:\n");
		deleteRightTree(ptr_root);
		preOrder(ptr_root);

		printf("\n\nclear btree!\n");
		clearBTree(ptr_root);
		//clear input buffer
		char ch;
		while((ch=getchar())!='\n'&&ch!=EOF);
	}

	return 0;
}
コード例 #9
0
ファイル: rbtree.c プロジェクト: inste/market
Node * searchNode(Node * a, T val) {
	if (a == NIL)
		return NIL;

	if (a->data == val)
		return a;
	if (a->data < val)
		return searchNode(a->right, val);
	else
		return searchNode(a->left, val);
}
コード例 #10
0
    vector<int> searchNode(TreeNode* root, int k1, int k2) {
        // write your code here
        if (root == nullptr) return list;
        int mid = root->val;
        
        if (mid >= k1 && mid <= k2) list.push_back(mid);

        if (mid < k2) searchNode(root->right, k1, k2);
        if (mid > k1) searchNode(root->left, k1, k2);    
        
        return list;
    }
コード例 #11
0
/*  Function to search an element in a Binary search tree  */
void searchNode(int i, node **n) {
	if(*n == NULL)
		printf("\nValue does not exist in tree!");
	else
		if((*n)->data == i)
			printf("\nValue found!");
		else
			if(i > (*n)->data)
				searchNode(i, &((*n)->rightChildNode));
			else
				searchNode(i, &((*n)->leftChildNode));
}
コード例 #12
0
ファイル: reroot.c プロジェクト: AndyShi12/Code-Example
void searchNode(node * tree, double * x, double * y, int n)
{
  if (tree == NULL) 
  return;
  if(tree->index==n)
  {
    *x = tree->xcoord;
    *y = tree->ycoord;
    return;
  }
  searchNode(TLEFT,  x,y,n);
  searchNode(TRIGHT, x,y,n);
}
コード例 #13
0
/*
@Function name : QuickSort
@Parameters : List to be sorted by Quicksort, first node of the list, last node of the list to be sorted
@Returns : Sorted list
@Descriptions : This function sorts the list according to quicksort methods
@Error Cases :
*/
list* quickSort(list* orgList, int low, int high)
{
	list_node* high_node = searchNode(orgList, high);
	list_node* low_node = searchNode(orgList, low);
	if (low < high )
	{	
		list_node *p = partition(low_node, high_node);
		int pivot = whereisNode(orgList, p);
		quickSort(orgList, low, pivot);
		quickSort(orgList, pivot+1, high);
	}
	return orgList;
}
コード例 #14
0
ファイル: main.c プロジェクト: ISimion/DSA-lab
int searchNode(NodeT* root, int value)
{
    if(root==NULL)
        return 0;
    else if(root->data==value)
        return 1;
  else
  {
      if(root->data < value)
        return searchNode(root->right, value);
      else
        return searchNode(root->left, value);
  }
}
コード例 #15
0
ファイル: Kruskal.cpp プロジェクト: abilng/MTech-Assignments
/* Description: Add a new edge to graph.
 * Input: Start node,End node and weight of edge
 * Output: NA.
 */
int Kruskal::addEdge(char startNodeName[], char endNodeName[], double label)
{
	struct GraphEdge* newEdge = new GraphEdge();

	newEdge->startNode = searchNode(startNodeName);
	newEdge->endNode = searchNode(endNodeName);
	if(!(newEdge->startNode && newEdge->endNode))
		return -1;
	newEdge->label = label;
	newEdge->nextEdge = NULL;
	lastGraphEdge->nextEdge = newEdge;
	lastGraphEdge = newEdge;

	return 0;
}
コード例 #16
0
    vector<int> searchRange(TreeNode* root, int k1, int k2) {
        // write your code here
        searchNode(root, k1, k2);   
        sort(list.begin(), list.end());

        return list;
    }
コード例 #17
0
StringTreeNode* StringBinaryTree::FindByString(string str)
{
  StringTreeNode searchNode(str); 

  StringTreeNode* pRes = (StringTreeNode*) Find(&searchNode);
  return pRes;
}
コード例 #18
0
ファイル: bstree.cpp プロジェクト: KyssLi/CLRS-Code
	void deleteNode(const T &key)
	{
		std::pair<BSTNode<T>*, bool> pos_msg = searchNode(key);
		if (!pos_msg.second)
			return;
		if (pos_msg.first->left_child == nullptr)//delete node with only right_child
			deleteNodeAuxiliary(pos_msg.first, pos_msg.first->right_child);
		else if (pos_msg.first->right_child == nullptr)
			deleteNodeAuxiliary(pos_msg.first, pos_msg.first->left_child);
		else
		{
			auto p = minimumPtr(pos_msg.first->right_child);//use "successor": actually use "minimumPtr"
			if (p->parent != pos_msg.first)
			{
				deleteNodeAuxiliary(p, p->right_child);
				p->right_child = pos_msg.first->right_child;
				p->right_child->parent = p;
			}
			deleteNodeAuxiliary(pos_msg.first, p);
			p->left_child = pos_msg.first->left_child;
			p->left_child->parent = p;
		}
		delete pos_msg.first;
		pos_msg.first = nullptr;
	}
コード例 #19
0
ファイル: Grid.c プロジェクト: FrOOmiX/Etude_cas
/** \brief Check if the word has already be set
 *
 * \param Trie : the dictionnary
 * \param Trie : words set in the grid
 * \param Cell[][] : grid of cell
 * \param int[][] : position of each letter
 * \return int : score of the word
 *
 */
int scoreWord(Trie *t, Trie *tGrid, Cell grid[N][N], int coord[17][2]) {

    int scoreWord = 0;
    int *pScoreWord = &scoreWord;
    char *word = getWordFromCoord(grid, coord);
    int lengthWord = strlen(word);

    // if the word has already been set
    if (searchNode(tGrid, word)) {

        printf("Word already found\n");
    } else {

        // If the word exist in dictionnary
        if (searchWordTrie(t, word)) {

            scoreTotal(grid, coord, pScoreWord, lengthWord);
        }
    }

    // insert in the trie the word set
    insertNode(tGrid, word);
    free(word);

    return *pScoreWord;
}
コード例 #20
0
/* The main() program begins */
int main()
{
	int ch, num, num1;
	do {
		printf("\nSelect a choice from the menu below.");
		printf("\n1. Insert a node.");
		printf("\n2. Search for a node.");
		printf("\nChoice: ");
		scanf("%d", &ch);
		switch(ch) {
			case 1: printf("\nEnter an element: ");
				scanf("%d", &num);
				insertNode(num, &rootNode);
				break;

			case 2: printf("\nEnter the element to be searched for: ");
				scanf("%d", &num);
				searchNode(num, &rootNode);
				break;

			default: exit(0);
		}
		printf("\nIf you want to return to the menu, press 1.");
		printf("\nChoice: ");
		scanf("%d", &num);
	} while(num == 1);

	return 0;
}
コード例 #21
0
ファイル: dfs.c プロジェクト: vaibhavbhat16/Seekers
void moveToNextNode(int currentNodeID ,int nextNodeID)    //Decides on which  node to move to
{
    vertexNode* v = searchNode(currentNodeID);
    //Found the vertex node
    //Create a temp head edgeNode

    edgeNode* temp_head = v->head; 
    orientation direction ;
    while(temp_head!=NULL) 
    {
        if(temp_head-> id == nextNodeID){
            direction = temp_head->turn;
            //lastTurn = direction;
		//lastPositionSeen = lastTurn;
            distanceToNextNode = temp_head->distance;
            break;
    }

        temp_head = temp_head->next;
    }   

    moveCar(direction, distanceToNextNode);
	lastTurn  = direction;
    printf("Node explored : %d \n", currentNodeID);
    // Rotate based on current orientation 
    // Set the timers

    // Actuate Motors
    // Keep updating currentDistanceTravelled

}
コード例 #22
0
void Painting::unloadMemory(int n){
	
	node *ptr = searchNode(n);
	if(ptr == NULL)
		cout<<"No Node with ID: "<<n<<endl;
	if(ptr == head){
		//head = ptr->next;
		head = NULL;
		cout<<"ID: "<<n<<" is removed from Memory"<<endl;
		return;
	}
	node *cur = head;
	node *prev = head;

	while(cur){
		if(cur == ptr){
			prev->next = cur->next;
			
			return;
		}
		prev = cur;
		cur= cur->next;
	}
	
}
コード例 #23
0
ファイル: linked_list.c プロジェクト: ozt88/NEXT_14_FIRST
void practice67(list_t list)
{
	node_t* newNode1 = (node_t*)malloc(sizeof(node_t));
	node_t* newNode2 = (node_t*)malloc(sizeof(node_t));
	node_t* currentPtr;

	printf("\n주어진 노드(40) 다음에 새 노드(45) 추가하는 함수 구현\n맨앞(NULL뒤)에 새 노드(5) 추가하는 함수 구현\n\n");


	newNode1->value = 45;
	newNode2->value = 5;
	printf("전체 리스트\n");
	printAllNodes(&list);
	currentPtr = searchNode(&list, 40);
	insertNode(&list, currentPtr, newNode1);
	printf("40뒤에 45추가\n");
	printAllNodes(&list);
	insertNode(&list, NULL, newNode2);
	printf("맨 앞에 5 추가\n");
	printAllNodes(&list);

	fflush(stdin);
	getchar();

	return;
}
コード例 #24
0
ファイル: find.cpp プロジェクト: yamamoto123/tapur_open
//search child nodes recursivly, then self
void *searchDeep(spConfigNode*node,char *name,void*ref,char type,char found)
{
	int i;
	void *ret=NULL;
	ENTRY;

	if(ref==NULL)
		found=1;

	for(i=0;i<node->numChildren;++i)
	{
		ret=searchDeep(node->children[i],name,ref,type,found);
		if(ret)
		{
			if(ret==(void*)-1)
			{
				DEBUG(-125,"Looks like we found something\n");
				++found;
			}
			else
				return(ret);
	}
	}

	//ok, so it wasn't there, but maybe ref was, lets look in root node
	return(searchNode(node,name,ref,type,found));
	
}
コード例 #25
0
ファイル: tree.c プロジェクト: storybook808/Tree
/* 
 * The deleteNode is incomplete.  It only deletes nodes
 * that are leaves.  Otherwise, it doesn't change the tree.
 */
void deleteNode(Node ** ptr2link) 
{
Node ** nodeptrptr;
Node * nodeptr;
Node * temp;
Node * temp2;
int event = 0;

char c;
/* Get the alphabet to delete */
while(1) {
   printf("Enter alphabet ('a' - 'z'): ");
   c = getchar();
   while(getchar() != '\n'); /* get rid of extra characters from stdin */
   if (c < 'a' || c > 'z') printf("\n Try again \n");
   else break;
}

/* Search for the node */
nodeptrptr = searchNode(c, ptr2link);
if (nodeptrptr == NULL) return; /* Couldn't find it */

/* Delete the node */
nodeptr = *nodeptrptr;
if (nodeptr->left == NULL && nodeptr->right == NULL) { /* A leaf */
   *nodeptrptr = NULL; /* Delete the node from the tree  by NULLing the
                          link from the parent node */
   destroyNode(nodeptr); /* Deallocate space back to memory */
}else if (nodeptr->left == NULL && nodeptr->right != NULL) {
   *nodeptrptr = nodeptr->right;
   destroyNode(nodeptr);
}else if (nodeptr->left != NULL && nodeptr->right == NULL) {
   *nodeptrptr = nodeptr->left;
   destroyNode(nodeptr);
}else if (nodeptr->left != NULL && nodeptr->right != NULL) {
   temp = nodeptr->right;
   temp2 = nodeptr;
   while(temp->left != NULL){
      temp2 = temp;
      temp = temp->left;
      event = 1;
   }if(temp->right != NULL){
      if(event == 1){
         temp2->left = temp->right;
      }else{
         temp2->right = temp->right;
      }
   }else{
      if(event == 1){
         temp2->left = NULL;
      }else{
         temp2->right = NULL;
      }
   }temp->right = nodeptr->right;
   temp->left = nodeptr->left;
   *nodeptrptr = temp;
   destroyNode(nodeptr);
}
}
コード例 #26
0
ファイル: main.c プロジェクト: cupsadarius/SDA
int main(void) {
    int ch, num, num1;
    do {
        printf("\nSelect a choice from the menu below.");
        printf("\n1. Insert a node.");
        printf("\n2. Search for a node.");
        printf("\n3. Delete a node.");
        printf("\n4. Display the Binary Search Tree.");
        printf("\nChoice: ");
        scanf("%d", &ch);
        switch(ch) {
            case 1: printf("\nEnter an element: ");
                    scanf("%d", &num);
                    //printf("YESYES");
                    insertNode(num, &rootNode);
                    break;

            case 2: printf("\nEnter the element to be searched for: ");
                    scanf("%d", &num);
                    searchNode(num, rootNode);
                    break;

            case 3: printf("\nEnter the element to be deleted: ");
                    scanf("%d", &num);
                    deleteNode(num, rootNode);
                    break;

            case 4: printf("\nSelect an order for the elements to be display in.");
                    printf("\n1. Pre-order.");
                    printf("\n2. Post-order.");
                    printf("\n3. In-order.");
                    printf("\nChoice: ");
                    scanf("%d", &num1);
                    switch(num1) {
                        case 1: printf("\nPre-order Display: ");
                                displayPreOrder(rootNode);
                                break;

                        case 2: printf("\nPost-order Display: ");
                                displayPostOrder(rootNode);
                                break;

                        case 3: printf("\nIn-order Display: ");
                                displayInOrder(rootNode);
                                break;

                        default: exit(0);
                    }
                    break;

            default: exit(0);
            }
        printf("\nIf you want to return to the menu, press 1.");
        printf("\nChoice: ");
        scanf("%d", &num);
    } while(num == 1);

    return 0;
}
コード例 #27
0
ファイル: test0.cpp プロジェクト: GHScan/DailyProjects
 bool insert(const T& val)
 {
     Node** node = searchNode(val);
     if (*node != NULL) return false;
     *node = new Node(val);
     ++m_size;
     return true;
 }
コード例 #28
0
ファイル: linked_list.c プロジェクト: ozt88/NEXT_14_FIRST
void practice5(list_t list)
{
	node_t* currentPtr;

	printf("\n주어진 key를 갖는 노드 찾는 함수 구현 70 & 100\n\n");

	printf("전체 리스트: ");
	printAllNodes(&list);
	currentPtr = searchNode(&list, 70);
	currentPtr ? printf("%d is found\n", currentPtr->value) : printf("Key is Not Found!\n");
	currentPtr = searchNode(&list, 100);
	currentPtr ? printf("%d is found!\n", currentPtr->value) : printf("Key is Not Found!\n");

	fflush(stdin);
	getchar();
	return;
}
コード例 #29
0
ファイル: Parameters.c プロジェクト: CALlanoR/MAPiCO-2.0.0
/**
* getNameFunction
*/
char* getNameFunction(Parameters argv, int NumInstruction)
{
   Instruction instruction;
   
   instruction = searchNode(argv.Instructions, NumInstruction);
   
   return instruction->NameFunction;
}//getNameFunction
コード例 #30
0
ファイル: Parameters.c プロジェクト: CALlanoR/MAPiCO-2.0.0
/**
* getIndexPlugIn
*/
int getIndexPlugIn(Parameters argv, int NumInstruction)
{
   Instruction instruction;
   
   instruction = searchNode(argv.Instructions, NumInstruction);
   
   return instruction->IndexPlugIn;   
}//getIndexPlugIn