/* Driver program to test above functions*/
int main()
{
    /*   6
        /  \
       10    2
      / \   / \
     1   3 7  12
     10 and 2 are swapped
    */
 
    struct node *root = newNode(6);
    root->left        = newNode(10);
    root->right       = newNode(2);
    root->left->left  = newNode(1);
    root->left->right = newNode(3);
    root->right->right = newNode(12);
    root->right->left = newNode(7);
 
    printf("Inorder Traversal of the original tree \n");
    printInorder(root);
 
    correctBST(root);
 
    printf("\nInorder Traversal of the fixed tree \n");
    printInorder(root);
 
    return 0;
}
void printInorder(node *root){
  if(root != 0){
    printInorder(root->left);
    printf("%d\n", root->key);
    printInorder(root->right);
  }
}
/* Driver program to test above functions*/
int main()
{
    /* Create following tree as first balanced BST
           100
          /  \
        50    300
       / \
      20  70
    */
    struct node *root1  = newNode(100);
    root1->left         = newNode(50);
    root1->right        = newNode(300);
    root1->left->left   = newNode(20);
    root1->left->right  = newNode(70);

    printInorder(root1);
 
    /* Create following tree as second balanced BST
            80
           /  \
         40   120
    */
    struct node *root2  = newNode(80);
    root2->left         = newNode(40);
    root2->right        = newNode(120);
    printInorder(root2);
 
    struct node *mergedTree = mergeTrees(root1, root2, 5, 3);
 
    printf ("Following is Inorder traversal of the merged tree \n");
    printInorder(mergedTree);
    return 0;
}
void printInorder(struct node* root){
	if(root == NULL){
		return;
	}
	printInorder(root->left);
	printf(" %d ", root->data);
	printInorder(root->right);
}
void printInorder (struct node* node)
{
    if (node == NULL)
        return;
    printInorder(node->left);
    printf("%d ", node->data);
    printInorder(node->right);
}
示例#6
0
void BinarySearchTree::printInorder(Node *node) {
    if (node == NULL) {
        return;
    }
    printInorder(node->left);
    print(node);
    printInorder(node->right);
}
示例#7
0
// Print the tree in-order
void printInorder(struct node *p)
{
  if(p != NULL)
    {
      printInorder(p->left);
      printf("%d %s\n", p->count, p->word);
      printInorder(p->right);
    }
    
} // end printInorder
示例#8
0
void BST<ItemType>::printInorder(TreeNode<ItemType>*n,ofstream &file)
{
    if(n == NULL) return;
    
    printInorder(n->left,file);
    
    file << n->element << " ";
    
    printInorder(n->right,file);
}
示例#9
0
文件: avl.c 项目: jared-hess/CS460
/* print the tree inorder */
void printInorder(struct avl_node *node)
{
	if (node == NULL)
		return;
	/* process left node */
	printInorder(node->left);
	/* process root */
	printf("%d ", node->key);
	/* process right node */
	printInorder(node->right);
}
示例#10
0
void printInorder(node * tree)
{
  if (tree == NULL) 
  return;

  printInorder(TLEFT);
  if (tree->split=='-')
  printf("(%le,%le)",tree->width,tree->height);
  else
  printf(("%c"),tree->split);
  printInorder(TRIGHT);
}
/**
 * A utility function to print inorder traversal of a given binary tree
 */
