Esempio n. 1
0
void pre_order(BiTree &root) {
	if (root != NULL) {
		printf("%c ", root->date);
		pre_order(root->lchild);
		pre_order(root->rchild);
	}
}
Esempio n. 2
0
void pre_order(struct heap *add_pre, int i) {
    printf("%d, ", add_pre->ptr_node[i].data);
    if (L_CHILD(i) < add_pre->size)
        pre_order(add_pre, L_CHILD(i));
    if (R_CHILD(i) < add_pre->size)
        pre_order(add_pre, R_CHILD(i));
}
Esempio n. 3
0
void pre_order(struct node *add_pre) {
    if (add_pre == NULL)
        return;
    printf("%d, ", add_pre->data);
    pre_order(add_pre->lc);
    pre_order(add_pre->rc);
}
Esempio n. 4
0
File: tree.c Progetto: ankitrwt/ds
//PRE ORDER--------------------------------------------------------------------------
void pre_order(node * record){
	if(record != NULL){
		printf("data = %d\n",record->val);
		pre_order(record->left);
		pre_order(record->right);
	}
}
Esempio n. 5
0
void pre_order(AVL a){
    
    if (a == NULL) return;
    
    printf("%d -> ", a->valor);
    pre_order(a->esq);
    pre_order(a->dir);
}
Esempio n. 6
0
void pre_order(link t, void (*visit)(link))
{
  if (!t)
    return;
  visit(t);
  pre_order(t->l, visit);
  pre_order(t->r, visit);
}
Esempio n. 7
0
//Tranversal algorithms
void pre_order(node_t * temp)
{
    if(temp!=NULL) {
        printf("Element: %d\n",temp->value);
        pre_order(temp->left);
        pre_order(temp->right);
    }
}
Esempio n. 8
0
void pre_order(struct treeNode* treeData)
{
    if(treeData != NULL)
    {        
        printf("%c", treeData->data);
        pre_order(treeData->left);
        pre_order(treeData->right);
    }    
}
Esempio n. 9
0
void pre_order(node *root)
{
	if(root)
	{	
		printf("%d",root->data);
		pre_order(root->left);
		pre_order(root->right);
	}
}
Esempio n. 10
0
void pre_order(struct node* _node)
{
	if (_node == NULL)
		return;

	printf("%c ", _node->c);
	pre_order(_node->lchild);
	pre_order(_node->rchild);
}
void pre_order(struct node *temp, int *result, int *i){
	if (temp == NULL){
		return;
	}
	result[*i] = temp->data;
	(*i)++;
	pre_order(temp->left, result, i);
	pre_order(temp->right, result, i);
}
 void pre_order(const node_ptr& n, OutIt& out)
 {
     if (n)
     {
         *(out++) = n->data;
         pre_order(n->left, out);
         pre_order(n->right, out);
     }
 }
Esempio n. 13
0
File: btree.c Progetto: sktwj/var
static inline void pre_order(struct bnode *bnode, 
		void (*todo)(struct bnode *))
{
	if (bnode) {
		todo(bnode);
		pre_order(bnode->lchild, todo);
		pre_order(bnode->rchild, todo);
	}	
}
Esempio n. 14
0
	void pre_order(node n, node N[]){		
		if (n.value == -1)
		{
			return;
		}
		answer[++tot]=n.value;
		//printf("%d ", n.value);
		pre_order(N[n.left],N);
		pre_order(N[n.right],N);
	}
void pre_order(TreeNode *pRoot)
{
	if(pRoot == NULL)
	{
		return ;
	}
	printf("%d ",pRoot->iVal);
	pre_order(pRoot->pLeft);
	pre_order(pRoot->pRight);
}
Esempio n. 16
0
void pre_order(TreeNode * temp)
{
	if(temp != NULL)
	{
		num++;
		printf(" %d",temp->data);
		pre_order(temp->leftchild);
		pre_order(temp->rightchild);
	//	free(temp);
	}
}
Esempio n. 17
0
/**
 * Solution by pre-order on the binary tree
 *  Time complexity: O(n)
 * Space complexity: O(depth), for the deepest call stack
 */
