Determine whether a binary tree is a binary search tree

Source: Internet
Author: User
The problem is that if a binary tree is given, is it a binary search tree )? Solution 1: brute-force search first describes the differences between a binary tree and a binary search tree. A binary tree refers to a tree with a maximum of two children at each node. A Binary Search Tree is a binary tree, but it has some additional constraints, these constraints must be true for each node:
  • The values of all nodes in the left subtree of node are smaller than those of node.
  • The value of all nodes in the right subtree of a node is greater than that of a node.
  • The left and right subtree of the node must also be a binary search tree.
This question may be frequently asked during the interview to evaluate the understanding of the Binary Search Tree definition. At first glance, you may want to achieve this: assume that the current node value is K. For each node in a binary tree, determine whether the value of the left child is smaller than K, and whether the value of the right child is greater than K. If all nodes meet this condition, the binary tree is a binary search tree. Unfortunately, this algorithm is incorrect. Consider the following binary tree. It meets the conditions of the above algorithm, but it is not a binary search tree.

    10   /  \  5   15     -------- binary tree (1)     /  \    6    20

Therefore, according to the definition of the binary search tree, we can think of a brute force search method to determine whether a binary tree is a binary search tree.

Assume that the current node value is K. For each node in a binary tree, the value of all nodes in the left subtree must be smaller than K, and the value of all nodes in the right subtree must be greater than K.

The brute-force search algorithm code is as follows. Although the efficiency is not high, it does work. The worst case of this solution is O (n ^ 2), and N is the number of nodes. (The worst case occurs when all nodes are on one side)

/* Determine whether the node values of the Left subtree are smaller than Val */bool issubtreelessthan (binarytree * P, int Val) {If (! P) return true; Return (p-> data <Val & issubtreelessthan (p-> left, Val) & issubtreelessthan (p-> right, Val ));} /* determine whether the node values of the right subtree are greater than Val */bool issubtreegreaterthan (binarytree * P, int Val) {If (! P) return true; Return (p-> DATA> Val & issubtreegreaterthan (p-> left, Val) & issubtreegreaterthan (p-> right, Val ));} /* determine whether a binary tree is a binary search tree */bool isbstbruteforce (binarytree * P) {If (! P) return true; return issubtreelessthan (p-> left, p-> data) & issubtreegreaterthan (p-> right, p-> data) & isbstbruteforce (p-> left) & isbstbruteforce (p-> right );}

A similar solution is to determine whether the maximum value of the Left subtree of a node is greater than that of a node. If yes, the binary tree is not a binary search tree. If not, then judge whether the minimum value of the right subtree is smaller than or equal to the value of node. If yes, it is not a binary search tree. If not, recursively determine whether the left and right subtree is a binary search tree. (The maxvalue and minvalue functions in the Code return the maximum and minimum values of the binary tree respectively. Here we assume that the binary tree is a binary search tree, and the actual return is not necessarily the maximum and minimum values)

Int isbst (struct node * node) {If (node = NULL) Return (true); // if the maximum value of the Left subtree> = value of the current node, returns false if (node-> left! = NULL & maxvalue (node-> left)> = node-> data) Return (false); // if the right subtree has a minimum value <= the value of the current node, returns false if (node-> right! = NULL & minvalue (node-> right) <= node-> data) Return (false); // If the left or right subtree is not BST, false if (! Isbst (node-> left) |! Isbst (node-> right) Return (false); // if all tests are passed, true return (true) is returned );}

Solution 2: better solution the binary tree (1) mentioned earlier is used as an example. When we traverse from node 10 to the right node 15, we know that the right subtree node values must all be in10 and+ Infinity)Between.When we traverse to the left child node 6 of node 15, we know that the value of the left child Tree node of node 15 must be between 10 and 15. Obviously, node 6 does not meet the condition, so it is not a binary search tree. The algorithm code is as follows:

Int isbst2 (struct node * node) {return (isbstutil (node, int_min, int_max);}/* returns true if the given binary tree is BST, and its value is> min and <Max. */INT isbstutil (struct node * node, int min, int max) {If (node = NULL) Return (true); // If the min and Max constraints are not met, returns false if (node-> data <= min | node-> DATA> = max) Return (false ); // recursively determine whether left and right subtree meets the min and Max constraints. Return isbstutil (node-> left, Min, node-> data) & isbstutil (node-> right, node-> data, max ));}

Because the algorithm only needs to access each node once, the time complexity is O (n), which is much more efficient than solution 1.

Solution 3: the central order traversal algorithm provides the following solution because the node values of a binary search tree are sorted in ascending order. The time complexity of this solution is O (n ).
Bool isbstinorder (binarytree * root) {int Prev = int_min; return isbstinorderhelper (root, Prev);}/* This function determines whether Binary Tree P is a binary search tree, and the node values are greater than Prev */bool isbstinorderhelper (binarytree * P, Int & PREV) {If (! P) return true; If (isbstinorderhelper (p-> left, Prev) {// If the left subtree is a binary search tree, and the node values are greater than the prev if (p-> DATA> PREV) {// determine whether the current node value is greater than the prev, this is because Prev has been set to the maximum value of the node that has been traversed in the middle order. Prev = p-> data; return isbstinorderhelper (p-> right, Prev); // If the node value is greater than Prev, set Prev to the current node value, determine whether the right subtree has a binary search tree and the node value is greater than the prev. } Else {return false ;}}


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.