int main()
{
	PriorityQueue h1;
	PriorityQueue h2;	
	int data[] =  {21, 10, 23, 14, 3, 26, 17, 8};	
	int data2[] = {18, 12, 33, 24, 6, 37, 7, 18};	
	int i;

	h1 = insert(data[0], NULL);
	for(i=1; i<8; i++)
	{
		h1 = insert(data[i], h1);
	}
	printf("\n=== after the leftist heap h1 is merged===\n");
	printPreorder(1, h1);

	h2 = insert(data2[0], NULL);
	for(i=1; i<8; i++)
	{
		h2 = insert(data2[i], h2);
	}
	printf("\n=== after the leftist heap h2 is merged===\n");
	printPreorder(1, h2);
	 
	h1 = merge(h1, h2);
	printf("\n=== after both h1 and h2 are merged===\n");
	printPreorder(1, h1);

	h1 = deleteMin(h1);
	printf("\n=== after executing deleteMin operation ===\n");
	printPreorder(1, h1);

	return  0;
}
int main()
{
	BinaryTree BinaryTree;

	BinaryTree = createBinaryTree();
	printf("\n ====== test for postordering the BinaryTree presented by left_right_child structure  ====== \n");	 
	
	printf("\n test for respectively inserting 'A' and 'B' into left and right child of the parent '/' , then 'C' and 'D' into the left and right child of the parent 'A' \n");	
	insert('A', find('/', BinaryTree), 1);	// 1 means left child
	insert('B', find('/', BinaryTree), 0);	// 0 means right child
	insert('C', find('A', BinaryTree), 1);
	insert('D', find('A', BinaryTree), 0);
	printPreorder(1, BinaryTree);
	
	printf("\n test for respectively inserting 'A' and 'B' into left and right child of the parent '/' \n");	
	insert('E', find('/', BinaryTree), 1);
	insert('F', find('/', BinaryTree), 0);
	printPreorder(1, BinaryTree); 

	printf("\n test for inserting 'E' into the right child of the  parent 'B' , then repectively 'F' and 'G' into the left and right child of the parent 'H' \n");	
	insert('E', find('B', BinaryTree), 0);		
	insert('F', find('E', BinaryTree), 1);
	insert('G', find('E', BinaryTree), 0);
	printPreorder(1, BinaryTree); /**/

	return 0;
}
void printPreorder(node *root){
  if(root != 0){
    printf("%d\n", root->key);
    printPreorder(root->left);
    printPreorder(root->right);
  }
}
Beispiel #4
0
void BinarySearchTree::printPreorder(Node *node) {
    if (node == NULL) {
        return;
    }
    print(node);
    printPreorder(node->left);
    printPreorder(node->right);
}
void printPreorder(struct node* node) {
	if (node == NULL)
		return;

	std::cout << node->data << " ";
	printPreorder(node->left);
	printPreorder(node->right);
}
Beispiel #6
0
void printPreorder(struct node * root)
{
	if(root==NULL)
		return;
	printf(" %d ",root->data);
	printPreorder(root->left);
	printPreorder(root->right);
}
void printPreorder (struct node * node)
{
  if (node == NULL)
  {
    return;
  }

  printf("%d ", node->data);
  printPreorder (node->left);
  printPreorder (node->right);
}
Beispiel #8
0
/* print the tree preorder */
void printPreorder(struct avl_node *node)
{
	if (node == NULL)
		return;
	/* process the root */
	printf("%d ", node->key);
	/* process the left node */
	printPreorder(node->left);
	/* process the right node */
	printPreorder(node->right);
}
Beispiel #9
0
void printPreorder(node * tree)
{
  if (tree == NULL) 
  return;
      
  if (tree->split=='-')
  printf("(%le,%le)",tree->width,tree->height);
  else
  printf(("%c"),tree->split);

  printPreorder(TLEFT);
  printPreorder(TRIGHT);
}
Beispiel #10
0
/* Given a binary tree, print its nodes in preorder*/
void printPreorder(struct node* node)
{
     if (node == NULL)
          return;
 
     /* first print data of node */
     printf("%d ", node->data);  
 
     /* then recur on left sutree */
     printPreorder(node->left);  
 
     /* now recur on right subtree */
     printPreorder(node->right);
}    
/*
 pre order traversal of binary tree
*/
void printPreorder(struct binaryTreeNode *node)
{
    if (node == NULL)
        return;

    //deal with the node
    printf("%d ", node->data);

    //recur on left subtree
    printPreorder(node->left);

    //recur on right subtree
    printPreorder(node->right);


}
// analog print directories and files name in the BinarySearchTree, which involves postorder traversal. 
void printPreorder(int depth, BinarySearchTree root)
{			
	int i;
		
	if(root) {		
		for(i = 0; i < depth; i++)
			printf("    ");		
		printf("%d\n", root->value);
		printPreorder(depth + 1, root->left);											
		printPreorder(depth + 1, root->right); // Attention: there's difference between traversing binary tree and common tree							
	}
	else {
		for(i = 0; i < depth; i++)
			printf("    ");		
		printf("NULL\n");
	}
}
Beispiel #13
0
int main(int argc, char **argv)
{
	//Checking error
	if (argc < 2)
	{
		printf("usage: %s <input_filename>\n", argv[0]);
		return 0;
	}
	//Open file
	FILE *in = fopen(argv[1], "r");
	if (in == NULL) return 0;
	//Declaration of variable
	int i, size,level;
	//Scanning size from file
	fscanf(in, "%d", &size);
	//allocating memory for array
	int *array = malloc(size * sizeof(int));
	//Scanning number from the file
	for (i=0; i<size; i++)
		fscanf(in, "%d", &array[i]);
	//Declaring a node
	bst *root =NULL;
	//Creating BST
	root=createBST(array,size);
	//print in-order,pre-order,post-order tree traversal
	printf("Creating Binary Search Tree...\n");
	printf("In-order traversal: "); 
	printInorder(root); 
	printf("\n");
	printf("Pre-order traversal: ");
	printPreorder(root);
	printf("\n");
	printf("Post-order traversal: ");
	printPostorder(root);
	printf("\n");
	//Sort arrat
	sort(array,size);
	//Creatin binary tree with minimum level
	root=createMinBST(array,0,size-1);
	//printing BST
	printf("Minimum Height BST\n");
	printTree(root);
	//Bonus
	printf("Bonus.Please enter level of the tree:");
	scanf("%d",&level);
	if(level>height(root))
	printf("\nThe maximum height of this tree is %d",height(root));
	else
	printGivenLevel(root,level);

 return 0;
}
Beispiel #14
0
int main(int argc, char *argv[]){

  //check command line arguments
  if(argc != 2){
    printf("usage: ./a.out <input_filename>");
    exit(0);
  }
  
  //open the file
  FILE* input = fopen(argv[1],"r");
  if(input == NULL){
    printf("File not found");
    exit(0);
  }

  bst* root = NULL; 
  int i = 0; 
  int size = 0;
  
  
  fscanf(input,"%d",&size);

  int *a = (int *)malloc(sizeof(int)*size);//create the array
  
  for(i=0;i<size;i++){
    fscanf(input,"%d",&a[i]);
  }
  
  close(input); //close file

  //printout the different ways
  printf("Creating Binary Search Tree\n");
  root = createBST(a,size);
  printf("In-Order traversal: ");
  printInorder(root); 
  printf("\nPre-Order traversal: ");
  printPreorder(root);
  printf("\nPost-Order traversal: ");
  printPostorder(root);

  printf("\n\nMinimum Height BST\n");
  sort(a,size);

  root = createMinBST(a,0,size-1);
  
  printTree(root);

  free(a); //free mem
  return 0;

}
Beispiel #15
0
int main(int argc, char **argv)
{
	if(argc < 2)
	{
		printf("usage: %s <input_filename>/n", argv[0]);
		return;
	}

	FILE* input = fopen(argv[1], "r");
	if (input == NULL)
		return;

	int i, size;
	fscanf(input, "%d", &size);
	int array[size];

	for(i=0; i<size; i++)
		fscanf(input, "%d", &array[i]);

	//Creates Binary Tree
	printf("Creating Binary Tree...\n");
	bst* root = createBST(array, size);

	//Printing Inorder Traversal
	printf("Inorder Traversal: ");
	printInorder(root);
	printf("\n");

	//Printing Pre-order Traversal
	printf("Pre-Order Traversal: ");
        printPreorder(root);
        printf("\n");

	//Printing Postorder Traversal
	printf("Postorder Traversal: ");
        printPostorder(root);
        printf("\n");

	//Printing the Binary Tree
	printf("\nMinimum Height BST\n");
	sort(array, size);
	root = createMinBST(array, array[0], array[size-1]);
	printTree(root);

	fclose(input);
	free(root);

	return 0;
}
int main(int argc, char** argv){
  node *root = NULL;

  insert(5, &root);
  insert(10, &root);
  insert(8, &root);
  insert(3, &root);
  insert(4, &root);

  printPreorder(root);
  printInorder(root);
  printPostorder(root);

  printf("%d was found\n", (search(8, root)->key));

  destroyTree(root);
}
Beispiel #17
0
int main() {
	node *root;
	node *tmp;
	//int i;

	root = NULL;

	//insert nodes
	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");
    printPreorder(root);

    printf("In Order Display\n");
    printInorder(root);

    printf("Post Order Display\n");
    printPostorder(root);

    // Search node into tree 
    tmp = searchTree(&root, 4);
    if (tmp)
    {
        printf("Searched node=%d\n", tmp->data);
    }
    else
    {
        printf("Data Not found in tree.\n");
    }

    //Deleting all nodes of tree
    deleteTree(root);
    
	return 0;
}
Beispiel #18
0
/* 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("\nPreorder traversal of binary tree is \n");
     printPreorder(root);
 
     printf("\nInorder traversal of binary tree is \n");
     printInorder(root);  
 
     printf("\nPostorder traversal of binary tree is \n");
     printPostorder(root);
 
     getchar();
     return 0;
}
int main()
{
	struct node *root = NULL;
	root = insert(root, 4);
	root = insert(root, 2);
	root = insert(root, 3);
	root = insert(root, 1);
	root = insert(root, 5);

	root = mirror(root);
	std::cout << sameTree(root, root) << std::endl;
	std::cout << size(root) << std::endl;
	std::cout << maxDepth(root) << std::endl;
	printInoder(root);
	std::cout << std::endl;
	printPostorder(root);
	std::cout << std::endl;
	printPreorder(root);
	return 0;
}
int main()
{
	BinarySearchTree bst;	
	int value[] = {2, 8, 1, 5, 3, 4};
	int i;

	printf("\n ====== test for preordering the BinarySearchTree ====== \n");	 	
	
	printf("\n test for creating a binary search tree with root value 6 \n");	
	bst = createBinarySearchTree(6);
	printPreorder(1, bst); 
		
	printf("\n test for insertint 6 nodes, that's  2, 8, 1, 5, 3 and 4 in turn \n");		
	for(i = 0; i < 6; i++)
		insert(value[i], bst);		
	printPreorder(1, bst); 	 	 

	printf("\n test for find minimum \n");		
	printf(" the minimum is %2d, in this binary search tree! \n", findMin(bst)->value);

	printf("\n test for find maximum \n");		
	printf(" the maximum is %2d, in this binary search tree! \n", findMax(bst)->value);

	printf("\n test for deleting node '2' with two nodes from the binary search tree \n");		
	deleteBinarySearchTree(2, bst);
	printPreorder(1, bst); 	 	 

	printf("\n test for deleting node '5' with one node from the binary search tree \n");		
	deleteBinarySearchTree(5, bst);
	printPreorder(1, bst); 	 	 

	printf("\n test for deleting node '8' with zeron node from the binary search tree \n");		
	deleteBinarySearchTree(8, bst);
	printPreorder(1, bst); 	 	 
	
	printf("\n test for inserting '8', '5' and '2' into the binary search tree \n");		
	insert(8, bst);
	insert(5, bst);
	insert(2, bst);
	printPreorder(1, bst); 	 	 

	return 0;
}
Beispiel #21
0
int main(int argc, char * * argv)
{
if (argc != 3)
{
  printf("usage: ./proj4 input output\n");
  return EXIT_FAILURE;
}

int index = 0;
double x = 0, y=0; 

// Build binary tree
stack * root = loadFile(argv[1], &index);
node * head = nodeCreate(root->stackNode->width, root->stackNode->height, root->stackNode->split, root->stackNode->index-1); //= buildTree(root);
root = StackPop(root);
buildTree(root, head, &head, index);
node * tree = NULL;
tree = nodeArrange(tree, &head);

// Packing
clock_t timeStart = clock();
assignCutline(tree);
setCoord(tree);
clock_t timeEnd = clock();
double packTime = (double) (timeEnd - timeStart) / CLOCKS_PER_SEC;
searchNode(tree, &x, &y, 1);
//printPostorderFull(tree);

//Reset output file and save output
FILE * fptr = fopen(argv[2], "w");
saveTree(tree, argv[2]);

// Screen dump
printf("Preorder: ");
printPreorder(tree);
printf("\n\nInorder: ");
printInorder(tree);
printf("\n\nPostorder: ");
printPostorder(tree);
printf("\n\nWidth: %le", tree->width);
printf("\nHeight: %le", tree->height);
printf("\n\nX-coordinate: %le", x);
printf("\nY-coordinate: %le", y); 
printf("\n");

// Rerooting
box * minBox = malloc(sizeof(box));
minBox->width = tree->width;
minBox->height = tree->height;

timeStart = clock();
// ------------------------------------ to run cases without rerooting, comment out the following lines
while (tree->right->split!='-')
{
tree = reroot(tree, minBox);
decideMin(minBox, tree);
//printPostorderFull(tree);
}
// ------------------------------------
timeEnd = clock();
double rootTime = (double) (timeEnd - timeStart) / CLOCKS_PER_SEC;


printf("\nElapsed Time: %le", packTime);
printf("\n\nBest width: %le", minBox->width);
printf("\nBest height: %le\n", minBox->height);
printf("\nElapsed Time for re-routing: %le\n", rootTime);

// Free stack
while(root!=NULL){
  root = StackPop(root);
}

// Memory management
free(root);
free(minBox);
deleteTree(head);
deleteTree(tree);
fclose(fptr);

return EXIT_SUCCESS;
}
Beispiel #22
0
void BinarySearchTree::printPreorder() const {
    print("Pre-order    : ");
    printPreorder(root);
    println();
}