int pre_order(struct TreeNode* root, int n) {
    if (!root) {
        return 0;
    }

    n = (n * 10) + root->val;
    if (!root->left && !root->right) {
        return n;
    }
    
    int sum = pre_order(root->left, n);
    sum += pre_order(root->right, n);
    return sum;
}
Esempio n. 18
0
int main(int argc, char **argv)
{
	/* 根据前序和中序遍历结果构造二叉树 */
	unsigned char pre_seq[] = { 4, 2, 1, 3, 6, 5, 7 };
	unsigned char in_seq[] = { 1, 2, 3, 4, 5, 6, 7 };
	/*
	 *		    4
	 *		   / \
	 *       2	   6
	 *      / \   / \
	 *     1   3  5  7
	 */

	printf("Create a tree with pre and in sequence\n");
	BiTree bTree = init(pre_seq, in_seq, 7);

	printf("Fork a tree\n");
	BiTree forkTree;
	forkTree = fork_tree(bTree);
	printf("pre: ");
	pre_order(forkTree, print_item);
	putchar('\n');
	printf("Tree count = %d depth = %d\n", tree_leavess(forkTree), tree_depth(forkTree));

	tree_destroy(bTree);
	tree_destroy(forkTree);

	return 0;
}
Esempio n. 19
0
int main() {
  int node_count, i;
  scanf("%d\n", &node_count);

  node nodes[node_count];

  for (i = 0; i < node_count; i++) {
    int value, left, right;
    node_init(&nodes[i]);

    scanf("%d %d %d", &value, &left, &right);
    nodes[i].value = value;

    if (left != -1) {
      nodes[i].left = &nodes[left];
      nodes[left].parent = &nodes[i];
    }

    if (right != -1) {
      nodes[i].right = &nodes[right];
      nodes[right].parent = &nodes[i];
    }
  }

  inorder(&nodes[0], print_int);
  printf("\n");
  pre_order(&nodes[0], print_int);
  printf("\n");
  post_order(&nodes[0], print_int);
  printf("\n");
}
Esempio n. 20
0
void recoverTree_n(ptn root) {
    int i = 0, c = 0;
    pal l = al_init(16);
    int w1 = 0, w2 = 0;
    int size = 0;
    ptn * arr = NULL;
    pre_order(l, root);
    size = l->size;
    arr = al_convert_to_array_free_l(l);
    for (i = 1; i < size; i ++) {
        if (arr[i]->val < arr[i-1]->val) {
            if (c == 0) {
                w1 = i;
                c ++;
            } else if (c == 1) {
                w2 = i;
                c ++;
            }
        }
    }
    if (c == 2) {
        swap(&(arr[w1-1]->val), &(arr[w2]->val));
    }
    if (c == 1) {
        swap(&(arr[w1]->val), &(arr[w1-1]->val));
    }
}
Esempio n. 21
0
int main()
{
	int data, ch;
	while (1) 
	{
		printf("\n1. Insertion in Binary Search Tree");
		printf("\n2. Preorder traversal ");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1:
				while (1) 
				{
				printf("Enter your data:");
				scanf("%d", &data);
				insertion(&root, data);
				printf("Continue Insertion(0/1):");
				scanf("%d", &ch);
				if (!ch)
				break;
				}
				break;
			case 2: pre_order(root);
				break;
			default: exit(0);
		}
	}
}
Esempio n. 22
0
int main()
{
    char expr[MAX_SIZE];
    struct treeNode* root;
        
    FILE *infile;
    infile = fopen("binaryEFDS.in", "r");
    if(infile == NULL)
    printf("File not found.");
    else
    fscanf(infile, "%s", expr);
        
    root = initPostOrder(expr);
            
    printf("Tree data in In-order: ");
    in_order(root);
    printf("\nTree data in Pre-order: ");
    pre_order(root);
    printf("\nTree data in Post-order: %s", expr);
    printf("\nResult: %d\n", getResult(root));    
    
    free(root->left);
    free(root->right);
    free(root);
}    
 // traversal - output via generator
 crs::generator<T> pre_order()
 {
     return crs::generator<T>(
         [this](crs::generator_output<T> out) {
             pre_order(out);
     });
 }
