int test_insert() {
  BTree *tree;
  BTree *find;
  int depth; 
  tree = NULL;
  
  printf("The numbers should be in ascending order.\n");
  
  btree_insert(&tree, "5");
  btree_insert(&tree, "1");
  btree_insert(&tree, "6");
  btree_insert(&tree, "9");
  btree_insert(&tree, "0");
  btree_insert(&tree, "4");
  btree_insert(&tree, "3");
  btree_insert(&tree, "8");
  btree_insert(&tree, "2");
  btree_insert(&tree, "7");
  btree_insert(&tree, "4");

  /*print_preorder(tree);*/
  printLevelOrder(tree,5);
  depth  = btree_depth(tree);
  printf("d_single:%d\n", depth);
  find = btree_find(tree, "Z");
  /*print_preorder(find);*/
  if(find == NULL){
    printf("Not found.\n");
  }else{
    printLevelOrder(find,5);
  }
  btree_free(tree);
  return 0;
}
int main() {
  BTree *tree;
  BTree *find;
  int depth;
  char *array[11];
  
  array[0] = "5";
  array[1] = "1";
  array[2] = "6";
  array[3] = "9";
  array[4] = "0";
  array[5] = "4";
  array[6] = "3";
  array[7] = "8";
  array[8] = "2";
  array[9] = "7";
  array[10] = "4";
  
  tree = array_to_btree(array);
  /*print_preorder(tree);*/
  printLevelOrder(tree,5);
  depth  = btree_depth(tree);
  printf("d_array:%d\n", depth);
  find = btree_find(tree, "A");
  /*print_preorder(find);*/
  if(find == NULL){
    printf("Not found.\n");
  }else{
    printLevelOrder(find,5);
  }
  btree_free(tree);
  test_insert();
  return 0;
}
Exemplo n.º 3
0
int main()
{
  struct node* root = newNode(4);
  root->left = newNode(2);
  root->right = newNode(7);
  root->left->left = newNode(1);
  root->left->right = newNode(3);
  /* root->right->right = newNode(9); */
  /* root->left->right->left = newNode(5); */
  /* root->left->right->right = newNode(11); */
  /* root->right->right->left = newNode(4); */

  /* printf("Preorder: "); */
  /* rec_PreOrder(root); */
  /* printf("\nInorder: "); */
  /* rec_InOrder(root); */
  /* printf("\nPostorder: "); */
  /* rec_PostOrder(root); */
  /* printf("\nPreorder_loop: "); */
  //loop_PreOrder(root);
  //loop_InOrder(root);
  //loop_PostOrder(root);

  
  root = insert(root, 6);
  printLevelOrder(root);
    
  
}
int main(){
    struct node* root=newNode(1);
    root->left=newNode(2);
    root->right=newNode(3);
    root->left->left=newNode(4);
    root->right->right=newNode(5);
    printf("level order traversal of binary tree is \n");
    printLevelOrder(root);
    printf("\n");
}
Exemplo n.º 5
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);
  root->left->left->left  = newNode(6);
    printLevelOrder(root);

