Showing posts with label binary search tree. Show all posts
Showing posts with label binary search tree. Show all posts

Feb 26, 2009

check the binary tree is binary search tree

isBST2() Solution (C/C++)
/*
Returns true if the given tree is a binary search tree

*/
int isBST2(struct node* node) {
return(isBSTUtil(node, INT_MIN, INT_MAX));
}
/*
Returns true if the given tree is a BST and its
values are >= min and <= max.
*/
int isBSTUtil(struct node* node, int min, int max) {
if (node==NULL) return(true);

// false if this node violates the min/max constraint
if (node->datadata>max) return(false);

// otherwise check the subtrees recursively,
return
isBSTUtil(node->left, min, node->data) &&
isBSTUtil(node->right, node->data+1, max)
);
}

Jan 8, 2009

Thread search tree (binary)

Thread search tree (binary)

For inorder traverse no matter recursive or iterative method, there are a lot of functions call overheads. We can use thread tree method to thread the tree. Build the thread search tree when we insert the nodes into binary search tree with a dummy node trick.
see:
http://www.cs.rutgers.edu/~kaplan/503/thread.html

Jan 3, 2009

given a binary tree ,find the largest sub-tree which is a BST...(largest means subtree having largest no of nodes in it)...this is a wonderful questio

given a binary tree ,find the largest sub-tree which is a BST...(largest means subtree having largest no of nodes in it)

in order traverse the binary tree to keep the longest increasing substring