Esempio n. 24
0
 vector<string> binaryTreePaths(TreeNode* root) {
     vector<string> ans;
     if(root == NULL)
         return ans;
     vector<int> cur;
     pre_order(root, ans, cur);
     return ans;
 }
int main()
{
	TreeNode *pRoot = create_tree();
	pre_order(pRoot);
	printf("\n");
	print_ttob(pRoot);
	return 0;
}
Esempio n. 26
0
/*
 * Function: int main(int argc, char args[])
 * Description: process main function
 * Input:  argc: parameter number
 *         args: parameter value array
 * Output: none
 * Return: function exit status
 * Others: none
 */
int main( )
{
	int		depth	   = 0;
	Bt		* bt	   = create_bt( );
	char	a[10][10]  = { "2", "8", "1", "3", "9", "4", "7", "5", "6", "0" };
	char	*b[10];
	int		i;
	Bt_Entry* entry;

	for( i = 0; i < 10; i++ )
	{
		b[i] = a[i];
	}
	init_bt( bt, b, 10, strcmp );
	printf("pre order for this bt\n");
	pre_order(bt->root,show_string);
	printf("in order for this bt\n");
	in_order( bt->root, show_string );
	printf("post order for this bt\n");
	post_order(bt->root,show_string);
	printf("level order for this bt\n");
	level_order(bt->root,show_string);
	special_level_order(bt->root,2,show_string);

	depth = calc_tree_depth( bt->root );
	printf( "depth is %d\n", depth );

	entry = get_entry( bt->root, "3", strcmp );
	if( entry )
	{
		printf( "entry item is %s\n", entry->item );
	} else
	{
		printf( "entry is NULL\n" );
	}

	set_entry( bt->root, "3", "33", strcmp );
	in_order( bt->root, show_string );

	entry = get_parent( bt->root, "33", strcmp );
	printf( "parent item is %s\n", entry->item );

	entry = get_right( bt->root, "5", strcmp );

	if( entry )
	{
		printf( "right item is %s\n", entry->item );
	}

	entry = get_left( bt->root, "8", strcmp );

	if( entry )
	{
		printf( "left item is %s\n", entry->item );
	}

	destroy_bt( bt, NULL );
}
Esempio n. 27
0
/*
 * Function: void pre_order( Bt_Entry* root, show_item show )
 * Description: previous order
 * Input:  root: the binary tree root
 *         show:  item showing function
 * Output: none
 * Return: void
 * Others: none
 */
void pre_order( Bt_Entry* root, show_item show )
{
	if( !root )
	{
		printf( "none for root\n" );
		return;
	}

	( *show )( root->item );

	if( root->left )
	{
		pre_order( root->left, show );
	}
	if( root->right )
	{
		pre_order( root->right, show );
	}
}
Esempio n. 28
0
void main()
{
	char * pre_str = "ABDECFG";
	char * mid_str = "DBEAFCG";

	struct node * root = restore_pre_mid(pre_str, mid_str, 7);

	pre_order(root);
	printf("\n");
}
int main(){
    vector<string> v = {"3","9","20","#","#","15","7"};
    TreeNode* root = create_tree(v);
    Solution s;
    string tree_str = s.serialize(root);
    cout<<tree_str<<endl;
    TreeNode* new_root = s.deserialize(tree_str);
    pre_order(new_root);
    return 0;
}
Esempio n. 30
0
 void pre_order(TreeNode *root, vector<string> &ans, vector<int> &cur)
 {
     cur.push_back(root->val);
     if(root->left)
         pre_order(root->left, ans, cur);
     if(root->right)
         pre_order(root->right, ans, cur);
     if(!root->left && !root->right && cur.size() != 0)
     {
         stringstream ss;
         for(int i = 0; i < cur.size(); i++)
         {
             if(i != 0)
                 ss<<"->";
             ss<<cur[i];
         }
         ans.push_back(ss.str());
     }
     cur.pop_back();
 }