void printInorder(struct node* node)
{
    if (node == NULL)
        return;
 
    /* first recur on left child */
    printInorder(node->left);
 
    printf("%d ", node->data);
 
    /* now recur on right child */
    printInorder(node->right);
}
/* Utility function to print inorder traversal of Binary Tree */
void printInorder (struct node* node)
{
    if (node == NULL)
        return;
 
    /* first recur on left child */
    printInorder (node->left);
 
    /* then print the val of node */
    printf("%d ", node->val);
 
    /* now recur on right child */
    printInorder (node->right);
}
/*
 in order traversal of binary tree
*/
void printInorder(struct binaryTreeNode *node)
{
    if (node == NULL)
        return;

    //first recur on left subtree
    printInorder(node->left);

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

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

}
示例#14
0
int main(int argc, char* argv[])
{
  node *root;
  root = NULL;

  int i;
 
  if(argc > 1)
    {

      for(i = 1; i < argc; i++)
	{
	  root = addNode(root, argv[i]);
	}


      printInorder(root);

    }

  else 
    printf("%s\n", "no word");

  return 0;

} // end main()
示例#15
0
/* Driver function to test above functions */
int main()
{
  struct node *root = NULL;
  int x;
 
  /* Constructing tree given in the above figure */
  root = newNode(10);
  root->left = newNode(-2);
  root->right = newNode(6);
  root->left->left = newNode(8);
  root->left->right = newNode(-4);
  root->right->left = newNode(7);
  root->right->right = newNode(5);
root->left->left->left = newNode(1);
//root->left->left->right = newNode(2);
//root->left->right->left = newNode(3);
//root->left->right->right = newNode(4);
 
  toSumTree(root);
 
  // Print inorder traversal of the converted tree to test result of toSumTree()
  printf("Inorder Traversal of the resultant tree is: \n");
  printInorder(root);
 
  getchar();
  return 0;
}
int main(){
	node* root = NULL;
    root = insert(root, 6);
    root = insert(root, -13);
    root = insert(root, 14);
    root = insert(root, -8);
    root = insert(root, 15);
    root = insert(root, 13);
    root = insert(root, 7);
 
    cout << "Inorder traversal of the given tree is: ";
    printInorder(root);
	removeOutsideRange(root, -10, 13);
 
    cout << "\nInorder traversal of the modified tree is: ";
    printInorder(root);
    getchar();
}
int main(){
	int k = 4;
    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(7);
    root->right->right = newNode(6);
    root->right->right->left = newNode(8);
    cout << "Inorder Traversal of Original tree" << endl;
    printInorder(root);
    cout << endl;
    cout << "Inorder Traversal of Modified tree" << endl;
    node *res = removeShortPathNodes(root, 1, k);
    printInorder(res);
	return 0;
}
int main(){
	node *root = newNode(50);
  root->left        = newNode(7);
  root->right       = newNode(2);
  root->left->left  = newNode(3);
  root->left->right = newNode(5);
  root->right->left  = newNode(1);
  root->right->right = newNode(30);
 
  printf("\n Inorder traversal before conversion ");
  printInorder(root);
 
  convertTree(root);
 
  printf("\n Inorder traversal after conversion ");
  printInorder(root);
	return 0;
}
int main(){
	int pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7};
    int post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
    int size = sizeof( pre ) / sizeof(int);
	int preStart = 0;
    struct node *root = constructTree(pre, post, 0, size-1, &preStart);

    printf("Inorder traversal of the constructed tree: \n");
    printInorder(root);
	return 0;
}
// Driver program to test above function
int main()
{
    /* Create following BST
              5
            /   \
           2     13  */
    node *root = newNode(5);
    root->left = newNode(2);
    root->right = newNode(13);

    printf(" Inorder traversal of the given tree\n");
    printInorder(root);

    addGreater(root);

    printf("\n Inorder traversal of the modified tree\n");
    printInorder(root);

    return 0;
}
// driver program to test indenticalTrees function
int main()
{
  char in[] = {'D', 'B', 'E', 'A', 'F', 'C'};
  char pre[] = {'A', 'B', 'D', 'E', 'C', 'F'};
  int len = sizeof(in)/sizeof(in[0]);
  struct node *root = buildTree(in, pre, 0, len - 1);
 
  /* Let us test the built tree by printing Insorder traversal */
  printf("\n Inorder traversal of the constructed tree is \n");
  printInorder(root);
  getchar();
}
示例#22
0
int main ()
{
    int pre[] = {10, 5, 1, 7, 40, 50};
    int size = sizeof( pre ) / sizeof( pre[0] );
 
    struct node *root = constructTree(pre, size);
 
    printf("   Inorder traversal of the constructed tree: \n");
    printInorder(root);
 
    return 0;
}
int main ()
{
    int pre[] = {1,7,5,40,10};
   // int size = sizeof( pre ) / sizeof( pre[0] );
 
    struct node *root = BuildTree(pre,0,4);
 
    printf("Inorder traversal of the constructed tree: \n");
    printInorder(root);
 
    return 0;
}
示例#24
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;
}
示例#25
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;

}
示例#26
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(){	
	node *root = newNode(10);
    root->left        = newNode(12);
    root->right       = newNode(15);
    root->left->left  = newNode(25);
    root->left->right = newNode(30);
    root->right->left = newNode(36);
 
    printf("\n\t\tInorder Tree Traversal\n\n");
   printInorder(root);
 
    struct node *head = BTToDLL(root);
 
    printf("\n\n\t\tDLL Traversal\n\n");
    printList(head);
}
int main(){
	LNode* head = NULL;
    push(&head, 36);  /* Last node of Linked List */
    push(&head, 30);
    push(&head, 25);
    push(&head, 15);
    push(&head, 12);
    push(&head, 10); /* First node of Linked List */

    node *root;
    queue<node*> q;
    convertList2Binary(head, &root, q);

    cout << "Inorder Traversal of the constructed Binary Tree is: \n";
    printInorder(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);
}
示例#30
0
文件: call.c 项目: syvjohan/CFun
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;
}