void level_order(struct node *temp, int *arr, int *index, int i) { if (temp == NULL) return; if (i == 0) { arr[(*index)++] = temp->data; } level_order(temp->right, arr, index, i - 1); level_order(temp->left, arr, index, i - 1); }
void main() { int opt,item ; tree_ptr temp,para; char search; do { clrscr(); printf("\npress 1 for maketree"); printf("\nPress 2 for inorder "); printf("\npress 3 for preorder"); printf("\npress 4 for postorder"); printf("\npress 5 for level order traversal:"); printf("\npress 6 for copying binary tree"); printf("\npress 7 for finding parent of given node"); printf("\npress 8 for finding deapth of tree"); printf("\npress 9 for find ancestr of given element"); printf("\npress 10 for find All The leaf node of the tree"); printf("\npress 99 for exit"); printf("\nenter the option"); scanf("%d",&opt); switch(opt) { case 1:printf("\nenter the item to be inserted:"); flushall(); scanf("%c",&item); root=insert(item,root) ; break; case 2:printf("\nThe inorder expersion is ..."); inorder(root); break; case 3:printf("\nThe preorder expersion is..."); preorder(root); break; case 4:printf("\nthe postorder expersion is .."); postorder(root); break; case 5:printf("\nThe level order traversal is .."); level_order(root); break; case 6:printf("\nThe copied binary tree is inorder expersion is ..\n"); temp=copy(root); inorder(temp); break; case 7: parent(root); break; case 8:depth(dir,ctr); break; case 9:grand(root); break; case 10:printf("\n"); count(root); printf("\nThe no of leaf node are %d",c); c=0; break; } getch(); }while(opt!=99); }
/* * 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 ); }
void traverse() { iter_inorder(root); printf("\n\n"); iter_preorder(root); printf("\n\n"); iter_postorder(root); printf("\n\n"); level_order(root); }
int main(int argc, char **argv) { ListNode dummy(0); ListNode *cur = &dummy; for (int i = 1; i < argc; i++) { cur->next = new ListNode(atoi(argv[i])); cur = cur->next; } Solution s; level_order(s.sortedListToBST(dummy.next)); return 0; }
int* BSTRighttoLeftRows(struct node* root) { if (root == NULL) return NULL; int c = 0; CountNodes(root, &c); int *arr = (int *)malloc(sizeof(int)*c); c = 0; int height = get_treeHeight(root); for (int i = 0; i < height; i++) { level_order(root, arr, &c, i); } return arr; }
int main(void) { Tree tree; printf("Create Tree (non-integer to stop):\n"); create_tree(&tree); printf("\nPre Order:\n"); pre_order(tree); printf("\nMid Order:\n"); mid_order(tree); printf("\nPost Order:\n"); post_order(tree); printf("\nLever Order:\n"); level_order(tree); empty_tree(&tree); return 0; }