Example #1
0
void BST<T>::inOrderTraversal(Node<T>* root) {
  if(root != 0) {
    inOrderTraversal(root->getLeftChild());
    std::cout << root->getValue() << std::endl;
    inOrderTraversal(root->getRightChild());
  }
}
Example #2
0
void AVL<T>::inOrderTraversal(Node<T>* curr) {
  if (curr != 0) {
    inOrderTraversal(curr->getLeftChild());
    std::cout << curr->getValue() << ", " << curr->getBalance() << std::endl;
    inOrderTraversal(curr->getRightChild());
  }
}
Example #3
0
void inOrderTraversal(struct TreeNode *root) {
  if(root != NULL) {
    inOrderTraversal(root->left);
    printf(" %d ", root->data);
    inOrderTraversal(root->right);
  }
}
Example #4
0
 void inOrderTraversal(BinaryTreeNode* node) {
     if (node != nullptr) {
         inOrderTraversal(node->left);
         //visit(node);
         inOrderTraversal(node->right);
     }
 }
/// Function to Traverse the Binary tree using In-Order Traversal Technique 
void inOrderTraversal(struct bt_node *node)
{
	if (node != NULL)
	{
		inOrderTraversal(node->lchild);
		printf("%d\n",node->data);
		inOrderTraversal(node->rchild);
	}
} 
Example #6
0
 void inOrderTraversal(TreeNode* root)
 {
     if (root==NULL)
     {
         return ;
     }
     inOrderTraversal(root->left);
     v.push_back(root->val);
     inOrderTraversal(root->right);
 }
Example #7
0
void inOrderTraversal( avlTreeNode* root )
{
  if( root )
    {
      inOrderTraversal( root -> leftTree );
      printf("%d ",root -> data );
      inOrderTraversal( root -> rightTree );
    }
  
}
Example #8
0
	void Tree::inOrderTraversal(TreeNode* curNode, LinkedList& list)
	{
		if (curNode == NULL)
		{
			return;
		}
		inOrderTraversal(curNode->left(), list);
		list.addNode(curNode->item());
		inOrderTraversal(curNode->right(), list);
	}
TreeNode * inOrderTraversal(TreeNode * N, TreeNode * next, int * doNext) {
    TreeNode * cur;

    if (N->left != NULL) {
        cur = inOrderTraversal(N->left,next,doNext);
        if (cur != NULL)
            return cur;
    }
    if (*doNext == 1) {
        return N;
    }
    if (N == next) {
        *doNext = 1;
    }
    if (N->right != NULL) {
        cur = inOrderTraversal(N->right,next,doNext);
        if (cur != NULL)
            return cur;
    }
    return NULL;
}
Example #10
0
void printTree(avlTreeNode* root)
{
  printf("\n\n**********************************\n\n");

  printf("\nInOrder Traversal\n");
  inOrderTraversal(root);

  printf("\nPreOrder Traversal\n");
  preOrderTraversal(root);

  printf("\nPostOrder Traversal\n");
  postOrderTraversal(root);
  printf("\t\t[%d]\n",root->height);
}
Example #11
0
 bool isValidBST(TreeNode* root) {
     if (root==NULL)
     {
         return true;
     }
     
     inOrderTraversal(root);
     for (int i=1;i<v.size();i++)
     {
         if (v[i]<=v[i-1])
         {
             return false;
         }
     }
     
     return true;
 }
