void printInOrder(node_t* root){
	if(root == NULL) return;
	printInOrder(root->left);
	printf("%d ", root->data);
	printInOrder(root->right);

}
int main() {
    struct node A, B, C, D, E;
    A.data = 128;
    B.data = 64;
    C.data = 256;
    D.data = 32;
    E.data = 48;

    A.left = &B;
    A.right = &C;
    B.left = &D;
    B.right = 0;
    C.left = 0;
    C.right = 0;
    D.left = 0;
    D.right = &E;
    E.left = 0;
    E.right = 0;

    struct node* cpyTree = deepcopy(&A);
    incTree(cpyTree);

    // Copy tree should be 1 larger than original in all node values
    printInOrder(&A);
    printf("\n");
    printInOrder(cpyTree);
}
Example #3
0
//
// Print values of tree rooted at node in ascending order
//
void printInOrder (struct Node* node) {
  if(node->left != NULL)
  printInOrder(node->left);
  printf ("%d\n", node->value);
  if(node->right != NULL)
  printInOrder(node->right);

}
/** Function to print a tree in the format: in order
 *	@param which_tree Pointer to the tree to be printed
 */
void printInOrder(Tnode* which_tree) {
	if (which_tree != NULL) {
		printInOrder(which_tree->left); //print left
		printf("%s\n", which_tree->data); //print the data
		printInOrder(which_tree->right); //print right child

	}
}
Example #5
0
void printInOrder(avlNode **node, int height){

    if(*node){
        printInOrder(&(*node)->left, height+1);
        printf("key: %d\tbalance: %d\theight: %d\n", (*node)->key, (*node)->balance, height);
        printInOrder(&(*node)->right, height+1);
    }
}
// Private printInOrder function.  It prints the value of the tree in InOrder.
void BinaryTree::printInOrder(TreeNode *p) const
{
	if(p)
	{
		printInOrder(p->left);
		cout << p->value << endl;
		printInOrder(p->right);
	}
}
Example #7
0
/*******************************
printPreOrder

This function prints the tree using a preOrder traversal
A sample call to this function is: printPreOrder(myTree, &printNode);

where printNode is the function to print a data item
*********************************/
void printPreOrder(Tree * root, void (* printNode) (TreeDataTypePtr data))
{
  Tree* node = root;
  if(node)
  {
    (*printNode)(node->content);
    printInOrder(node->left, printNode);
    printInOrder(node->right, printNode);
  }
}
Example #8
0
void BST<T>::printInOrder(BSTNode* t) const
{
	if (t == nullptr)
	{
		return;
	}
	printInOrder(t->left);
	cout << t->element << " ";
	printInOrder(t->right);
}
Example #9
0
void SyntaxTree::printInOrder(NimbleSyntax* theNode)
{
    if(theNode != 0)
    {
            printInOrder(theNode->getLeft());
            print(theNode);
            printInOrder(theNode->getRight());
            
    }
}
Example #10
0
//
// Print values of tree rooted at node in ascending order
//
void printInOrder (struct Node* node) {
    // TODO
    if (node->left != NULL) {
        printInOrder(node->left);
    }
    printf("%d \n",node->value);

    if (node->right != NULL) {
        printInOrder(node->right);
    }
}
Example #11
0
/**
 * [printPostorder description]
 * @param node [description]
 */
