void pre_order(BiTree &root) { if (root != NULL) { printf("%c ", root->date); pre_order(root->lchild); pre_order(root->rchild); } }
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)); }
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); }
//PRE ORDER-------------------------------------------------------------------------- void pre_order(node * record){ if(record != NULL){ printf("data = %d\n",record->val); pre_order(record->left); pre_order(record->right); } }
void pre_order(AVL a){ if (a == NULL) return; printf("%d -> ", a->valor); pre_order(a->esq); pre_order(a->dir); }
void pre_order(link t, void (*visit)(link)) { if (!t) return; visit(t); pre_order(t->l, visit); pre_order(t->r, visit); }
//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); } }
void pre_order(struct treeNode* treeData) { if(treeData != NULL) { printf("%c", treeData->data); pre_order(treeData->left); pre_order(treeData->right); } }
void pre_order(node *root) { if(root) { printf("%d",root->data); pre_order(root->left); pre_order(root->right); } }
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); } }
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); } }
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); }
void pre_order(TreeNode * temp) { if(temp != NULL) { num++; printf(" %d",temp->data); pre_order(temp->leftchild); pre_order(temp->rightchild); // free(temp); } }
/** * 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; }
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; }
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"); }
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)); } }
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); } } }
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); }); }
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; }
/* * 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 ); }
/* * 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 ); } }
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; }
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(); }