Пример #1
0
Файл: 333.cpp Проект: byc1234/oj
    int largestBSTSubtree(TreeNode* root) {

        int res = 0;
        int mini, maxi;
        bool b = isBST(root, res, mini, maxi);

        return res;
    }
Пример #2
0
int main() {
	TreeNode* root =new TreeNode(0);
	TreeNode* left = new TreeNode(-1);
	TreeNode* right = new TreeNode(2);
//	root->left = left;
//	root->right = right;
	if (isBST(root)) std::cout << "T" << std::endl;
	else std::cout << "F" << std::endl;
	return 0;
}
 bool isBST(TreeNode *node, int min, int max)
 {
     if(!node)
         return true;
     if(node->left && node->left->val >= max)
         return false;
     if(node->right && node->right->val <= min)
         return false;
     return node->val < max && node->val > min && isBST(node->left, min, node->val) && isBST(node->right, node->val, max);
 }
Пример #4
0
/* Returns true if a binary tree is a binary search tree */
int isBST(struct node* node)
{
    if (node == NULL)
        return(true);

    /* false if the max of the left is > than us */
    if (node->left!=NULL && maxValue(node->left) > node->data)
        return(false);

    /* false if the min of the right is <= than us */
    if (node->right!=NULL && minValue(node->right) < node->data)
        return(false);

    /* false if, recursively, the left or right is not a BST */
    if (!isBST(node->left) || !isBST(node->right))
        return(false);

    /* passing all that, it's a BST */
    return(true);
}
int main()
{
	Tree root;
	root=createNode(4,root);
	root->left=createNode(5,root->left);
	root->right=createNode(3,root->right);
	if(isBST(root,3)==-1)
	printf("No\n");
	else
	printf( "yes\n");
}
int isBST(struct node* n) {
if(n != NULL) {

  int value = n->data,checkl = 1,checkr = 1;
  if(n->left != NULL) {
    if(value > maxValue(n->left))
      checkl = isBST(n->left);
    else
      return 0;
  }
  if(n->right != NULL) {
    if(value < minValue(n->right))
      checkr = isBST(n->right);
    else
      return 0;
  }
if(checkl == 1 && checkr == 1)
  return 1;
}

}
Пример #7
0
/*tree * check(tree *root)
{
	if(root==NULL)
		return;
	if(root->left->data < root->data)
	{
		check(root->left);
	}
	else
	{
		printf("no");
		//break;
	}
	if(root->right->data > root->data)
	{
		check(root->right);
	}
	else
	{
		printf("no");
		//break;
	}
	return root;
}*/
bool isBST( tree* root)
{
    static tree *prev = NULL;
     
    // traverse the tree in inorder fashion and keep track of prev node
    if (root)
    {
        if (!isBST(root->left))
          return false;
 
        // Allows only distinct valued nodes
        if (prev != NULL && root->data <= prev->data)
          return false;
 
        prev = root;
 
        return isBST(root->right);
    }
 
    return true;
}
Пример #8
0
int main()
{
	tree *root;
	int i,j,n,ele;
	root=newnode(4);
	root->left=newnode(2);
	root->left->left=newnode(1);
	root->left->right=newnode(3);
	root->right=newnode(5);
	inorder(root);
	printf("%d",isBST(root));	
}	
Пример #9
0
int main()
{
    nodet *root=createNode(10);
    root=insert(root,4);
    root=insert(root,20);
    root=insert(root,40);
    root=insert(root,25);
    root=insert(root,1);
    prettyPrint(root,0);
    printf("\n %d",isBST(root));
    return 0;
}
void testIsBST() {

struct node* rootNode = insert(NULL,5);
rootNode = insert(rootNode,2);
rootNode = insert(rootNode,7);
rootNode = insert(rootNode,1);
//rootNode->left = insert(rootNode->left,6);
printTree(rootNode);
if(isBST(rootNode) == 1)
  printf("Given tree is a BST \n");
else
  printf("Given tree is not a BST \n");
}
Пример #11
0
int main()
{
  node *root = NULL, *rootb = NULL, *rooth = NULL, *temp;
  int sum = 25, *a, l = 0;
  a = (int *)malloc(sizeof(int));

  root = insert(root, 10);
  root = insert(root, 7);
  root = insert(root, 6);
  root = insert(root, 8);
  root = insert(root, 13);
  root = insert(root, 15);
  root = insert(root, 11);

  printf("Part2: Size: \t%d\n", size(root, 0));
  printf("Part3: Depth: \t%d\n", maxDepth(root));
  printf("Part4: Min: \t%d\n", minValue(root));
  printf("Part5: Post: \t");printPostorder(root);printf("\n");
  printf("Part6: Path Sum: %d %s\n", sum, hasPathSum(root, sum)?"True":"False");
  printf("Part7: Leaf Paths");printPaths(root, a, l);
  printf("\n");
  inorder(root);printf("\n");
  printf("Part8: Mirror: ");mirror(root);inorder(root);printf("\n");

  rootb = insert(rootb, 10);
  rootb = insert(rootb, 7);
  rootb = insert(rootb, 6);
  rootb = insert(rootb, 8);
  rootb = insert(rootb, 13);
  rootb = insert(rootb, 15);
  rootb = insert(rootb, 11);

  printf("Part9: doubleTree: ");doubleTree(rootb);inorder(rootb);printf("\n");
  printf("Part10: sameTree: %s\n", sameTree(root, root)?"True":"False");
  mirror(root);
  printf("Part11: isBST: %s\n", isBST(root)?"True":"False");
  printf("Part12: Tree list: ");
  rooth = treeList(root);
  temp = rooth -> left;
  printf("%d ", rooth -> data);
  while(temp != rooth)
    {
      printf("%d ", temp -> data);
      temp = temp -> left;
    }
  printf("\n");

  return(0);
}
Пример #12
0
/* Driver program to test above functions*/
int main()
{
    Node* root = newNode(3);
    root->left = newNode(2);
    root->right = newNode(5);
    root->left->left = newNode(1);
    root->left->right = newNode(4); 
     
    if(isBST(root))
        printf("Is BST\n");
    else
        printf("Not a BST\n");
         
    return 0;
} 
main() {
	struct node *root = NULL;
	int value;

	root = plain_binary_tree();
	printf("Initial tree :\n");
	print(root);

	value= isBST(root);	

	if (value == 1)
		printf("\nIs a Binary Search Tree\n");
	else
		printf("\nNot a Binary Search Tree\n");
}
Пример #14
0
/* Driver program to test above functions*/
int main()
{
  struct node *root = newNode(4);
  root->left        = newNode(2);
  root->right       = newNode(5);
  root->left->left  = newNode(1);
  root->left->right = newNode(3);

  if(isBST(root))
    printf("Is BST");
  else
    printf("Not a BST");

  getchar();
  return 0;
}
Пример #15
0
int main()
{
    struct node *root = newNode(5);
    
    root->left = newNode(3);
    root->right = newNode(7);
    root->left->left = newNode(3);
    root->left->right = newNode(4);
    
    if(isBST(root))
        printf("YES");
    else
        printf("NO");
    
    getchar();
    return 0;
}
Пример #16
0
int main(int argc,char** argv){
  BinTree_t* root=malloc(sizeof(BinTree_t));
  BinTree_t* l=malloc(sizeof(BinTree_t));
  BinTree_t* r=malloc(sizeof(BinTree_t));
  root->left=l;
  root->right=r;
  root->value=1;
  l->value=2;
  r->value=3;
  BinTree_t* ll=malloc(sizeof(BinTree_t));
  BinTree_t* lr=malloc(sizeof(BinTree_t));
  l->left=ll;
  l->right=lr;
  ll->value=4;
  lr->value=5;
  BinTree_t* rl=malloc(sizeof(BinTree_t));
  BinTree_t* rr=malloc(sizeof(BinTree_t));
  r->left=rl;
  r->right=rr;
  rl->value=6;
  rr->value=7;
  
  isBST(root);
  find(root,10);
  printf("-------inOrder-----");
  inOrderVisit(root,&print_visitor);
  printf("\n");
  printf("-------postOrder-----");
  postOrderVisit(root,&print_visitor);
  printf("\n");
  printf("-------preOrder-----");
  preOrderVisit(root,&print_visitor);
  printf("\n");
  printf("-------print Level-----");
  level_visit(root,&print_visitor);
  printf("\n");
}
Пример #17
0
int main(int argc, char* argv[])
{

#if DEBUG
	printf("\n>>==============Debug Value Set=================<<\n");
#endif
	char* file = argv[1];
	tree *newTree = readData(file);

#if DEBUG
	printf("\n>>==============Printing tree in InOrder=================<<\n");
#endif
	inOrder(newTree->root);
	printf("\n");
#if DEBUG
	printf("\n>>==============Printing tree in PreOrder=================<<\n");
#endif
	preOrder(newTree->root);
	printf("\n");
#if DEBUG
	printf("\n>>==============Printing tree in PostOrder=================<<\n");
#endif
	postOrder(newTree->root);
	printf("\n");	



#if DEBUG
	printf("\n>>==============Deleting with case 1: =================<<\n");
#endif	
	delete(newTree,51);
	
#if DEBUG
	printf("\n>>==============Printing after deletion, in InOrder=================<<\n");
#endif

#if DEBUG
	printf("\n>>==============Deleting with case 2: =================<<\n");
#endif	
	delete(newTree,52);
#if DEBUG
	printf("\n>>==============Printing after deletion, in InOrder=================<<\n");
#endif

#if DEBUG
	printf("\n>>==============Deleting with case 3: =================<<\n");
#endif	
	delete(newTree,10);
#if DEBUG
	printf("\n>>==============Printing after deletion, in InOrder=================<<\n");
#endif

	inOrder(newTree->root);
	printf("\n");

#if DEBUG
	printf("\n>>==============Searching for a value =================<<\n");
#endif
	bool ans = search(newTree->root,57);
	if(ans == 1)
		printf("Value found!\n" );
	else
		printf("Value not found!\n" );

	ans = search(newTree->root,99);
	if(ans == 1)
		printf("Value found!\n" );
	else
		printf("Value not found!\n" );

#if DEBUG
	printf("\n>>==============Height of tree =================<<\n");
#endif
	int ht = height(newTree->root);
	printf("%d \n",ht);

#if DEBUG
	printf("\n>>==============Is given tree a BST? =================<<\n");
#endif
	int ansbst = isBST(newTree->root,4,58);
	printf("%d \n",ansbst);

	//printf("%d\n", newTree->root->left->left->right->left->left->dataPtr->value);

	clearTree(newTree->root);
	free(newTree);
}
Пример #18
0
bool BinTree::isBST() const
{
	return isBST(root);
}
Пример #19
0
void main()
{
    struct node* node = buildTree(12);
    printlevelorder(node);
    printf("\n Is BST = %s\n", isBST(node, 500, -500) ? "Yes" : "No");
}
 bool isValidBST(TreeNode *root) {
     if(root == NULL)
         return true;
     isBST(root, INT_MIN, INT_MAX);
 }
