Ejemplo n.º 1
0
bool IsBST(Node* n, int min, int max)
{
	if (!n) return true;
	if (min < n->val && n->val < max)
	{
		return IsBST(n->left, min, n->val) && IsBST(n->right, n->val, max);
	}
	return false;
}
Ejemplo n.º 2
0
int IsBST(struct TNode* root){
if(!root){
return 1;
}

if(root->left && root->data< GetRight(root->left)->data){
return 0;
}

if(root->right && root->data > GetLeft(root->right)->data){
return 0;
}
return IsBST(root->left) && IsBST(root->right);
//return 1;
}
Ejemplo n.º 3
0
int main() {
	clBinarySearchTree aTree;
	clBinaryTree anotherTree;

	aTree.InsertElement(10);
	aTree.InsertElement(6);
	aTree.InsertElement(1);
	aTree.InsertElement(2);
	aTree.InsertElement(14);
	aTree.InsertElement(13);
	aTree.InsertElement(15);
	aTree.InsertElement(18);
	aTree.InsertElement(21);
	//aTree.TraversePreOrder();
	aTree.TraverseInOrder();
	//aTree.TraversePostOrder();
	aTree.DeleteElement(10);
	aTree.TraverseInOrder();

	/* Binary Tree with level order */
	anotherTree.InsertElement(1);
	anotherTree.InsertElement(12);
	anotherTree.InsertElement(14);
	anotherTree.InsertElement(2);
	anotherTree.InsertElement(90);
	anotherTree.InsertElement(76);
	anotherTree.InsertElement(4);
	anotherTree.InsertElement(0);
	anotherTree.InsertElement(8);
	anotherTree.TraverseLevelOrder();

	/* Balanced Check */
	clBinarySearchTree myTree;
	myTree.InsertElement(10);
	myTree.InsertElement(6);
	myTree.InsertElement(8);
	myTree.InsertElement(1);
	myTree.InsertElement(14);
	myTree.InsertElement(12);
	myTree.InsertElement(16);
	myTree.PrintBalanced();
	aTree.PrintBalanced();

	/* Binary Tree from Array */
	int array[] = {1,2,3,4,5,6,7,8,9,10};
	clBinarySearchTree mySweetTree(array, 0, 9);
	mySweetTree.TraverseInOrder();

	/* Lists from Binary Tree */
	cout << "Tree List" << endl;
	vector< list<treeNode_t *> > ourLists = mySweetTree.BinaryTreeToList();
	/* traverse first 2 lists */
	for (list<treeNode_t *>::iterator i = ourLists[0].begin(); i != ourLists[0].end(); i++) {
		cout << (*i)->data << endl;
	}

	/* Check if binary tree is BST or not */
	IsBST(mySweetTree.GetHeadPtr());
}
Ejemplo n.º 4
0
bool IsBST(bstNode* pRoot,int lbound , int ubound)
{
	if(pRoot==NULL)
	{
		return true;
	}
	bool b1 = true;
	if(pRoot->left)
	{
		if(pRoot->data < pRoot->left->data)
		b1 = false;
	}
	bool b2 = false;
	if(pRoot->data > lbound)
	{
		b2 = true;
	}
	bool b3 = false;
	if(pRoot->data < ubound)
	{
		b3 = true;
	}
	bool b4 = true;
	if(pRoot->right)
	{
		if(pRoot->data > pRoot->right->data)
		b4 = false;
	}
	return(b1 && b2 && b3 && b4 && IsBST(pRoot->left,lbound,pRoot->data) && IsBST(pRoot->right,pRoot->data,ubound));
	/*
	return (
			(pRoot->data > (pRoot->left ? pRoot->left->data : 0) )
			&& (pRoot->data > lbound)
			&& (pRoot->data < ubound)
			&& (pRoot->data < (pRoot->right ? pRoot->right->data : 1000) )
			&& IsBST(pRoot->left,lbound,pRoot->data)
			&& IsBST(pRoot->right,pRoot->data,ubound)
			)*/
}