void printInOrder(struct node* node)
{
  if(node == NULL)
  {
    return;
  }
  else
  {
    printInOrder(node->left);
    printf("[%d]",node->data);
    printInOrder(node->right);
  }
}
/*Prints the list of nodes in the tree in order*/
void printInOrder(BinTree * theTree, BinNode * nodeToPrint) {
    if (nodeToPrint->leftNode != NULL) {
        printInOrder(theTree, nodeToPrint->leftNode);
    }

    if (nodeToPrint !=NULL) {
        theTree->printFunction(nodeToPrint->binVPtr);
        printf("\n");
    }
    
    if (nodeToPrint->rightNode != NULL) {
        printInOrder(theTree, nodeToPrint->rightNode);
    }
}
Example #13
0
void printInOrder(struct treeNode * node, int height)
{
    if(node==0) return ;

    //print left sub-tree
    printInOrder(node->left, height-1);

    //print item
    for(int i=0;i<height;i++)printf("   ");
    printf("%03d\n",node->item);

    //print right sub-tree
    printInOrder(node->right, height-1);
}
Example #14
0
void printInOrder(struct node* head, int layer)
{
  int x;
  if(head->lchild)
  {
    printInOrder(head->lchild, layer+1);
  }
  for(x = 0; x < layer; x++)
    printf("\t");
  printNode(head);
  if(head->rchild)
  {
    printInOrder(head->rchild, layer+1);
  }
}
int main()
{
    struct Node* root = NULL;
    root = insert(root, 12);
    root = insert(root, 10);
    root = insert(root, 13);
    root = insert(root, 7);
    root = insert(root, 5);
    root = insert(root, 15);
    printInOrder(root);
    root = removeHalfNodes(root);
    printf("\nAfter Removal of Half Nodes:\n");
    printInOrder(root);
    return 0;
}
int main(void)
{
    initializeTree();
    while(1)
    {
        printf("1. Insert item. 2. Delete item. 3. Search item. \n");
        printf("4. Print height of tree. 5. Print height of an item. \n");
        printf("6. PrintInOrder. 7. Range Search.\n");

        int ch;
        scanf("%d",&ch);
        if(ch==1)
        {
            int item;
            scanf("%d", &item);
            insertItem(root, item);
        }
        else if(ch==2)
        {
            int item;
            scanf("%d", &item);
            deleteItem(root, item);
        }
        else if(ch==3)
        {
            int item;
            scanf("%d", &item);
            struct treeNode * res = searchItem(root, item);
            if(res!=0) printf("Found.\n");
            else printf("Not found.\n");
        }
        else if(ch==4)
        {
            int height = calcNodeHeight(root);
            printf("Height of tree = %d\n", height);
        }
        else if(ch==5)
        {
            int item;
            scanf("%d", &item);
            int height = calcHeight(item);
            printf("Height of %d = %d\n", item, height);
        }
        else if(ch==6)
        {
            int h = calcNodeHeight(root);
            printf("\n--------------------------------\n");
            printInOrder(root, h);
            printf("--------------------------------\n");
        }
        else if(ch==7)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%d\n",rangeSearch(root,l,r));
        }
    }

}
/**	Function to print a tree in different formats: in order, pre order, post order
 *	@param which_tree Pointer to the tree to be pointed
 */