Пример #21
0
bool isBST(BinaryTreeNode * pRoot)
{
    return isBST(pRoot, std::nemeric_limits<int>::max(), std::numeric_limits<int>::min());
}
 bool isValidBST(TreeNode *root) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     return isBST(root, INT_MIN, INT_MAX);
 }
Пример #23
0
bool BinarySearchTree<T>::isBST( int minData, int maxData){
    return isBST(root, minData, maxData);
}
Пример #24
0
 bool isValidBST(TreeNode* root) {
 	return isBST(root, INT_MIN, INT_MAX, true, true);
 }
Пример #25
0
int isBST(node *root, int min, int max){
    if (root==NULL) return 1;
    if(root->data<min || root->data>max)
        return 0;
    else return isBST(root->right, root->data, max) && isBST(root->left, min,root->data);
}
Пример #26
0
int main(int argc, char** argv)
{
  int i, n;
  struct TreeNode* bst = NULL;
  struct TreeNode* tree = makeTestTree(5,1);

  printf("test tree: ");
  printTree(tree);
  printf("tree leaves: ");
  printLeaves(tree);
  printf("tree depth = %d\n", maxDepth(tree));
  printf("tree balanced = %d\n", isBalanced(tree));
  printf("tree isBST = %d\n", isBST(tree));

  freeTree(tree);
  tree = NULL;

  tree = makeTestTree(6,2);

  printf("another test tree: ");
  printTree(tree);
  printf("tree leaves: ");
  printLeaves(tree);
  printf("tree depth = %d\n", maxDepth(tree));
  printf("tree balanced = %d\n", isBalanced(tree));
  printf("tree isBST = %d\n", isBST(tree));

  freeTree(tree);
  tree = NULL;

  tree = makeNotBST();

  printf("notBST: ");
  printTree(tree);
  printf("notBST leaves: ");
  printLeaves(tree);
  printf("notBST depth = %d\n", maxDepth(tree));
  printf("notBST balanced = %d\n", isBalanced(tree));
  printf("notBST isBST = %d\n", isBST(tree));

  printf("empty tree: ");
  printTree(bst);

  for(i = 0; i < 23; ++i)
  {
    n = (i*17+11) % 23;
    bst = insertBST(bst, n);
  }

  printf("filled BST: ");
  printTree(bst);
  printf("BST leaves: ");
  printLeaves(bst);
  printf("BST depth = %d\n", maxDepth(bst));
  printf("BST minimum value = %d\n", minValueBST(bst));
  printf("BST balanced = %d\n", isBalanced(bst));
  printf("BST isBST = %d\n", isBST(bst));

  for(i = -4; i < 25; i+=4)
  {
    n = removeBST(&bst, i);
    if(!n) printf("remove did not find %d\n", i);  
  }

  printf("BST after removes: ");
  printTree(bst);
  printf("BST leaves: ");
  printLeaves(bst);
  printf("BST depth = %d\n", maxDepth(bst));
  printf("BST minimum value = %d\n", minValueBST(bst));
  printf("BST balanced = %d\n", isBalanced(bst));
  printf("BST isBST = %d\n", isBST(bst));  

  freeTree(bst);
  bst = NULL;

  freeTree(tree);
  tree = NULL;

  return 0;
}
Пример #27
0
int main (int argc, const char * argv []) {
	
	printf("\n\tStart inserting into the tree...\n");
	TNODE_p_t tree = createNode();
	
	int choice;
	do {
		printf("\n---------------------------------------------------------------------");
		printf("\n\t0. Rebuild tree (recursive)\n\t1. Insert element (iterative)\n\t2. Insert element (recursive)\n\t3. Preorder transversal (recursive)\n\t4. Preorder transversal (iterative)\n\t5. Inorder transversal (recursive)\n\t6. Inorder transversal (iterative)\n\t7. Postorder transversal (recursive)\n\t8. Postorder transversal (iterative)\n\t9. Level order transversal (recursive)\n\t10. Search for an item (recursive)\n\t11. Check if it's BST.\n\t12. Check if it's a Mirror.\n\tEnter choice: ");
		scanf(" %d", &choice);
		
		String item = initString(SIZE);
		
		switch (choice) {
				
			case 0: tree = createNode();
				break;
				
			case 1: printf("\n\tEnter item to be inserted: ");
				scanf(" %s", item);
				insertIterative(&tree, item);
				break;
				
			case 2: printf("\n\tEnter item to be inserted: ");
				scanf(" %s", item);
				insertRecursive(&tree, item);
				break;
				
			case 3: printf("\n\tPreorder (Rec): ");
				preorderRecursive(tree);
				break;
				
			case 4: printf("\n\tPreorder (Iter): ");
				preorderIterative(tree);
				break;
				
			case 5: printf("\n\n\tInorder (Rec): ");
				inorderRecursive(tree);
				break;
				
			case 6: printf("\n\tInorder (Iter): ");
				inorderIterative(tree);
				break;
				
			case 7: printf("\n\n\tPostorder (Rec): ");
				postorderRecursive(tree);
				break;
				
			case 8: printf("\n\tPostorder (Iter): ");
				postorderIterative(tree);
				break;
				
			case 9: printf("\n\n\tLevel Order: ");
				levelOrder(tree);
				break;
				
			case 10: {
				printf("\tEnter item to be searched: ");
				scanf(" %s", item);
				TNODE_p_t loc = search(tree, item);
				if (loc != NULL)
					printf("\n\t'%s' is present in the tree. (%p)\n", item, loc);
				else
					printf("\n\t'%s' is not present in the tree.\n", item);
				break;
			}
				
			case 11: {
				if (isBST(tree)) {
					printf("\n\tTree is BST. Inorder: ");
					inorderRecursive(tree);
				}
				else {
					printf("\n\tTree is not a BST. Inorder: ");
					inorderRecursive(tree);
				}
				break;
			}
				
			case 12: {
				if (isMirror(tree)) {
					printf("\n\tTree is a mirror. Inorder: ");
					inorderRecursive(tree);
				}
				else {
					printf("\n\tTree is not a mirror. Inorder: ");
					inorderRecursive(tree);
				}
				break;
			}
				
			default: break;
				
		}
		
	} while (choice >= 0 && choice <= 12);
	
}