Exemplo n.º 1
0
/** Function to print a tree in the format: pre order
 *	@param which_tree Pointer to the tree to be printed
 */
void printPreOrder(Tnode* which_tree) {
	if (which_tree != NULL) {
		printf("%s\n", which_tree->data); //print data
		printPreOrder(which_tree->left); //print left child
		printPreOrder(which_tree->right); //print right child
	}
}
Exemplo n.º 2
0
void printPreOrder(tree_node *root){
	if(root == NULL) return;
	printf("%d\t", root->data);
	printPreOrder(root->left);
	printPreOrder(root->right);
	return;
}
Exemplo n.º 3
0
 /**
  * Prints all nodes of a tree or subtree according
  * to the following order:
  * 1º - Parent node;
  * 2º - Left child;
  * 3º - Right child;
  * @param node
  */
 void printPreOrder(Node *node) {
     if (node != NULL) {
         cout << node->info  << "\t" << node->height << "\t" << node->balance << endl;
         printPreOrder(node->left);
         printPreOrder(node->right);
     }
 }
Exemplo n.º 4
0
void printPreOrder(avlNode **node, int height){

    if(*node){
        printf("key: %d\tbalance: %d\theight: %d\n", (*node)->key, (*node)->balance, height);
        printPreOrder(&(*node)->left, height+1);
        printPreOrder(&(*node)->right, height+1);
    }
}
void printPreOrder(struct TreeNode *T)
{
    if (T != NULL) {
        printf("%d ", T->value);
        printPreOrder(T->lchild);       
        printPreOrder(T->rchild);
    }
}
// Private printPreOrder function.  It prints the value of the tree in PreOrder.
void BinaryTree::printPreOrder(TreeNode *p) const
{
	if(p)
	{
		cout << p->value << endl;
		printPreOrder(p->left);
		printPreOrder(p->right);
	}
}
Exemplo n.º 7
0
void printPreOrder(struct BT *root)
{
	if(root)
	{
		
		printf("[%c]\t",root->data);
		printPreOrder(root->l);
		printPreOrder(root->r);
		
	}
	
}
Exemplo n.º 8
0
/**
 * [printPostorder description]
 * @param node [description]
 */
void printPreOrder(struct node* node)
{
  if(node == NULL)
  {
    return;
  }
  else
  {
    printf("[%d]",node->data);
    printPreOrder(node->left);
    printPreOrder(node->right);
  }
}
Exemplo n.º 9
0
/**	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
}
Exemplo n.º 10
0
int main()
	{
		char pre[] = {'I', 'L', 'I', 'L', 'L'};
		int index = 0;
		printf("fdfdf\n");
		struct BT *root =buildTree(pre,&index, sizeof(pre)/sizeof(pre[0]));
		printf("fdfdf\n");
		printPreOrder(root);
	
	return 0;
	}
Exemplo n.º 11
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;
}
Exemplo n.º 12
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;
}
Exemplo n.º 13
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;
}
Exemplo n.º 14
0
int main(int argc, char **argv){
	srand(time(NULL));
	tree_node *root = (tree_node *)malloc(sizeof(tree_node));
	root->data = 0;//rand()%10;
	makeTree(root);
	printf("PreOrder:");
	printPreOrder(root);
	printf("\n");

	printf("Inorder traversal: ");
	inorder_traversal(root);
	printf("\n");
/*	printf("Max in tree = %d\n",find_max(root));
	printf("Reverse Level Order :");
	printReverseLevelOrder(root);
	printf("Height of tree = %d\n",height_of_tree(root));
*/
	tree_node *root1 = root;
	tree_node *root2 = (tree_node *)malloc(sizeof(tree_node));
	root2->left = root2->right = NULL;
	root2->data = 0;
	makeTree(root2);
	printf("Are they structurally identical = %d\n",isStructurallyIdentical(root1, root2));

	tree_node *root3 = (tree_node *)malloc(sizeof(tree_node));
	root3->left = root3->right = NULL;
	MakeAMirror(root1, root3);

	printf("Inorder traversal of Mirrored Tree :");
	inorder_traversal(root3);
	printf("\n");

	printf("Diameter of tree = %d\n", diameterOfTree(root1));
	printAllPathsToLeaf(root1);

	DeleteTree(root1);
	DeleteTree(root2);
	DeleteTree(root3);

	return 0;
}
int main(int argc, char**argv) {
    int n, i;
    int array[110];
    while (scanf("%d", &n) != EOF) {
        for (i = 0; i < n; i++) {
            scanf("%d", &array[i]);
        }
        struct TreeNode *T = NULL;
        for (i = 0; i < n; i++) {
            T = insertTreeNode(T, array[i]);
        }
        printPreOrder(T);
        printf("\n");
        printMidOrder(T);
        printf("\n");
        printPostOrder(T);
        printf("\n");
    }
    
    return 0;
}
// Public printPreOrder function
void BinaryTree::printPreOrder() const
{
	printPreOrder(root);
}