Beispiel #1
0
int main()
{
    list_t l = make_list();

    char _[] = "deadbeef";
    char *msg = _;
    for (;*msg != '\0';msg++) {
        insert(*msg, header(l));
    }
    traversal(print_node, l);
    sep;
    printf("%c", find('a', l)->e);
    sep;
    insert('v', find('a', l));
    traversal(print_node, l);
    sep;
    traversal(print_node, pop('e', l));
    sep;
    destory(l);
    printf("%d %d", is_empty(l), is_last(header(l)));
    sep;

    return 0;
}
    vector<int> findFrequentTreeSum(TreeNode* root) {
        vector<int> res;
        if(!root) return res;
        unordered_map<int, int> mp;
        int maxFreq = 0, sum = 0;
        traversal(root, mp);
	//note here the first traverse can be replaced if we define a maxFreq
        for(auto it: mp){
            if(it.second > maxFreq)
                maxFreq = it.second;
        }
        for(auto it : mp){
            if(it.second == maxFreq)
                res.push_back(it.first);
        }
        return res;
    }
	void traversal(int m, int n, int i, int j, int count, int z, int arr[][10])
	{

		for(;j<m-1;j++)
		{
			if(count<=z)
			{
				printf("%d\t",arr[i][j]);
				count++;
			}
		}

		for(;i<n-1;i++)
		{
			if(count<=z)
			{
				printf("%d\t",arr[i][j]);
				count++;
			}
		}

		for(;j>0;j--)
		{
			if(count<=z)
			{
				printf("%d\t",arr[i][j]);
				count++;
			}
		}

		for(;i>0;i--)
		{
			if(count<=z)
			{
				printf("%d\t",arr[i][j]);
				count++;
			}
		}
i++;
j++;
m--;
n--;
	if (count<=z)
	traversal(m,n,i,j,count,z,arr);
	}
Beispiel #4
0
int main(int argc, char * argv[]) {
    int * intPtr = NULL;
    volatile int number = 0;
    treenode_t * root = NULL;

    printf("intPtr is at address %p\n", &intPtr);
    malloc_wrapper((void *)&intPtr, 4);
    if (intPtr == NULL) {
        printf("could not malloc\n");
        return -1;
    }

    printf("intPtr is at address %p\n", &intPtr);
    printf("intPtr is pointing to %p\n", intPtr);

    free (intPtr);

    printf("is big endian result : %08x\n", isBigEndian());

    insert(&root, 1);
    printf("root is %p\n", root);
    traversal(root);
    insert(&root, 2);
    traversal(root);
    insert(&root, 3);
    traversal(root);
    insert(&root, 1);
    traversal(root);
    insert(&root, 2);
    traversal(root);
    insert(&root, 3);
    traversal(root);
    insert(&root, -1);
    traversal(root);
    insert(&root, 0);
    traversal(root);

    printf("\n");
    deleteTree(&root);

    return 0;
}
int is_identical(struct node_dll *head, struct node *root){
	if(root==NULL)
		return -1;
	if (head == NULL)
		return -1;
	struct node_dll *temp1=NULL;
	int j=0;	
	a = (int *)malloc(sizeof(int)*20);
	traversal(root);
	temp1 = head;
	while (temp1 != NULL)
	{
		if (temp1->data != a[j])
			return 0;
		j++;
		temp1 = temp1->next;
	}
	return 1;
}
void main()
{

	int i=0,j=0,count=1,m,n,arr[20][20],x,y,z;

	printf("enter the no. of rows and columns:\n");
	scanf("%d%d",&n,&m);

	printf("Enter the elements:\n");
	for(x=0;x<n;x++)
	{	
		for(y=0;y<m;y++)
		{	
			scanf("%d",&arr[x][y]);
		}
	}
	z=m*n;
	traversal(m,n,i,j,count, z,arr);
	}
Beispiel #7
0
int setClass::initFileIfo(const char *tempfile){
    int state=0;//
    int pos=0;
    total++;
    if(total>8){
       QMessageBox::warning (NULL,QString("Warnings"),QString("You have loaded over 8 sets.It would be cleared all?"));
        //
       state=OVERLOADFILE;
       total--;
       return state;
    }
    Head[total-1]=new fileIfo;
    if(Head[total-1]==NULL){
        QMessageBox::critical(NULL,QString("Error"),QString("Can not allocate memory!"));
        printf("Can not allocate memory!");
        state=ALLOCATEMEMORYFAILED;
        return state;
    }
    Head[total-1]->id=total;
    Head[total-1]->absaddr=tempfile;
    Head[total-1]->name=tempfile;
    pos=Head[total-1]->name.find_last_of('\\');
    if(pos>=0){
        Head[total-1]->name.erase(0,pos+1);
    }

    pos=Head[total-1]->name.find_last_of('/');
    if(pos>=0){
        Head[total-1]->name.erase(0,pos+1);
    }

    pos=Head[total-1]->name.find_last_of('.');
    if(pos>=0){
      Head[total-1]->name.erase(pos);
    }

    traversal();
    return state;
}
Beispiel #8
0
void main()
{   int n;
    struct lst *head=0,*p=0;
    while(1)
    {
        struct lst *temp;
        temp=(struct lst*)malloc(sizeof(struct lst));
        scanf("%d lol  ",&n); //if this is there another data after zero is needed
//        scanf("%d",&n); if this is there then only one zero is needed
        if(n==0)
            break;
        temp->data=n;
        if(head==0)
            {p=head=temp;
            temp->next=0;}
        else
            p->next=temp;
            temp->next=0;
            p=temp;

    }
    traversal(head);
}
Beispiel #9
0
    // k-best interface!
    bool operator()(int k, yield_type& yield, weight_type& weight)
    {
      // k is simply a dummy...
      
      yield_set_type yields;
      
      hypergraph_type::node_set_type::const_iterator niter_end = graph.nodes.end();
      for (hypergraph_type::node_set_type::const_iterator niter = graph.nodes.begin(); niter != niter_end; ++ niter) {
	const hypergraph_type::node_type& node = *niter;
      
	hypergraph_type::node_type::edge_set_type::const_iterator eiter_end = node.edges.end();
	for (hypergraph_type::node_type::edge_set_type::const_iterator eiter = node.edges.begin(); eiter != eiter_end; ++ eiter) {
	  const hypergraph_type::edge_type& edge = graph.edges[*eiter];
	
	  weight_type score = function(edge);
	  yields.clear();
	
	  // *=
	  hypergraph_type::edge_type::node_set_type::const_iterator niter_end = edge.tails.end();
	  for (hypergraph_type::edge_type::node_set_type::const_iterator niter = edge.tails.begin(); niter != niter_end; ++ niter) {
	    score *= weights[*niter];
	    yields.push_back(&derivations[*niter]);
	  }
	  
	  // +=
	  if (score > weights[node.id]) {
	    weights[node.id] = score;
	    traversal(edge, derivations[node.id], yield_iterator(yields.begin()), yield_iterator(yields.end()));
	  }
	}
      }
      
      yield = derivations.back();
      weight = weights.back();

      return true;
    }
Beispiel #10
0
 vector<vector<int>> pathSum(TreeNode* root, int sum) {
     vector<vector<int> > result;
     
     if (root == NULL)
     {
         return result;
     }
     
     vector<vector<TreeNode*> > resultNodesVec;
     vector<vector<TreeNode*> > visitedNodesVec;
     vector<int> visitedSumVec;
     
     // init 
     resultNodesVec.clear();
     
     vector<TreeNode*> t;
     t.push_back(root);
     visitedNodesVec.push_back(t);
     
     visitedSumVec.push_back(root->val);
     
     // traversal
     traversal(resultNodesVec,visitedNodesVec,visitedSumVec,sum);
     
     for (int i = 0;i<resultNodesVec.size();i++)
     {
         vector<TreeNode*> v = resultNodesVec[i];
         vector<int> vj;
         for (int j = 0;j<v.size(); j++)
         {
             vj.push_back(v[j]->val);
         }
         result.push_back(vj);
     }
     
     return result;
 }