void print_tree(Tnode *which_tree) {
	printf("Print in order\n");
	printInOrder(which_tree); //print in order
	printf("Print pre order\n");
	printPreOrder(which_tree); //print pre order
	printf("Print post order\n");
	printPostOrder(which_tree); //print post order
}
Example #18
0
void exploitMemLeaks() {	

	// All data allocated and tracked, all will be freed
	int * low = calloc(1, sizeof(int));
	* low = 1;
	
	int * mid = calloc(1, sizeof(int));
	* mid = 2;
	
	int * hi = calloc(1, sizeof(int));
	* hi = 3;
	
	printf("Created the following data to add to a tree:\n");
	printf("Low: %d, Mid: %d, Hi: %d\n", * low, * mid, * hi);
	
	// Test inside a tree
	Tree * test = createBinTree(&compNum, &destructor);
	
	addToTree(test, mid);
	addToTree(test, low);
	addToTree(test, hi);
	
	printInOrder(test, &print);
	// Up to here, as long as destroyBinTree is called, no leaks are possible
	// Now lets look at some of the issues with the library
	
	// Left will contain Low (1), we know this. This function allocates memory
	// so it's return value must be freed
	Tree * left = getLeftSubtree(test);
	free(left);
	// All is well, no memory leaks here

	// Now let's get the right subtree, Hi (3), but before we free it, let's get
	// get one of it's subtrees too. We know it won't contain anything so
	// presumably we'll get some sort of error return value
	Tree * right = getRightSubtree(test);	

	Tree * empty = getRightSubtree(right);
	if (empty == NULL)
		printf("The call to an empty subtree returned NULL.\n");
		// Oh good, we did get some error handling, great now there's no problem

	// OOPS, no, there's a huge issue. The getSubtree functions are presumably 
	// doing the following: allocating memory to a temporary new tree pointer, 
	// checking what's in the subtree and returning NULL if nothing is in it.
	// So the temporary new tree pointer to allocated memory is completely lost
	// in all instances.
	
	// We can't even detect the error without a memory checker because the
	// following call of free(NULL) has no effect
	free(empty);
	
	free(right);
	
	destroyBinTree(test);	
}
Example #19
0
void main(){
	printf("\n");
	queue qu;
	queue *q = &qu;
	initQueue(4,q);
	printQueue(q);
	push(1,q);
	push(2,q);
	push(3,q);
	printQueue(q);
	push(4,q);
	printInOrder(q);

	push(5,q);
	push(6,q);
	push(7,q);
	push(8,q);
	printQueue(q);
	printInOrder(q);

}
Example #20
0
void testBST() {
    int array[ARRAY_LEN];
    randomize(array, ARRAY_LEN, ARRAY_MAX);
    int i;

    bstNode* head = NULL;
    for (i=0; i < ARRAY_LEN; i++) {
        insertBSTNode(&head, array[i]);
    }

    printInOrder(head);
}
Example #21
0
int main()
{
  TreeNode *root=NULL;
  createBinaryTree(&root);
  printf("\n\n");
  printPreOrder(root);
  printf("\n");
  printInOrder(root);
  printf("\n");
  printPostOrder(root);
  printf("\n");
  return 0;
}
Example #22
0
//
// Create root node, insert some values, and print tree in order
//
int main (int argc, char* argv[]) {
    // TODO
    struct Node* root = newNode(100);
    insert(root,10);
    insert(root,120);
    insert(root,130);
    insert(root,90);
    insert(root,5);
    insert(root,95);
    insert(root,121);
    insert(root,131);
    insert(root,1);
    printInOrder(root);
}
Example #23
0
//
// Create root node, insert some values, and print tree in order
//
int main (void) {
  struct Node root = {100, NULL, NULL};
  insert(&root, 10);
  insert(&root, 120);
  insert(&root, 130);
  insert(&root, 90);
  insert(&root, 5);
  insert(&root, 95);
  insert(&root, 121);
  insert(&root, 131);
  insert(&root, 1);
  printInOrder(&root);
  return 0;
}
Example #24
0
//
// Create root node, insert some values, and print tree in order
//
int main (int argc, char* argv[]) {
    struct Node* n = newNode(100);
    insert(n,(10));
    insert(n,(120));
    insert(n,(130));
    insert(n,(90));
    insert(n,(5));
    insert(n,(95));
    insert(n,(121));
    insert(n,(131));
    insert(n,(1));
    printInOrder(n);


}
Example #25
0
void p1(void)
{
    struct Node * r=NULL;
    struct Data d;
    d.data=10;
    r=insertBST(r,d);
    d.data=1;
    r=insertBST(r,d);
    d.data=15;
    r=insertBST(r,d);
    d.data=9;
    r=insertBST(r,d);
    printInOrder(r);
    deleteTree(r);
}
Example #26
0
int main()
{
  int internalNodes ;
  TreeNode *root=NULL;
  createBinaryTree(&root);
  printf("\n\n");
  printPreOrder(root);
  printf("\n");
  printInOrder(root);
  printf("\n");
  printPostOrder(root);
  printf("\n");
  internalNodes = countInternalNodes(root);
  printf("Internal nodes =%d\n",internalNodes);
  return 0;
}
void btree::printInOrder(bnode* cnode)
{
    if (cnode)
    {
        cout << cnode->data << " ";
        if (cnode->childvec.size() > 0)
        {
            for (int i = 0; i < cnode->childvec.size(); i++)
            {
                printInOrder(cnode->childvec[i]);
            }
        }
    }

    return;

}
Example #28
0
int main()
{
	/*
	 * Create a pointer that will be the root of the tree.
	 */
	bTreeNode *rootPtr = NULL;
	/*
	 * Insert a bunch of numbers for testing purposes.
	 * The tree constructed should look like:
	 *
	 *           5
	 *         /   \
	 *		  3	    7
	 *       / \   / \
	 *      1   4 6   9
	 */
	rootPtr = insert(rootPtr, 5);
	rootPtr = insert(rootPtr, 3);
	rootPtr = insert(rootPtr, 7);
	rootPtr = insert(rootPtr, 1);
	rootPtr = insert(rootPtr, 4);
	rootPtr = insert(rootPtr, 6);
	rootPtr = insert(rootPtr, 9);

	/*
	 * Traversing the tree in Preorder
	 */
	printf("\nPreOrder\n");
	printPreOrder(rootPtr);

	/*
	* Traversing the tree in Inorder
	*/
	printf("\nInOrder\n");
	printInOrder(rootPtr);

	/*
	* Traversing the tree in Postorder
	*/
	printf("\nPostOrder\n");
	printPostOrder(rootPtr);

	return 0;
}
Example #29
0
int main(int argc, char** argv)
{
  struct node* head;
  int x;

  srand(time(NULL));

  head = insert(NULL, rand() % 10000);

  for(x = 0; x < 10; x++)
  {
    insert(head, rand() % 10000);
  }

  printInOrder(head, 0);

  freeTree(head);

  return 0;
}
Example #30
0
void binTree::printTree(void)
{
	printInOrder(this->getMainLeaf());
}