int main() { node *tmp; root = NULL; /* Inserting nodes into tree */ insert(&root, 9); insert(&root, 4); insert(&root, 15); insert(root, 6); insert(&root, 12); insert(&root, 17); insert(&root, 2); /* Printing nodes of tree */ printf("Pre Order Display\n"); print_preorder(root); printf("In Order Display\n"); print_inorder(root); printf("Post Order Display\n"); print_postorder(root); /* Search node into tree */ tmp = search(&root, 4); if (tmp) { printf("Searched node=%d\n", tmp->data); } else { printf("Data Not found in tree.\n"); } /* Deleting all nodes of tree */ deltree(root); return 0; }
int main() { char input[3]; int flag,data=0; //dynamic allocatio of the root root=(node*)malloc(sizeof(node)); printf("\nEnter the data to the node"); //assign data scanf("%d",&root->data); root->left=NULL; root->right=NULL; //for new node printf("\nDo you want to enter new node(yes/no):"); scanf("%s",input); flag=string_test(input); //Iterate itself until flag is 1 i. the string entered is yes while(flag==1) { //for new node printf("\nEnter the data to the new node"); //scan data scanf("%d",&data); //calling the create node function to create new node create_node(root,data); printf("\nDo you want to enter new node(yes/no):"); scanf("%s",input); //calling string_test fuction to check whether the input is yes or no flag=string_test(input); } //display the tree in pre_order printf("\npreorder\n"); print_preorder(root); //display the tree in in_order printf("\ninorder\n"); print_inorder(root); //display the tree in post_order printf("\npostorder\n"); print_postorder(root); return 0; }
int main(int argc, char **argv){ t_nodo *albero = NULL; int idx = 0; int max = argc; int result; albero = ins_nodo_prefissa(albero,argv,&idx); albero = ins_nodo_postfissa(albero,argv,&max); result = calcola(albero); printf("\npreorder: \n"); print_preorder(albero); printf("\npostorder: \n"); print_postorder(albero); printf("\ninorder: \n"); print_inorder(albero); printf("\nRisultato = %d\n",result); return 0; }
int main() { struct Tnode *root = NULL; root = create_node(1); root->left = create_node(2); root->left->left = create_node(4); root->left->right = create_node(5); root->right = create_node(3); root->right->right = create_node(7); printf("Pre-order\n"); print_preorder(root); printf("\n"); pre_order_iterative(root); printf("Post-order\n"); print_postorder(root); printf("\n"); post_order_iterative(root); printf("In-order\n"); print_inorder(root); printf("\n"); in_order_iterative(root); }
int main() { struct hash *my_hash, *my_hash2; int x; /* Testing Hash with Linked List items */ struct list_t *root, *root2, *root3; int a=43,b=63,c=99,d=70,e=25,f=17,g=57,h=69,i=111,j=120,k=70,l=73,m=75; root = list_init(); root2 = list_init(); root3 = list_init(); root = list_remove_rear(root); root = list_remove_any(root,&k); root = list_remove_front(root); list_insert_rear(root, &a); list_insert_rear(root, &b); root = list_insert_after(root,&c,3); list_insert_rear(root, &d); root = list_insert_front(root, &e); root = list_insert_front(root, &f); list_insert_rear(root, &g); root = list_remove_front(root); list_insert_rear(root, &h); root = list_insert_after(root,&i,5); root = list_insert_after(root,&j,120); root = list_remove_any(root,&k); root = list_remove_front(root); list_insert_rear(root, &l); root = list_remove_any(root,&l); list_insert_rear(root, &m); root = list_remove_rear(root); root = list_remove_rear(root); root = list_remove_rear(root); root = list_remove_rear(root); root = list_remove_rear(root); root = list_remove_rear(root); root = list_remove_rear(root); my_hash = hash_init("list"); hash_insert(my_hash, "key1", root); hash_insert(my_hash, "key2", root2); hash_insert(my_hash, "key3", root3); printf("%s\n",my_hash->hash_type); char* s_val = "key2"; char* r_val = hash_get(my_hash, s_val); printf("%s - %s\n", s_val, r_val); hash_iterate(my_hash); hash_destroy(my_hash); /* Testing Hash with Binary Tree items */ struct node *root_node = NULL, *root_node2 = NULL, *root_node3 = NULL; root_node = insert(root_node,5,NULL); root_node = delete_node(root_node,5); root_node = insert(root_node,7,NULL); root_node = insert(root_node,3,NULL); root_node = insert(root_node,6,NULL); root_node = insert(root_node,9,NULL); root_node = insert(root_node,12,NULL); root_node = insert(root_node,1,NULL); print_preorder(root_node); root_node = delete_node(root_node,7); my_hash2 = hash_init("tree"); hash_insert(my_hash2, "key1", root_node); hash_insert(my_hash2, "key2", root_node2); hash_insert(my_hash2, "key3", root_node3); hash_iterate(my_hash2); hash_destroy(my_hash2); return 0; }
int main() { /* tree0 */ /* M */ /* / \ */ /* / \ */ /* A K */ /* / \ / \ */ /* C E F G */ /* / \ */ /* N P */ BTN N = {1,'N',(BTNP)0,(BTNP)0}; BTN P = {2,'P',(BTNP)0,(BTNP)0}; BTN C = {3,'C',(BTNP)0,(BTNP)0}; BTN G = {4,'G',(BTNP)0,(BTNP)0}; BTN E = {5,'E',&N,(BTNP)0}; BTN F = {6,'F',(BTNP)0,&P}; BTN A = {7,'A',&C,&E}; BTN K = {8,'K',&F,&G}; BTN M = {9,'M',&A,&K}; BTNP tree0 = &M; printf("The weight and height of tree0 are %d and %d.\n", weightBT(tree0), heightBT(tree0)); printf("tree0 preorder is "); print_preorder(tree0); printf("\ntree0 inorder is "); print_inorder(tree0); printf("\ntree0 postorder is "); print_postorder(tree0); printf("\n"); BTNP tree1=0; insertBST(4,'G',&tree1); insertBST(5,'E',&tree1); insertBST(3,'C',&tree1); insertBST(2,'P',&tree1); insertBST(7,'A',&tree1); insertBST(8,'K',&tree1); insertBST(1,'N',&tree1); insertBST(9,'M',&tree1); insertBST(6,'F',&tree1); // print_preorder(tree1); // printf("\n"); // BTN temp = {4, 'G', (BTNP) 0, (BTNP) 0}; // BTNP tempp = (BTNP) malloc(sizeof(BTN)); // *tempp = temp; // print_preorder(tempp); // BTNP temp2 = make_BTN(4, 'G', (BTNP) 0, (BTNP) 0); // print_preorder(temp2); // printf("\n%d\n", weightBT(temp2)); printf("The weight and height of tree1 are %d and %d.\n", weightBT(tree1), heightBT(tree1)); printf("tree1 preorder is "); print_preorder(tree1); printf("\ntree1 inorder is "); print_inorder(tree1); printf("\ntree1 postorder is "); print_postorder(tree1); printf("\n"); // BTNP tree2 = &N; // BTNP tree3 = findBST(0, tree2); // printf("%d\n", (int) *tree3); // printBTN(tree3); // printf("\n"); BTNP np; int i; for( i=0; i<11; i++){ np = findBST(i,tree1); printf( " key %2d",i) ; if( np ){ printf( " has name "); printBTN(np); printf("\n");; } else printf(" is not in tree1.\n"); } // findBST(4,tree1)->name = 'b'; insertBST(4,'b', &tree1); printf("After changing the name of key 4\n"); for( i=0; i<11; i++){ np = findBST(i,tree1); printf( " key %2d",i) ; if( np ){ printf( " has name "); printBTN(np); printf("\n");; } else printf(" is not in tree1.\n"); } return 0; }
void print_preorder(BTree *tree) { if (tree == NULL) return; printf("%s\n", tree->str); print_preorder(tree->left); print_preorder(tree->right); }
void btree::print(){ print_preorder(root); std::cout << std::endl; print_inorder(root); std::cout << std::endl; }
void btree::print_preorder(node* root){ if(root == NULL) return; std::cout << root->value << " "; print_preorder(root->left); print_preorder(root->right); }
int identity(){ binary_tree *tree = init_bt(); read_db_to_tree(tree); node* tmp; char buffer[50]; float fbuff; int cmd; FILE *f; while(1){ printf("########################\n"); printf("# 1. Print In-Order #\n"); printf("# 2. Search #\n"); printf("# 3. Modify #\n"); printf("# 4. Delete #\n"); printf("# 5. Exit #\n"); printf("########################\n"); printf("Enter your choice: "); scanf("%d",&cmd); switch (cmd){ case 1: inorder(tree->root); break; case 2: printf("Enter IDENTITY NUMBER: "); scanf("%s",&buffer); tmp = bt_search(buffer, tree->root); if (tmp != NULL) { printf("\nResult: "); print_node(tmp); printf("\n"); } break; case 3: printf("Enter IDENTITY: "); scanf("%s", &buffer); tmp = bt_search(buffer, tree->root); if (tmp != NULL){ printf("Enter Name: "); scanf("%s",&buffer); strcpy(tmp->data->name, buffer); printf("Enter Sirname: "); scanf("%s",&buffer); strcpy(tmp->data->sirname, buffer); printf("Enter Average Grade: "); scanf("%f",&fbuff); tmp->data->avg_grade = fbuff; printf("\n\n"); print_node(tmp); } fflush(stdin); break; case 4: printf("Enter IDENTITY: "); scanf("%s",&buffer); delete_element(buffer, tree->root); break; case 5: // Clear the previous database file f = fopen("student_database.txt", "w"); fclose(f); print_preorder(tree->root); return 0; default: printf("Bad Command\n"); break; } } }
void bst<T>::print_preorder() const { print_preorder(root); std::cout << std::endl; }
int main(int argc, char **argv) { BiTree tree; BiTreeNode *node; int i; /***************************************************************************** * * * Initialize the binary tree. * * * *****************************************************************************/ bitree_init(&tree, free); /***************************************************************************** * * * Perform some binary tree operations. * * * *****************************************************************************/ fprintf(stdout, "Inserting some nodes\n"); if (insert_int(&tree, 20) != 0) return 1; if (insert_int(&tree, 10) != 0) return 1; if (insert_int(&tree, 30) != 0) return 1; if (insert_int(&tree, 15) != 0) return 1; if (insert_int(&tree, 25) != 0) return 1; if (insert_int(&tree, 70) != 0) return 1; if (insert_int(&tree, 80) != 0) return 1; if (insert_int(&tree, 23) != 0) return 1; if (insert_int(&tree, 26) != 0) return 1; if (insert_int(&tree, 5) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); i = 30; if ((node = search_int(&tree, i)) == NULL) { fprintf(stdout, "Could not find %03d\n", i); } else { fprintf(stdout, "Found %03d...Removing the left tree below it\n", i); bitree_rem_left(&tree, node); fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); } i = 99; if ((node = search_int(&tree, i)) == NULL) { fprintf(stdout, "Could not find %03d\n", i); } else { fprintf(stdout, "Found %03d...Removing the right tree below it\n", i); bitree_rem_right(&tree, node); fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); } i = 20; if ((node = search_int(&tree, i)) == NULL) { fprintf(stdout, "Could not find %03d\n", i); } else { fprintf(stdout, "Found %03d...Removing the right tree below it\n", i); bitree_rem_right(&tree, node); fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); } i = bitree_is_leaf(bitree_root(&tree)); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (0=OK)\n", i); i = bitree_is_leaf(bitree_left((bitree_root(&tree)))); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (0=OK)\n", i); i = bitree_is_leaf(bitree_left(bitree_left((bitree_root(&tree))))); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (1=OK)\n", i); i = bitree_is_leaf(bitree_right(bitree_left((bitree_root(&tree))))); fprintf(stdout, "Testing bitree_is_leaf...Value=%d (1=OK)\n", i); fprintf(stdout, "Inserting some nodes\n"); if (insert_int(&tree, 55) != 0) return 1; if (insert_int(&tree, 44) != 0) return 1; if (insert_int(&tree, 77) != 0) return 1; if (insert_int(&tree, 11) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bitree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); print_preorder(bitree_root(&tree)); fprintf(stdout, "(Inorder traversal)\n"); print_inorder(bitree_root(&tree)); fprintf(stdout, "(Postorder traversal)\n"); print_postorder(bitree_root(&tree)); /***************************************************************************** * * * Destroy the binary tree. * * * *****************************************************************************/ fprintf(stdout, "Destroying the tree\n"); bitree_destroy(&tree); return 0; }