int Successor (Tree *T, void *I) {
    int doNext = 0;
    if (T->root == NULL || T->current == NULL) {
        /* tree was empty or minimum/successor was not previously called */
        return 0;
    }

    T->current = inOrderTraversal(T->root,T->current,&doNext);

    if (T->current == NULL) {
        return 0;
    }

    I = T->copyValue(I,T->current->value);

    return 1;
}
Example #13
0
int main() 
{
	Node *root = newNode(20); 
	root->left = newNode(8);
	root->left->left = newNode(4);
	root->left->right = newNode(12);
	root->left->right->left = newNode(10);
	root->left->right->right = newNode(14);
	root->right = newNode(22);

	int height = treeHeight(root);
	printf("Tree Height : %d\n", height);

	printf("preOrderTraversal :\n");
	preOrderTraversal(root);
	printf("\n");

	printf("inOrderTraversal :\n");
	inOrderTraversal(root);
	printf("\n");

	printf("postOrderTraversal :\n");
	postOrderTraversal(root);
	printf("\n");

	Node *anNode = findLowestCommonAncestor(root, 4, 14);
	printf("findLowestCommonAncestor 4 and 14 is : %d\n", anNode->data);

	int count = traverse(root, 0, NULL);
	printf("traverse count = %d\n", count);

	Node *newRoot = heapifyBinaryTree(root);
		

	return 0;
}
int main ()
{
	binaryTreeInsert(4);
	binaryTreeInsert(2);
	binaryTreeInsert(7);
	binaryTreeInsert(10);
	binaryTreeInsert(5);
	binaryTreeInsert(0);
	binaryTreeInsert(3);
	binaryTreeInsert(-5);
	binaryTreeInsert(1);
	
	printf(" Preorder Traversal :\n");
	preOrderTraversal(root);
	
	printf("\n Inorder Traversal:\n");
	inOrderTraversal(root);
	
	printf("\n Postorder Traversal:\n");
	postOrderTraversal(root);
	
	printf("\n Breadth Traversal:\n");
	breadthTraversal(root);
}
Example #15
0
void BST<T>::print() {
  inOrderTraversal(root);
}
Example #16
0
int testBinarySearchTree(void) {
	Tree *head = NULL;
	Tree *singleNodeTree = malloc(sizeof(Tree));
	Tree *test[2];	
	if(singleNodeTree == NULL) {
		exit(EXIT_FAILURE);
	}
	singleNodeTree->value = 100;
	singleNodeTree->left = NULL;
	singleNodeTree->right = NULL;

	printf("Start Test of Binary Search Tree.\n");
	printf("Initial contents of the tree:\n");
	print_ascii_tree(head);

	printf("\nadd() 5 to the tree\n");
	head = addToTree(head, 5);
	print_ascii_tree(head);
	printf("\nadd() 2 to the tree\n");
	head = addToTree(head, 2);
	print_ascii_tree(head);
	printf("\nadd() 12 to the tree\n");	
	head = addToTree(head, 12);
	print_ascii_tree(head);
	printf("\nadd() 1 to the tree\n");	
	head = addToTree(head, 1);
	print_ascii_tree(head);
	printf("\nadd() 8 to the tree\n");	
	head = addToTree(head, 8);
	print_ascii_tree(head);
	printf("\nadd() 4 to the tree\n");	
	head = addToTree(head, 4);
	print_ascii_tree(head);
	printf("\nadd() 3 to the tree\n");	
	head = addToTree(head, 3);
	print_ascii_tree(head);
	printf("\nadd() 10 to the tree\n");	
	head = addToTree(head, 10);
	print_ascii_tree(head);
	printf("\nadd() 9 to the tree\n");	
	head = addToTree(head, 9);
	print_ascii_tree(head);	
	printf("\nadd() 11 to the tree\n");	
	head = addToTree(head, 11);
	print_ascii_tree(head);	
	printf("\nadd() 7 to the tree\n");	
	head = addToTree(head, 7);
	print_ascii_tree(head);		

	if( (test[0] = findInTree(head, 3)) == NULL) {
		printf("\nfind(3) = Value Not Found\n");
	} else {
		printf("\nfind(3) = %d\n", test[0]->value);
	}

	if( (test[0] = findInTree(head, 7)) == NULL) {
		printf("\nfind(7) = Value Not Found\n");
	} else {
		printf("\nfind(7) = %d\n", test[0]->value);
	}

	if( (test[0] = findInTree(head, 4)) == NULL) {
		printf("\nfind(4) = Value Not Found\n");
	} else {
		printf("\nfind(4) = %d\n", test[0]->value);
	}

	findParentInTree(head, head, 3, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(3) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	findParentInTree(head, head, 5, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(5) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	findParentInTree(head, head, 2, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(2) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	findParentInTree(head, head, 9, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(9) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	printf("Depth of the tree = %d\n", getTreeDepth(head, 0));
	inOrderTraversal(head);
	preOrderTraversal(head);
	postOrderTraversal(head);	
	breadthFirstSearch(head);		
	/* Delete Order: 1, 3, 12, 8, 5 */
	printf("\nbefore removing any items\n");
	print_ascii_tree(head);		
	printf("\nremove() 1 from the tree\n");	
	head = removeFromTree(head, 1);
	print_ascii_tree(head);		
	printf("\nremove() 3 from the tree\n");	
	head = removeFromTree(head, 3);
	print_ascii_tree(head);		
	printf("\nremove() 12 from the tree\n");	
	head = removeFromTree(head, 12);
	print_ascii_tree(head);		
	printf("\nremove() 8 from the tree\n");	
	head = removeFromTree(head, 8);
	print_ascii_tree(head);		
	printf("\nremove() 5 from the tree\n");	
	head = removeFromTree(head, 5);
	print_ascii_tree(head);			
	printf("\nremove() 11 from the tree\n");	
	head = removeFromTree(head, 11);
	print_ascii_tree(head);				
	printf("Depth of the tree = %d\n", getTreeDepth(head, 0));
	inOrderTraversal(head);
	breadthFirstSearch(head);		

	printf("\nsinglNodeTests start here: \n");
	print_ascii_tree(singleNodeTree);
	inOrderTraversal(singleNodeTree);

	printf("\n remove() 1 from tree\n");
	if(removeFromTree(singleNodeTree, 1) == NULL) {
		printf("Node Not Found, nothing removed\n");
	}
	printf("Depth of the singleNodeTree = %d\n", getTreeDepth(singleNodeTree, 0));	

	printf("\n remove() 100 from tree\n");
	singleNodeTree = removeFromTree(singleNodeTree, 100);
	printf("Depth of the singleNodeTree = %d\n", getTreeDepth(singleNodeTree, 0));

	return EXIT_SUCCESS;
}
Example #17
0
int main() {

  // create the tree
  struct TreeNode *root = NULL;
  root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  root->data = 1;

  root->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  (root->left)->data = 2;
  (root->left)->right = NULL;

  (root->left)->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  ((root->left)->left)->data = 4;
  ((root->left)->left)->left = NULL;
  ((root->left)->left)->right = NULL;

  (root->left)->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  ((root->left)->right)->data = 5;
  ((root->left)->right)->left = NULL;
  ((root->left)->right)->right = NULL;

  (root->right) = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  (root->right)->data = 3;
  (root->right)->right = NULL;

  (root->right)->left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  ((root->right)->left)->data = 6;
  ((root->right)->left)->left = NULL;
  ((root->right)->left)->right = NULL;

  (root->right)->right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
  ((root->right)->right)->data = 7;
  ((root->right)->right)->left = NULL;
  ((root->right)->right)->right = NULL;


  printf("     1   \n");
  printf("   /   \\  \n");
  printf("  2     3   \n");
  printf(" /  \\  / \\ \n");
  printf("4   5  6  7 \n");

  printf("\nPreOrder Traversal\n");
  preOrderTraversal(root);
  printf("\n");

  printf("InOrder Traversal\n");
  inOrderTraversal(root);
  printf("\n");

  printf("PostOrder Traversal\n");
  postOrderTraversal(root);
  printf("\n");

  printf("LevelOrder Traversal\n");
  struct TreeNode *temp = NULL;
  struct Queue *queue = createQueue();
  enqueue(queue, root);

  while(queue->front != NULL) {
    temp = dequeue(queue);
    printf(" %d ", temp->data);

    if(temp->left != NULL) {
      enqueue(queue, temp->left);
    }

    if(temp->right != NULL) {
      enqueue(queue, temp->right);
    }
  }

  printf("\n");
  destroyQueue(&queue);

  return 0;
}
Example #18
0
void inOrderTraversal(Node *root) {
	if(root == NULL) return;
	inOrderTraversal(root->left);
	printf("%d\t", root->data);
	inOrderTraversal(root->right);
}
Example #19
0
int main() {
    printf("Start\n");

    printf("Initializing stack\n");
    Stack* stack = (Stack *) malloc(sizeof (Stack));
    initStack(stack);

    int i;
    for (i = 0; i < 10; i++) {
        pushStack(stack, initIntElem(i));
        int *k = inspectStack(stack);
        printf("Pushed %d\n", *k);
    }

    while (!isEmptyStack(stack)) {
        int *k = popStack(stack);
        printf("Popped %d\n", *k);
        free(k);
    }


    printf("Freeing stack\n");
    clearStack(stack);
    free(stack);


    printf("Initializing queue\n");
    Queue *queue = (Queue *) malloc(sizeof (Queue));
    initQueue(queue);

    for (i = 0; i < 10; i++) {
        enqueue(queue, initIntElem(i));
        int *k = inspectLastQueue(queue);
        printf("Enqueued %d\n", *k);
    }

    while (!isEmptyQueue(queue)) {
        int *k = dequeue(queue);
        printf("Dequeued %d\n", *k);
        free(k);
    }

    printf("Freeing queue\n");
    clearQueue(queue);
    free(queue);

    printf("End\n");

    BinaryTree *a = (BinaryTree*)malloc(sizeof(BinaryTree));
    a->root = NULL;
    insertBinaryTree(a,initIntElem(10));
    insertBinaryTree(a,initIntElem(5));
    insertBinaryTree(a,initIntElem(12));
    insertBinaryTree(a,initIntElem(7));
    insertBinaryTree(a,initIntElem(8));
    insertBinaryTree(a,initIntElem(19));
    
    preOrderTraversal(a);
    printf("\n");
    postOrderTraversal(a);
    printf("\n");
    inOrderTraversal(a);
    printf("\n");
    breadthFirstTraversal(a);
    return 0;
}