Home c Binary Search Tree on C / C++

Binary Search Tree on C / C++

Author

Date

Category

There is a task. There is a binary tree. It is required to determine whether this tree is a binary search tree. It is advisable to submit a decision in C++ or S. The temporal and spatial complexity of the algorithm is estimated, as well as the versatility of the approach.


Answer 1, Authority 100%

UPD: My not true, correct.

Suppose that the tree is defined as:

struct binarytreenode {
  BinaryTreenode (INT VAL): Leftchild (NULL), RightChild (NULL),
    PARENT (NULL), VALUE (VAL) {}
  Binarytreenode * leftchild;
  BinaryTreenode * RightChild;
  BinaryTreenode * Parent;
  INT VALUE;
};

Then the verification algorithm will be as follows:

bool isbst (binarytreenode * root)
{
  Return isbst_ (root, 0, 0);
}
BOOL ISBST_ (BinaryTreenode * Node, BinaryTreenode * Minparent, BinaryTreenode * MaxParent)
{
  if (minparent & amp; & gt; minparent- & gt; value & gt; = node- & gt; value)
    RETURN FALSE;
  If (MaxParent & Amp; & amp; MaxParent- & GT; Value & LT; = Node- & GT; Value)
    RETURN FALSE;
  if (node- & gt; leftchild! = 0 & amp; & amp;! isbst_ (node- & gt; leftchild, minparent, node))
    RETURN FALSE;
  If (node- & gt; rightchild! = 0 & amp; & amp;! isbst_ (node- & gt; rightchild, node, maxparent))
    RETURN FALSE;
  RETURN TRUE;
}

Temporary complexity O (n), where N is the number of nodes in the tree. Spatial complexity O (H), where H is the depth of the tree.


Answer 2, Authority 67%

http://www.structur.h1.ru/derevo.htm – the structure of the tree is written here
In fact, your task is well solved by the recursion recursively go down the wood. If when checking it turns out that it is not a tree – Throw Someerror; If everything is fine – then no exceptions to throw the recursion will unfold and say that everything is ok


Answer 3, Authority 67%

We’re bypassed the tree according to the rule of the LCP, on the way you check whether the increasing sequence (or decreasing, can be determined by the first two unequal values). If it does not work, then not a search tree. Something like this.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions