Binary Search Tree (5) and Binary Search Tree

Source: Internet
Author: User

Binary Search Tree (5) and Binary Search Tree
In the first article in this series, I have already introduced some properties of the Binary Search Tree:

  • Any node value in the left subtree of a node is smaller than the root node.
  • The value of any node in the right subtree of a node is greater than that of the root node.
  • The left and right subtree must be a binary search tree and duplicate nodes are not allowed.

Based on these properties, we naturally get this Judgment Method: each node in the tree has a specific value.

Assume that the node of the tree is defined:
Struct Node {int key; Node * left; Node * right ;};

Method 1 (simple but incorrect) checks whether the left child node is smaller than the node and the right child node is greater than the node for each node.
Bool isBST (Node * node) {if (node = NULL) return true; // if the left child is greater than the root node, It is not BST if (Node-> left! = NULL & node-> left-> key> node-> key) return false; // if the right child node is smaller than the root node, It is not BST if (node-> right! = NULL & node-> right-> key <node-> key) return false; // recursive judgment if (! IsBST (node-> left) |! IsBST (node-> right) return false; // after the detection is completed, BST return true if all conditions pass ;}
However, this method is incorrect. As shown in the following example, node 4 is in the left subtree of Root Node 3, but the function detects that this tree is BST.
3
/\
2 5
/\
1 4

Method 2 (the result is correct, but the efficiency is relatively low) for each node, check whether the maximum value in the left subtree is smaller than it, and the minimum value in the right subtree is greater than it.
// If it is BST, truebool isBST (Node * node) {if (node = NULL) return true; // if the maximum value of the Left subtree is greater than the root Node, returns false if (node-> left! = NULL & maxValue (node-> left)> node-> key) return false; // if the minimum value of the right subtree is smaller than the root node, returns false if (node-> right! = NULL & minValue (node-> right) <node-> key) return false; // recursively judges if (! IsBST (node-> left) |! IsBST (node-> right) return (false); // all detected values pass, which is BST return true ;}
The maxValue and minValue functions return the maximum and minimum values in a non-empty tree respectively.

Method 3 (correct and valid) method 2 is inefficient because part of the data in the tree needs to be traversed repeatedly. A better solution is to traverse each node only once. Method 3 cleverly limits the range of node values in the subtree so that each node only needs to be accessed once. The initial range of node values can be set to INT_MIN and INT_MAX.
// Determine whether it is BST
Bool isBST (Node * node)
{
Return (isBSTUtil (node, INT_MIN, INT_MAX ));
}
// If it is a binary search tree with a value range of [min, max], true is returned.
Bool isBSTUtil (Node * node, int min, int max)

Code implementation:
# Include <iostream> struct Node {int key; Node * left; Node * right;}; bool isBSTUtil (Node * node, int min, int max ); // determine whether it is BSTbool isBST (Node * node) {return (isBSTUtil (node, INT_MIN, INT_MAX);} // if it is a binary search tree, if the value range is [min, max], truebool isBSTUtil (Node * node, int min, int max) is returned {// The empty tree is also BST if (node = NULL) return true; // if the node value violates the maximum/minimum constraints, it is not BST if (node-> key <min | node-> key> max) return false; // recursively check the subtree return isBSTUtil (node-> left, min, node-> key-1) & isBSTUtil (node-> right, node-> key + 1, max);} // create a new BST Node * createNewNode (int item) {Node * temp = new Node; temp-> key = item; temp-> left = temp-> right = NULL; return temp;} int main () {/* tree1 Definition 4/\ 2 5/\ 1 3 */Node * root = createNewNode (4); root-> left = createNewNode (2 ); root-> right = createNewNode (5); root-> left = createNewNode (1); root-> left-> right = createNewNode (3 ); if (isBST (root) std: cout <"tree1 is BST \ n"; else std: cout <"tree1 is not BST \ n "; /* tree2 Definition 4/\ 2 5 // 1 3 */root = createNewNode (4); root-> left = createNewNode (2 ); root-> left = createNewNode (1); root-> right = createNewNode (5); root-> right-> left = createNewNode (3 ); if (isBST (root) std: cout <"tree2 is BST \ n"; else std: cout <"tree2 is not BST \ n "; return 0 ;}
Output:
Tree1 is BST
Tree2 is not BST

Time Complexity: O (n)
Auxiliary space: if the size of the function call stack is not considered, it is O (1); otherwise, it is O (n)

Method 4 (using the Middle-order traversal) 1) traverse the tree in the middle order and save the result in the temp array.
3) check whether the elements in the temp array are in ascending order. If yes, the tree is BST.
Time Complexity: O (n)
Method 4 can be further optimized. We can avoid using this additional array. In the middle-order time, you can save the front node. If the current node is smaller than the front node, this tree is not BST.
// Determine whether it is BSTbool isBST (Node * root) {static Node * prev = NULL; // traverse the tree in the middle order and save the front Node to the if (root) in the prev) {if (! IsBST (root-> left) return false; // check the legality of the node value if (prev! = NULL & root-> key <= prev-> key) return false; prev = root; // return isBST (root-> right) On the right subtree );} return true ;}

For more information, see:
Http://en.wikipedia.org/wiki/Binary_search_tree
Http://cslibrary.stanford.edu/110/BinaryTrees.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.