Beispiel #11
0
int CriticalPath(int n)
{
    nodePointer ptr;
    int i;
    for (i = 1; i < n; ++i)
        if (!earliest[i]){
            fprintf(stderr, "\n%s\n", "Network Unreacheable.");
            exit(EXIT_FAILURE);
        }
    printf("\netv:");
    for (i = 0; i < n; ++i) printf("%3d", earliest[i]);
    puts("");
    for (i = 0; i < n; ++i)
        latest[i] = earliest[n-1];
    while (i--)
        for (ptr = graph[order[i]].link; ptr; ptr = ptr->link)
            if (latest[order[i]] > latest[ptr->vertex] - ptr->duration)
                latest[order[i]] = latest[ptr->vertex] - ptr->duration;
    printf("ltv:");
    for (i = 0; i < n; ++i) printf("%3d", latest[i]);
    puts("");
    traversal(0, n - 1, 0);
    return earliest[n-1];
}
void recoverTree(TreeNode *root) 
{
	pre1 = NULL;
	post1 = NULL;
	temp = NULL;
	pre2 = NULL;
	post2 = NULL;
	traversal(root);
	if( pre2 == NULL )
	{
		int temp = pre1->val;
		pre1->val = post1->val;
		post1->val = temp;
	}
	else
	{
		int temp = pre1->val;
		pre1->val = post2->val;
		post2->val = temp;

	}
	return;
	
}
 vector<int> preorderTraversal(TreeNode* root) {
     vector<int> ans;
     traversal(root, ans);
     return ans;
 }
 void traversal(TreeNode* root, vector<int>& ans){
     if(root == NULL) return;
     ans.push_back(root->val);
     traversal(root->left, ans);
     traversal(root->right, ans);
 }
 void traversal(TreeNode* root){
     if (NULL == root) return;
     nodes.push_back(root->val);
     traversal(root->left);
     traversal(root->right);
 }
 vector<int> preorderTraversal(TreeNode* root) {
     nodes.clear();
     traversal(root);
     return nodes;
 }
//autocomplete function implemented in complete.c as required by A3 instructions
//recieves user input, runs search to see if in trie, and traversal to print autocompleted words
void autocomplete(TrieNode *trie, char *lookup) {
    TrieNode *stub=search(trie,lookup);
    traversal(stub,lookup);
}
Beispiel #18
0
    void traversal(vector<vector<TreeNode* > > &result,vector<vector<TreeNode*> > visitedNodesVec,vector<int> sumVec,int sum)
    {
        vector<vector<TreeNode*> >  visitedNodesVec_copy;
        vector<int> sumVec_copy;
        
        int  visited_all_node_counter = 0;
        int size = visitedNodesVec.size();
        
        for (int i =0;i<size;i++)
        {
            int visitedSum = sumVec[i];
            vector<TreeNode*> visitedPath = visitedNodesVec[i];
            TreeNode* last = *(visitedPath.end()-1);
            if (isLeaf(last))
            {
                visited_all_node_counter++;
                
                if (visitedSum == sum)
                {
                    result.push_back(visitedPath);   
                    continue;
                }
            }
            else if (hasSingleChilde(last))
            {
                if (last->left)
                {
                   visitedSum += last->left->val;
                   visitedPath.push_back(last->left); 
                   
                   
                   visitedNodesVec_copy.push_back(visitedPath);
                   sumVec_copy.push_back(visitedSum);
                }
                else
                if (last->right)
                {
                    visitedSum += last->right->val;
                    visitedPath.push_back(last->right); 
                    
                   visitedNodesVec_copy.push_back(visitedPath);
                   sumVec_copy.push_back(visitedSum);
                }

            }
            else
            {
                int visitedSum_left = visitedSum+last->left->val;
                int visitedSum_right = visitedSum+last->right->val;
                
                vector<TreeNode*> visitedPath_left = visitedPath;
                vector<TreeNode*> visitedPath_right = visitedPath;
                visitedPath_left.push_back(last->left);
                visitedPath_right.push_back(last->right);
                
                visitedNodesVec_copy.push_back(visitedPath_left);
                visitedNodesVec_copy.push_back(visitedPath_right);               
                
                sumVec_copy.push_back(visitedSum_left);
                sumVec_copy.push_back(visitedSum_right);
            }
        }
        
        if (visited_all_node_counter == size)
        {
            // stop the recursion
            return;
        }
        
        traversal(result,visitedNodesVec_copy,sumVec_copy,sum);
    }
