Ejemplo n.º 1
0
Archivo: Tree.c Proyecto: goutamdh/Tree
int main(void) {
	Node *root = NULL;
	int choice, ele;

	setbuf(stdout, NULL);

	printf("Enter choice : 1 for add Binary Search Tree Iteratively\n2 for add Binary Search Tree Recursively\n3 for display in Pre Order\n4 for display in In Order\n5 for display in Post Order\n6 to count no of BST Nodes\n7 to count Height of BST\n8 to check if it is Strict BST\n9 to check if it is Complete BST\n10 for Binary Search Recursively\n11 for Binary Search Recursively\n12 to add to Binary Tree Iteratively\n13 to add to Binary Tree Recursively\n14 to find Largest element in BT\n15 to count number of nodes in BT\n");

	do{
		printf("Enter choice : ");
		scanf("%d", &choice);

		switch (choice) {
		case 1:
			// Add BST Iterative
			printf("Enter a value\n");
			scanf("%d", &ele);

			addBinarySearchTree(&root, ele);
			break;
		case 2:
			// Add BST Recursively
			printf("Enter a value\n");
			scanf("%d", &ele);

			addBinarySearchTreeRecursively(&root, ele);

			break;
		case 3:
			// Display Pre Order
			displayPreOrder(root);
			printf("\n");

			break;
		case 4:
			// Display In Order
			displayInOrder(root);
			printf("\n");

			break;
		case 5:
			// Display Post Order
			displayPostOrder(root);
			printf("\n");

			break;
		case 6:
			// Count Binary Search Tree Node
			ele = countBinarySearchTreeNodes(root);
			printf("%d\n",ele);

			break;
		case 7:
			// Height Binary Search Tree Node
			ele = heightBinarySearchTree(root);
			printf("%d\n", ele);

			break;
		case 8:
			// Check if Strict Binary Search Tree

			if(isStrictBinarySearchTree(root) == 1){
				printf("Is Strict Binary Search Tree\n");
			}
			else{
				printf("Not a Strict Binary Search Tree\n");
			}

			break;
		case 9:
			// Check if Complete Binary Search Tree

			if(isCompleteBinarySearchTree(root) >= 1){
				printf("Is Strict Binary Search Tree\n");
			}
			else{
				printf("Not a Strict Binary Search Tree\n");
			}

			break;
		case 10:
			// Binary Search Tree Iteratively
			printf("Enter a value to search\n");
			scanf("%d", &ele);

			if(binarySearchIterative(root, ele) == 1){
				printf("Exists\n");
			}
			else{
				printf("Does not exist\n");
			}

			break;
		case 11:
			// Binary Search Tree Recursively
			printf("Enter a value to search\n");
			scanf("%d", &ele);

			if(binarySearchRecursion(root, ele) == 1){
				printf("Exists\n");
			}
			else{
				printf("Does not exist\n");
			}

			break;
		case 12:
			// Add Binary Tree Iteratively

			printf("Enter a value\n");
			scanf("%d", &ele);

			addBinaryTreeIteratively(&root, ele);
			break;
		case 13:
			// Add Binary Tree Recursively

			printf("Enter a value\n");
			scanf("%d", &ele);

			addBinaryTreeRecursively(&root, ele);

			break;
		case 14:
			// Largest Binary Tree
			ele = largestBinaryTree(root);

			printf("Largest element is : %d",ele);

			break;

		case 15:
			// Count No of Binary Tree Nodes
			ele = countBinaryTreeNodes(root);
			printf("%d\n", ele);

			break;

		default:{
			choice = -1;
		}
		}


	}while(choice != -1);


	return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
int main(int argc, char const *argv[]){
  int size, value, option;
  bool find;
	srand(time(0));
  size = (rand()%100)+1;
  int *list = new int[size];
	for(int i = 0; i < size; i++){
    list[i] = (rand()%100)+1;
  }
  value = (rand()%100);
  option = 0;
  switch (option) {
    case 0:
      {
        auto start = std::chrono::steady_clock::now();
        find = binarySearchIterative(list, 0, size-1, value);
        auto end = std::chrono::steady_clock::now();
        auto diff = end - start;
        if(find)
        {
          std::cout << "You find the element using the binary search iterative." << std::endl;
        }
        else{
          std::cout << "You not find the element using the binary search iterative." << std::endl;
        }
        std::cout << "And the time of search was " << std::chrono::duration <double, std::milli> (diff).count() << " ms." << std::endl;
      }
      break;
    case 1:
      {
        auto start = std::chrono::steady_clock::now();
        find = binarySearchRecursive(list, 0, size-1, value);
        auto end = std::chrono::steady_clock::now();
        auto diff = end - start;
        if(find)
        {
          std::cout << "You find the element using the binary search recursive." << std::endl;
        }
        else{
          std::cout << "You not find the element using the binary search recursive." << std::endl;
        }
        std::cout << "And the time of search was " << std::chrono::duration <double, std::milli> (diff).count() << " ms." << std::endl;
      }
      break;
    default:
      std::cout << "This isn't a valid option, please restart the program." << std::endl;
      break;
  }
	option = 1;
  switch (option) {
    case 0:
      {
        auto start = std::chrono::steady_clock::now();
        find = binarySearchIterative(list, 0, size-1, value);
        auto end = std::chrono::steady_clock::now();
        auto diff = end - start;
        if(find)
        {
          std::cout << "You find the element using the binary search iterative." << std::endl;
        }
        else{
          std::cout << "You not find the element using the binary search iterative." << std::endl;
        }
        std::cout << "And the time of search was " << std::chrono::duration <double, std::milli> (diff).count() << " ms." << std::endl;
      }
      break;
    case 1:
      {
        auto start = std::chrono::steady_clock::now();
        find = binarySearchRecursive(list, 0, size-1, value);
        auto end = std::chrono::steady_clock::now();
        auto diff = end - start;
        if(find)
        {
          std::cout << "You find the element using the binary search recursive." << std::endl;
        }
        else{
          std::cout << "You not find the element using the binary search recursive." << std::endl;
        }
        std::cout << "And the time of search was " << std::chrono::duration <double, std::milli> (diff).count() << " ms." << std::endl;
      }
      break;
    default:
      std::cout << "This isn't a valid option, please restart the program." << std::endl;
      break;
  }
  delete[] list;
	return 0;
}