return 0;
}
void MorseCodeTreeTest::testCase1()
{
    MorseCodeTree tree;
    tree.addNode('2', "..");
    tree.addNode('3', ".-");
    tree.addNode('0', ".");
    tree.addNode('1', "-");
    tree.addNode('4', "-.");
    tree.addNode('5', "--");

    QCOMPARE(printLevelOrder(tree.root()), QString("012345"));
    QChar ch = tree.find("--");
    QVERIFY(ch == '5');
}
/* Driver program to test above functions*/
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("Level Order traversal of binary tree is \n");
  printLevelOrder(root);

  getchar();
  return 0;
}
Exemplo n.º 8
0
int main(int argc, char const *argv[])
{
	BST *tree = NULL;
	tree = insertNode(tree, 12);
	tree = insertNode(tree, 10);
	tree = insertNode(tree, 20);
	tree = insertNode(tree, 9);
	tree = insertNode(tree, 11);
	printf("\nIn Order Traversal:\t");
	inOrder(tree);
	printf("\nPre Order Traversal:\t");
	preOrder(tree);
	printf("\nPost Order Traversal:\t");
	postOrder(tree);
	printf("\nLevel Order Traversal:\t");
	printLevelOrder(tree);
	printf("\n");
	return 0;
}
Exemplo n.º 9
0
void BST<T>::printLevelOrder() const
{
	printLevelOrder(root);
	cout << endl;
}
Exemplo n.º 10
0
int main(int argc, char** argv) {
        //STAMPA ARRAY

	int a[] = {7, 6, 7, 9, 10, 11, 7, 9, 3, 3, 5};
	int i;
        int ris;
        
	printf("Unsorted array is:  ");
	for(i = 0; i < 11; ++i)
		printf(" %d ", *(a+i));

        //heapify(a,11);
	printf("\n\nSorted array is:  ");
	for(i = 0; i < 11; ++i)
		printf(" %d ", a[i]);
      
        
        //STAMPA ALBERI
    /*
    	int a[] = {3, 3, 5, 6, 7, 7, 7, 9, 9, 10, 11};
        int indexI, indexF, dim;
	int i;
        int ris;
    */    
        Tree t,p;

        t=emptyThree();
        t=createABRWPtoARRAY(a, 11, t);
        deleteABRnode(t,6);
        //printf(" %d ", t->left->val);
            printf("\n \n");
        printLevelOrder(t);
            printf("\n"); 

        //STAMPA LISTE
            /*
        char a[]={'c','i','a','a','o'};
        char b[]={'c','i','c','c','i','o'};
        char c[]={'a','i','a','o'};
        char d[]={'p','a','b','l','o'};
        int i;

        char* total[]={a,b,c,d};
        
        Queue que=initqueue();
        
        printf("\n\nUnsorted array is:  ");
	for(i = 0; i < 4; ++i)
		printf(" %s ", *(total+i));
            
            //creo la coda
        printf("\n\nQueue Before is:  ");
        enqueue(que,a);
        enqueue(que,b);
        enqueue(que,c);
        enqueue(que,d);
        printQueue(que);
        
        printf("\n\nQueue After is:  ");
        inverti(que);
        printQueue(que);
             * */
return (EXIT_SUCCESS);
}
Exemplo n.º 11
0
int main()
{
	//treeNode *root = NULL;
	int x;
	int ch, num, num1;
	treeNode * temp;
	treeNode *med;
	int m = 1;

	do {
		printf("\nSelect a choice from the menu below.");
		printf("\n1. Generate Binary Search Tree");
		printf("\n2. Print the BST in pre-order format");
		printf("\n3. Print the BST in in-order format");
		printf("\n4. Print the BST in post-order format");
		printf("\n5. Print the BST in breadth-first format");
		printf("\n6. Find a value in the BST");
		printf("\n7. Find the minimum value in the BST nodes");
		printf("\n8. Find the maximum value in the BST nodes");
		printf("\n9. Calculate the average value of the BST nodes");
		printf("\n10. Find the median value of the BST nodes");
		printf("\n11. Calculate the sum of the BST nodes");
		printf("\n12. Count the number of BST nodes");
		printf("\n13. Delete a value in the BST");
		printf("\n14. Exit Program");
		printf("\n");
		printf("\nEnter Choice: ");
		scanf("%d", &ch);
		switch (ch) {
		case 1: 
			genrateTree();
			printf("\n");
			printf("\nTree in IN-ORDER : ");
			PrintInorder(root);
			printf("\n");
			break;

		case 2:
			PrintPreorder(root);
			break;

		case 3: 
			PrintInorder(root);
			break;

		case 4:
			PrintPostorder(root);
			break;
		case 5:
			printLevelOrder(root);
			break;
		case 6:
			printf("\nEnter the element to be Find in TREE: ");
			scanf("%d", &num);
			temp=Find(root, num);
			if (temp->data == num)
			{
				printf("Element Found\n");
			}
			else
			{
				printf("Element NOT Found\n");
			}
			break;
		case 7:
			temp = FindMin(root);
			printf("Minimum element is %d\n", temp->data);
			break;
		case 8:
			temp = FindMax(root);
			printf("Maximum element is %d\n", temp->data);
			break;
		case 9:
			in_order_sum(root);
			printf("Average element is %d\n", sum / counter(root));
			sum = 0;
			break;
		case 10:
			med = medianTraverse(root, findHeight(root));
			printf("Median Value is %d\n", med->data);
			break;
		case 11:
			in_order_sum(root);
			printf("\nThe sum of all the elements are:%d\n", sum);
			sum = 0;
			break;
		case 12:
			printf("Total Number of Nodes %d\n", counter(root));
			break;
		case 13:
			PrintInorder(root);
			printf("\n");
			printf("\nEnter the element to be Deleted: ");
			scanf("%d", &num);
			Delete(root, num);
			break;
		case 14:
			exit(0);
			break;

		default: exit(0);
		}
		//printf("%d", rootNode->data);
		printf("\n");
		printf("\nIf you want to return to the menu, press 1.");
		printf("\nChoice: ");
		scanf("%d", &num);
	} while (num == 1);
	
}