Beispiel #19
0
void traversal (struct node *current){
  if (current == NULL)
    return;
  printf("%d \t",current->val);
  traversal(current->next);
}
 vector <int> inorderTraversal(TreeNode *root) {
     vector<int> output;
     traversal(root, output);
     
     return output;
 }
Beispiel #21
0
void dump() {
	traversal(g_hiertree->root);
	return;
}
Beispiel #22
0
rt_public EIF_REFERENCE edclone(EIF_CONTEXT EIF_REFERENCE source)
{
    /* Recursive Eiffel clone. This function recursively clones the source
     * object and returns a pointer to the top of the new tree.
     */
    RT_GET_CONTEXT
    EIF_GET_CONTEXT
    EIF_REFERENCE root = (EIF_REFERENCE)  0;		/* Root of the deep cloned object */
    jmp_buf exenv;					/* Environment saving */
    struct {
        union overhead discard;		/* Pseudo object header */
        EIF_REFERENCE boot;					/* Anchor point for cloning process */
    } anchor;
    struct rt_traversal_context traversal_context;
    int volatile is_locked;

#ifdef DEBUG
    int xobjs;
#endif

    if (source == NULL) {
        return NULL;			/* Void source */
    }

    /* The deep clone of the source will be attached in the 'boot' entry from
     * the anchor structure. It all happens as if we were in fact deep cloning
     * the anchor pseudo-object.
     */

    memset (&anchor, 0, sizeof(anchor));	/* Reset header */
    anchor.boot = (EIF_REFERENCE)  &root;	/* To boostrap cloning process */

    RT_GC_PROTECT(source);	/* Protect source: allocation will occur */

#ifdef DEBUG
    xobjs = nomark(source);
    printf("Source has %x %d objects\n", source, xobjs);
#endif

    /* Set up an exception trap. If any exception occurs, control will be
     * transferred back here by the run-time to give us a chance to clean-up
     * our structures.
     */

    {
        RTXDRH;							/* Save stack contexts */
        EIF_EO_STORE_LOCK;				/* Because we perform a traversal that marks
										   objects, we need to be sure we are the
										   only one doing it. */
        is_locked = 1;

        excatch(&exenv);		/* Record pseudo-execution vector */
        if (setjmp(exenv)) {
            RTXSCH;						/* Restore stack contexts */
            map_reset(1);				/* Reset in emergency situation */
            /* If we locked the EO_STORE_MUTEX, then we need to unmark objects
             * and unlock it. */
            if (is_locked) {
                /* We are only concerned abount unmarking objects, so we do not perform any
                 * accounting. */
                CHECK ("Not accounting", traversal_context.accounting == 0);
                CHECK ("Not unmarking", traversal_context.is_unmarking == 0);
                /* First we mark again all objects. */
                traversal_context.is_unmarking = 0;
                traversal(&traversal_context, source);
                /* Then we unmark them. */
                traversal_context.is_unmarking = 1;
                traversal(&traversal_context, source);
                /* Now we can unlock our mutex. */
                EIF_EO_STORE_UNLOCK;
            }
            ereturn(MTC_NOARG);					/* And propagate the exception */
        }

        /* Now start the traversal of the source, allocating all the objects as
         * needed and stuffing them into a FIFO stack for later perusal by the
         * cloning process.
         */

        memset (&traversal_context, 0, sizeof(struct rt_traversal_context));
        traversal_context.accounting = TR_MAP;
        traversal(&traversal_context, source);		/* Object traversal, mark with EO_STORE */
        hash_malloc(&hclone, traversal_context.obj_nb);	/* Hash table allocation */
        map_start();					/* Restart at bottom of FIFO stack */

#ifdef DEBUG
        printf("Computed %x %d objects\n\n", source, traversal_context.obj_nb);
#endif

        /* Throughout the deep cloning process, we need to maintain the notion of
         * enclosing object for GC aging tests. The enclosing object is defined as
         * being the object to which the currently cloned tree will be attached.
         *
         * We need to initialize the cloning process by computing a valid reference
         * into the root variable. That will be the enclosing object, and of course
         * it cannot be void, ever, or something really really weird is happening.
         *
         * To get rid of code duplication, I am initially calling rdeepclone with
         * an enclosing object address set to anchor.boot. The anchor structure
         * represents a pseudo anchor object for the object hierarchy being cloned.
         */

        rdeepclone(source, (EIF_REFERENCE) &anchor.boot, 0);	/* Recursive clone */
        hash_free(&hclone);						/* Free hash table */
        map_reset(0);							/* And eif_free maping table */
        /* Release all the hector pointers asked for during the map table
         * construction (obj_nb exactly) */
        CHECK("Has objects", traversal_context.obj_nb > 0);
        eif_ostack_npop(&hec_stack, traversal_context.obj_nb);

#ifdef DEBUG
        xobjs= nomark(source);
        printf("Source now has %d objects\n", xobjs);
        xobjs = nomark(anchor.boot);
        printf("Result has %d objects\n", xobjs);
#endif

        RT_GC_WEAN(source);			/* Release GC protection */
        expop(&eif_stack);			/* Remove pseudo execution vector */
    }

    EIF_EO_STORE_UNLOCK;		/* Free marking mutex */
    is_locked = 0;

    return anchor.boot;			/* The cloned object tree */
}
Beispiel #23
0
void balance(Tree* root, int value) {
    int flag = 0;
    getLevel(root, &flag);
    if(flag < 0) {
        Tree* broken = find(root, -flag);
        
        traversal(root);
      //  printf("broken: %d.\n", broken->value);
        
        if(value < broken->value) { // L
            if(value < broken->left->value) { // LL
                // printf("LL\n");
                Tree* three = broken;
                Tree* threeR = broken->right;
                Tree* two = broken->left;
                Tree* one = two->left;
                Tree* twoR = two->right;
                
                int temp = three->value;
                three->value = two->value;
                two->value = temp;
                
                three->left = one;
                three->right = two;
                two->left = twoR;
                two->right = threeR;
            }
            else { //if(value > broken->left->value  LR
                // printf("LR\n");
                Tree* A = broken;
                Tree* AR = A->right;
                Tree* B = A->left;
                Tree* C = B->right;
                Tree* CL = C->left;
                Tree* CR = C->right;
                
                int temp = A->value;
                A->value = C->value;
                C->value = temp;
                
                A->right = C;
                B->right = CL;
                C->left = CR;
                C->right = AR;
            }
        }
        else { //if(value > broken->value)  R
            if(value > broken->right->value) { // RR
                //printf("RR\n");
                Tree* A = broken;
                Tree* AL = A->left;
                Tree* B = A->right;
                Tree* BL = B->left;
                Tree* C = B->right;
                
                int temp = A->value;
                A->value = B->value;
                B->value = temp;
                
                A->left = B;
                A->right = C;
                B->left = AL;
                B->right = BL;
            }
            else { //if(value < broken->right->value) RL
                //printf("RL\n");
                Tree* A = broken;
                Tree* AL = A->left;
                Tree* B = A->right;
                Tree* C = B->left;
                Tree* CL = C->left;
                Tree* CR = C->right;
                
                int temp = A->value;
                A->value = C->value;
                C->value = temp;
                
                A->left = C;
                B->left = CR;
                C->left = AL;
                C->right = CL;
            }
        }
        traversal(root);
        //printf("\n");
    }
}
Beispiel #24
0
list_t traversal(tree_t tree) {
    if (tree_isEmpty(tree)) return list_make();
    return append(traversal(tree_left(tree)), 
        list_make(tree_elt(tree), traversal(tree_right(tree))));
}
 void traversal(vector<int>& ret, TreeNode* x)
 {
     ret.push_back(x->val);
     if (x->left) traversal(ret,x->left);
     if (x->right) traversal(ret,x->right);
 }
 void traversal(TreeNode *root){
     if( root == NULL ) return;
     traversal(root -> left);
     inorder.push_back(root -> val);
     traversal(root -> right);
 }
	vector<int> preorderTraversal(TreeNode *root) {
		vector<int> result;
		traversal(root,result);
		return result;
	}
 TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
     inorder.clear();
     traversal(root);
     inorder.push_back(val);
     return createTree(0, inorder.size());
 }
 vector<int> postorderTraversal(TreeNode* root) 
 {
     vector<int> res;
     traversal(root,res);
     return res;
 }
Beispiel #30
0
void SquareMaze::solveMaze()
{
	vector<int> path;
	traversal(0, 0, 0 , 1 , path);
}