int main() { int n; scanf("%d", &n); int tree[n + 1][2], idx = 0; while (++idx <= n) { scanf("%d %d", &tree[idx][0], &tree[idx][1]); } preorder(tree, 1); printf("\n"); inorder(tree, 1); printf("\n"); postorder(tree, 1); return 0; }
void bintree<T>::display() { if(root==NULL) cout<<"Tree Is Not Created"; else { cout<<"\n The Inorder Traversal of Tree is : "; inorder(root); cout<<"\n The Preoder Traversal of Tree is : "; preorder(root); cout<<"\n The Preoder Traversal of Tree is : "; postorder(root); } }
void main() { node root = NULL; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); root = insert(root, 40); root = insert(root, 50); root = insert(root, 25); printf("\nThe preorder traversal is: "); preorder(root); printf("\n"); }
int main(int argc, char *argv[]) { struct Node *root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->left = new Node(6); root->right->right = new Node(7); std::cout << "Preorder Traversal(iterative): "; preorder(root); std::cout << "\n"; return 0; }
int main(void) { Tree *root = (Tree *)malloc(sizeof(Tree)); root->left = root->right = root->parent = NULL; root->val = INT_MIN; int i = 0; int minlen = INT_MAX; int templen = 0; for (i = 1; i <= 1024; i++) { insert(root, i); } preorder(root->left); printf("\n"); for (i = 1; i <= 1024; i++) { find(root, i); } preorder(root->left); printf("\n"); return 0; }
void main() { int n,digit,g,o; node *insert(node *,int ); void preorder(node *P); void inorder(node *P); void postorder(node *P); void deltree(void); do { printf("\tWhat do you want to do? \n"); printf("\t\t 1) insert the element:\n"); printf("\t\t 2) preorder traversal:\n"); printf("\t\t 3) inorder traversal:\n"); printf("\t\t 4) postorder traversal:\n"); printf("\t\t 5) delete \n"); scanf("%d",&n); switch(n) { case 1: printf("\tEnter the digit:\n"); scanf("%d",&digit); while(digit!=0) { P=insert(P,digit); scanf("%d",&digit); } break; case 2: printf("\tThe preorder traversal is as follows:\n"); preorder(P); getch(); break; case 3: printf("\t The inorder traversal is as follows:\n"); inorder(P); getch(); break; case 4: printf("\t The postorder traversal is as follows:\n"); postorder(P); getch(); break; case 5: printf("\t delete\n"); deltree(); break; } printf("\t DO you want to continue:\n"); printf("\t 1)YES\n"); printf("\t 2)NO\n"); scanf("%d",&g); clrscr(); }while(g==1); }
int main() { int pre[] = {5,3,6,2,7,1,4,8,9}; int in[] = {1,2,3,4,5,6,7,8,9}; struct BT *root = makeTree(in, pre, 0, sizeof(pre)/sizeof(pre[0])); printf("\t====inordeer====\n"); inorder(root); printf("\t====preordeer====\n"); preorder(root); return 0; }
int main() { struct node * root = newnode(1); root->left=newnode(2); root->right=newnode(3); root->left->left=newnode(4); root->left->right=newnode(5); printf("preorder\n"); preorder(root); printf("INorder\n"); inorder(root); printf("postorder\n"); postorder(root); return 0; }
pair<TreeNode*, TreeNode*> preorder(TreeNode *root) { if(root == NULL) return make_pair(root, root); pair<TreeNode*, TreeNode*> left = preorder(root->left); pair<TreeNode*, TreeNode*> right = preorder(root->right); root->left = NULL; TreeNode *tail = root; if(left.first) { tail->right = left.first; tail = left.second; } if(right.first) { tail->right = right.first; tail = right.second; } return make_pair(root, tail); }
int main() { int row = 3;//no. of input lines //1st element in each row is node Value, 2nd - no of child, ,>=3rd....=>value of child int data[3][5]={{1,3,2,3,4},{2,2,5,6,0},{3,3,8,9,10}}; int i; struct tree* root=NULL; for(i=0;i<row;i++) { // preorder(root); root = createnary(root,data[i]); } preorder(root); }
int main(int argc, char *argv[]){ element_type array[20]; element_type array1[20]; int i; BTREE T1 = init_tree(); BTREE T2 = init_tree(); BTREE T3 = NULL; for(i=0; i<20; i++){ array[i] = random(200); array1[i] = i; printf("%d ", array[i]); } printf("\n"); T1 = creat_tree(array, sizeof(array)/sizeof(array[0]), T1); T2 = creat_tree(array, 1, T2); preorder(T1); printf("\n"); preorder(T2); printf("\n"); if(contain_tree(T1, T2)) printf("T1 contains T2\n"); else printf("T1 doesn't contain T2\n"); free_tree(T1); free_tree(T2); return 0; }
main() { int choice,num; root=NULL; while(1) { printf("\n"); printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Inorder Traversal\n"); printf("4.Preorder Traversal\n"); printf("5.Postorder Traversal\n"); printf("6.Display\n"); printf("7.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the number to be inserted : "); scanf("%d",&num); insert(num); break; case 2: printf("Enter the number to be deleted : "); scanf("%d",&num); del(num); break; case 3: inorder(root); break; case 4: preorder(root); break; case 5: postorder(root); break; case 6: display(root,1); break; case 7: exit(); default: printf("Wrong choice\n"); }/*End of switch */ }/*End of while */ }/*End of main()*/
int main() { int n,x; struct node *location,*parent; do { printf("1.insertion\n2.deletion\n3.search\n4.inorder traversal\n5.preorder traversal\n6.postorder\n7.exit\n"); printf("\nenter ur choice\n"); scanf("%d",&n); switch(n) { case 1: printf("\nenter element to be inserted\n"); scanf("%d",&x); insert(x); break; case 2: printf("\nenter element to be deleted\n"); scanf("%d",&x); del(x); break; case 3: printf("\nenter element to be searched\n"); scanf("%d",&x); find(x,&parent,&location); if(location!=NULL) printf("\nsearch successfull\n"); else printf("\nitem not found\n"); break; case 4: inorder(root); break; case 5: preorder(root); break; case 6: postorder(root); break; case 7: return 0; break; default : printf("\ndefault entry\n"); } } while(n!=7); }
int main(void) { struct node* root = NULL; //struct node* root2 = NULL; struct node *find = NULL; char str[1024]; root = insert(root, 10); root = insert(root, 15); root = insert(root, 9); root = insert(root, 8); root = insert(root, 13); root = insert(root, 7); root = insert(root, 5); root = insert(root, 18); root = insert(root, 22); root = insert(root, 3); root = insert(root, 4); root = insert(root, 2); root = insert(root, 1); print_ascii_tree(root); find = search(root, 18); print_ascii_tree(root); find = search(root, 22); printf("\n\n\nDATA found is %d\n", find->data); find = min_node(root); printf("Min in this tree is %d\n", find->data); find = max_node(root); printf("Mx in this tree is %d\n", find->data); print_ascii_tree(root); preorder(root); printf("\n"); inorder(root); printf("\n"); postorder(root); printf("\n"); printf("DEPTH is %d\n", depth(root)); tree_to_string(root, str); printf("The STR generated is %s\n", str); //string_to_tree(&root2, str); //print_ascii_tree(root2); printf("COUNT is %d\n",nodes(root)); bool res = hassum(root, 45); printf("Bool val is %d\n", (int)res); levelorder(root); return 0; }
int main() { int n; scanf("%d",&n); int i; struct node* root=NULL; for(i=0;i<n;i++) { int a; scanf("%d",&a); insert(&root,a,NULL); } inorder(root); printf("\n"); preorder(root); printf("\n"); postorder(root); printf("\n"); int search; scanf("%d",&search); int start=0; int end=k-1; while(start<=end) { if(arr[start]+arr[end]==search) { printf("%d %d",arr[start],arr[end]); break; } else if(arr[start]+arr[end]>search) { end--; } else { start++; } } /* searchi(root,search); //printf("%d",root->right->left->data); inorder(root); printf("\n"); preorder(root); printf("\n"); postorder(root); printf("\n");*/ return 0; }
int main(int argc, char *argv[]) { int array[] = {2, 3, 4, 5, 6, 8, 10, 12, 14, 15, 16, 18, 20}; int lb = 0; int ub = sizeof(array)/sizeof(int) - 1; tnode *root = tnode_construct_bst(array, lb, ub); preorder(root); //Question printf("\n"); postorder(root); // Question printf("\n"); tnode_destroy_bst(root); // Question return EXIT_SUCCESS; }
show(NodeT *root,int level) { while ('\n' != getc(stdin)); printf( "\nPreorder listing\n" ); preorder( root, 0 ); printf( "Press Enter to continue." ); while ('\n' != getc(stdin)); printf( "\nInorder listing\n" ); inorder( root, 0 ); printf( "Press Enter to continue." ); while ('\n' != getc(stdin)); printf( "\nPostorder listing\n" ); postorder( root, 0 ); printf( "Press Enter to continue." ); while ('\n' != getc(stdin)); }
int main() { nptr root = NULL; Insert(&root, 10); Insert(&root, 11); Insert(&root, 7); Delete(root, 11); Insert(&root, 15); Insert(&root, 12); preorder(root); printf("\n\n"); //inorder(root); printf("\n\n"); //postorder(root); printf("\n\n"); return 0; }
void main(void) { int ch; char y; int data; struct node *temp; while(1) { printf("\n\n\n1: Insert\t2: Inorder Traversal\t3: Preorder Traversal\t4: Postorder Traversal\t5: Exit\nEnter you choice: "); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter data value: "); scanf("%d",&data); temp=new_node(data); if(root==NULL) { root=temp; } else insert(root,temp); break; case 2: inorder(root); break; case 3: preorder(root); break; case 4: postorder(root); break; case 5: exit(0); default: printf("\nInvalid Choice !"); } } }
int main() { Node* root; int arr[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; maketree(arr, 0, sizeof(arr)/sizeof(int)-1, &root); inorder(root); printf("height is %d\n", height(root)); preorder(root); printf("Balance: %d\n", isBalanced(root)); return 0; }
void main() { node *root; insert(&root, 9); insert(&root, 4); insert(&root, 15); insert(&root, 6); insert(&root, 12); insert(&root, 17); insert(&root, 2); printf("PREORDER\n"); preorder(root); printf("INORDER\n"); inorder(root); printf("POSTORDER\n"); postorder(root); }
int main() { int n; node *root=0,*newnode; printf("enter info and enter -999 for end\n"); scanf("%d",&n); while(n!=-999) { if(n==-999) break; newnode=getnode(n); root=insert(root,newnode); scanf("%d",&n); } preorder(root); getch(); }
void main ( ) { clrscr ( ); insert (&root, 20); insert (&root, 60); insert (&root, 40); insert (&root, 10); insert (&root, 30); insert (&root, 50); insert (&root, 25); insert (&root, 28); insert (&root, 29); clrscr ( ) ; printf ("\n Traversing BST into preorder :") ; preorder (root) ; getch ( ) ; }
int main() { //FILE*f=fopen("data.txt","r"); //FILE*g=fopen("out.txt","w"); Node*rad=constructie(); printf("\npreordine\n"); preorder(rad,0); printf("\ninordine\n"); inorder(rad,0); printf("\npostorder\n"); postorder(rad,0); //fclose(f); //fclose(g); return 0; }
int main(void) { // create the root node for the tree and a starting value. BST tree = NULL; int r; for (int i = 0; i < 40; i++) { r = rand() % 100 + 1; tree = add_element_to_BST(tree, r); } preorder(tree); destroy_tree(&tree); printf("....................destroy...................\n"); inorder(tree); return EXIT_SUCCESS; }
int main() { Solution s; int sum = 22; vector<int> preorder({5, 4, 11, 7, 2, 8, 13, 4, 5, 1}), inorder({7, 11, 2, 4, 5, 13, 8, 5, 4, 1}); TreeNode *root = buildTree(preorder, inorder); root->print(); cout << s.pathSum(root, sum) << endl; preorder = {5, 4, 11, 7, 2, 8, 13, 4, 1}, inorder = {7, 11, 2, 4, 5, 13, 8, 4, 1}, sum = 22; root = buildTree(preorder, inorder); root->print(); cout << s.pathSum(root, sum) << endl; preorder = {1}, inorder = {1}, sum = 1; root = buildTree(preorder, inorder); root->print(); cout << s.pathSum(root, sum) << endl; return 0; }
void flatten(TreeNode* root) { if(root == NULL) return; deque<TreeNode*> q; preorder(q,root); q.pop_front(); TreeNode* curr = root; while(!q.empty()) { curr -> right = q.front(); q.pop_front(); curr = curr -> right; } }
/************************************************** * This function opens the file and reads each * * line into the buildArray function. It then * * calls the rest of the functions to sort the * * array and build the balanced binary tree. * * EXIT 2 for file input error * * EXIT 1 for memory error * **************************************************/ void initialize(char *filename) { FILE *fp; char buffer[21], **names; int count = 0, *count_ptr = &count, size = 2, *size_ptr = &size; BNODE* root; if ((fp = fopen(filename, "r")) == NULL) { printf("Error: Filename does not exist\n"); exit(2); } if ((names = malloc(2 * sizeof(char*))) == NULL) { printf("Error: Unable to allocate memory\n"); exit(1); } while (fgets(buffer, sizeof(buffer), fp) != NULL) { /* Double the size of the array after it is full */ if (count == size) names = resizeArray(names, size_ptr); names = buildArray(names, buffer, count_ptr); } fclose(fp); names = sortArray(names, count_ptr); root = buildTree(names, 0, count-1); printf("\n preorder: "); preorder(root); printf("\n\n"); printf(" inorder: "); inorder(root); printf("\n\n"); printf("postorder: "); postorder(root); printf("\n"); }
void main(){ int no,ch; printf("Enter root\n"); scanf("%d",&no); root=make(no); p=root; while(1){ printf("\nEnter another no\n"); printf("\nEnter 0 to terminate\n"); scanf("%d",&no); if(no==0) break; p=root; q=root; while(no!=p->data && q!=NULL){ p=q; if(no< p->data) q=p->left; else q=p->right;} if(no < p->data){ printf("Left branch of %d is %d",p->data,no); left(p,no);} else{ right(p,no); printf("Right branch of %d is %d",p->data,no); }} while(1) { printf("\n1:Inorder\n2:Preorder\n3:Postorder\n"); printf("Enter choice\n"); scanf("%d",&ch); switch(ch) { case 1: inorder(root); break; case 2: preorder(root); break; case 3: postorder(root); break; case 4: exit(0); default : printf("Invalid choice\n"); break; }}}
int main(){ freopen("in.txt", "r", stdin); node * root = NULL; int n, i=0, item, sum=0, * psum; //STDIN - n, tree elements scanf("%d", &n); for(;i++<n; scanf("%d", &item), insert(&root, item)); //STDOUT - tree traversal preorder(root); printf("\n"); inorder(root); printf("\n\n"); psum = ∑ //STDOUT - modified tree traversal dpreorder(root, psum); printf("\n"); dinorder(root, psum); printf("\n"); return 0; }