void btree::print_inorder(node* root){ if(root == NULL) return; print_inorder(root->left); std::cout << root->value << " "; print_inorder(root->right); }
void menu() { int c=0,data; BINNODE temp; while(c!=9) { printf("\t\t\t\tMenu for AVL tree build and Traversal\n"); printf("\t\t\t1.Insert into AVL tree\n"); printf("\t\t\t2.Search in AVL tree\n"); printf("\t\t\t3.Height of the tree\n"); printf("\t\t\t4.Number of nodes in the tree\n"); printf("\t\t\t5.Inorder Traversal\n"); printf("\t\t\t9.Exit\n"); printf("\t\t\t\tEnter your choice:"); scanf("%d",&c); switch(c) { case 1: printf("Enter the data to be inserted:"); scanf("%d",&data); if(root==NULL) { if((temp=(BINNODE)malloc(sizeof(struct binnode)))\ ==NULL) printf("Error 1:\ Memory Allocation Problem.\n"); else { temp->data=data; temp->left=NULL; temp->right=NULL; temp->balance_factor=0; root=temp; } } else insert_node(data); break; case 2: printf("Enter the data to serach:"); scanf("%d",&data); if((temp=search_node(root,data))==NULL) printf("Node does not exist.\n"); else printf("Node found,with balance factor:%d\n" ,temp->balance_factor); break; case 3: printf("Height of the tree:%d\n"\ ,tree_height(root)); break; case 4: printf("Number of nodes in the tree:%d\n"\ ,count_node(root)); break; case 5: print_inorder(root); printf("\n"); break; case 9: deallocate(root); printf("Exiting...\n"); break; } }
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; }
void bst<T>::print_inorder() const { print_inorder(root); std::cout << std::endl; }
//print the tree inorden= ordenated report void report(node *root){ print_inorder(root); }