int isBst1(struct node *root) { if(root == NULL) return 1; if(root->data < root->left->data || root->data > root->right->data) return 0; isBst(root->left) && isBst(root->right); }
bool isBst(struct node * root,int min,int max ) { if(root == NULL) return true; if(root->data < min || root->data > max) return false; return (isBst(root->left,min,root->data) && isBst(root->right,root->data,max)); }
bool isBst(struct Node *root){ static struct Node *prev = NULL; if(root){ if(!isBst(root->left)){ return false; } if(prev && root->data <= prev->data){ printf("\nComparing root : %d and prev: %d",root->data,prev->data); return false; } prev = root; return isBst(root->right); } return true; }
int getLargestBst(struct node * root) { if(isBst(root,MIN,MAX)) return(size(root)); return max(getLargestBst(root->left),getLargestBst(root->right)); }
int isBst(tree* root, tree** predecessor){ if(!root) return 1; if(!root-> left && !root -> right){ if(*predecessor){ if((*predecessor) -> data > root -> data) return 0; } *predecessor = root; return 1; } if(isBst(root ->left, predecessor)){ if(*predecessor){ if ((*predecessor) -> data > root -> data){ return 0;} } *predecessor = root; return isBst(root -> right, predecessor); } return 0; }
int main() { int totalNodesInTree , inFixArr[20] , preFixArr[20] ; tree *root = NULL; tree* pred = NULL; int sum; printf("\nEnter length of arr : "); scanf("%d",&totalNodesInTree); printf("\nEnter values of nodes in preorder\n"); scanArr(preFixArr,totalNodesInTree); printArr(preFixArr,totalNodesInTree); printf("\nEnter values of nodes in inorder\n"); scanArr(inFixArr,totalNodesInTree); printArr(inFixArr,totalNodesInTree); printf("\n\nPost Fix Order as shown :\n"); root = toPostFix(inFixArr,preFixArr,0,totalNodesInTree-1,0); printf("\n"); printf("after creating tree :- \n"); printf("\nPrefix Notation\n"); prefix(root); printf("\nInfix Notation\n"); infix(root); printf("\nPostfix Notation\n"); postfix(root); printf("\n"); if(isBst(root, &pred)){ printf("Its BST \n"); } else{ printf("Its not BST\n"); } printf("Final root pred => %d\n",pred -> data); return 0; }
// driver program to test indenticalTrees function int main() { struct node * root = newNode(10); root->left = newNode(8); root->right = newNode(22); root->left->left = newNode(5); root->left->right = newNode(9); root->right->left = newNode(2); //root->right->right = newNode(2); int i = isBst(root); //printf("%d\t",i); if(i) printf("BT satisfies Childern sum property\n"); else printf("No Child sum property exist"); return 0; }
int main() { Node* root=NULL; //Min(root); //Max(root); insert(root,15); insert(root,13); insert(root,24); insert(root,24); insert(root,100); insert(root,240); Search(root,24); Search(root,15); Search(root,100); Search(root,240); std::cout<<"MINIMUM :"<<Min(root)<<std::endl; std::cout<<"MAXIMUM :"<<Max(root)<<std::endl; std::cout<<"HEIGHT :"<<Height(root)<<std::endl; levelOrder(root); std::cout<<std::endl; preOrder(root); std::cout<<std::endl; inOrder(root); std::cout<<std::endl; postOrder(root); std::cout<<std::endl; std::vector<int> v; isBst(root, v); for(const int& a:v) std::cout<<a<<" "; std::cout<<std::endl; return 5; }
int main() { TreeNode* root = NULL; if (isBst(root)) cout << "True" <<endl; else cout << "False" <<endl; root = new TreeNode(10); root->left = new TreeNode(8); root->right = new TreeNode(13); if (isBst(root)) cout << "True" <<endl; else cout << "False" <<endl; root->val = 10; root->left->val = 11; root->right->val = 13; if (isBst(root)) cout << "True" <<endl; else cout << "False" <<endl; root->val = 10; root->left->val = 9; root->right->val = 13; root->right->right= new TreeNode(11); if (isBst(root)) cout << "True" <<endl; else cout << "False" <<endl; root->val = 10; root->left->val = 0; root->right->val = 11; root->right->right->val = 12; if (isBst(root)) cout << "True" <<endl; else cout << "False" <<endl; root->val = 10; root->left->val = 9; root->right->val = 13; root->right->right->val = 14; root->right->left = new TreeNode(8); if (isBst(root)) cout << "True" <<endl; else cout << "False" <<endl; return 0; }
int main(){ struct Node *root = NULL; root = insert(root,10); insert(root,7); insert(root,16); insert(root,9); insert(root,8); insert(root,5); insert(root,22); printf("\nInorder : "); inorder(root); int key = 10; struct Node *keyNode = search(root,key); printf("\nSearch for %d : %d and parent: %d",key, keyNode == NULL ? -1 : keyNode->data); //printf("\nDeleting key %d : %d",key,delete_key(root,key)->data); printf("\nInorder : "); inorder(root); printf("\nIs tree BST : %d",isBst(root)); int key1 = 8; int key2 = 22; struct Node *lca = lowestCommonAncestor(root,key1,key2); printf("\nLowest Common Ancestor for %d and %d is : %d",key1,key2,lca == NULL ? -1 : lca->data); int a[] = {4, 2, 5, 1, 3}; int sa = sizeof(a)/sizeof(a[0]); printf("\nArray is printed in ascending order : "); printSorted(a,0,sa); int item = 5; struct Node* succ = inorder_successor(root,item); printf("\nInorder Successor for %d is : %d",item,succ == NULL ? -1 : succ->data); int k = 5; printf("\n%d smallest key in BST is %d",k,kth_smallest_bst(root,k)); { int key1 = 9,key2 = 22; printf("\nPrinting keys in range of %d and %d : ",key1,key2); printKeysInRange(root,key1,key2); } { int a[] = {1, 2, 3, 4, 5, 6, 7}; int sa = sizeof(a)/sizeof(a[0]); struct Node *root = sortedArrayToBalancedBst(a,0,sa-1); printf("\nInorder : "); inorder(root); } { Node* root = NULL; root = insert(root, 6); root = insert(root, -13); root = insert(root, 14); root = insert(root, -8); root = insert(root, 15); root = insert(root, 13); root = insert(root, 7); printf("\nInorder traversal of the given tree is: "); inorder(root); root = removeKeysOutsideRange(root, -10, 13); printf("\nInorder traversal of the modified tree is: "); inorder(root); } return 0; }