HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Prove $T$ is a BST iff for every node $x$ of $T$ that is not a leaf, the key of $x$ is larger or equal than the key of the left child of $x$

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
leftthelargerequalnodethaneverybstiffprove

Problem

Let $T$ be a complete binary tree. Prove that $T$ is a binary search tree if and only if for every node $x$ of $T$ that is not a leaf, the key of $x$ is larger or equal than the key of the left child of $x$ or it is less than or equal than the key of the right child.

To prove that if $T$ is BST then for every node $x$ of $T$ that is not a leaf, the key of $x$ is larger or equal than the key of the left child I could say that from the definition of BST it follows that given a node $x$ and a node $y$ which belongs to the left sub-tree of $x$ then $key(y) \le key(x)$.

By the hypothesis if we choose any node $z$ of in the left sub-tree of $y$ then $key(z) \le key(y)$ so the relation holds true for all of the nodes of the tree.

I'm not sure how to approach the proof of the second direction: if for every node $x$ of $T$ that is not a leaf, the key of $x$ is larger or equal than the key of the left child then $T$ is a BST.

Solution

The second direction is not always true, in a BST all elements in the right subtree of a node should have a key bigger than its key and all elements in the left subtree of a node should have a key less than its key. for example, consider this binary tree:


4
/ \
2 6
/ \ / \
1 9 3 7


it has the property that for every node x of $T$ that is not a leaf, the key of x is larger or equal than the key of the left child and less or equal than the key of the right child, but it's not a BST since 3 is in the right subtree of root but its key is less than the key of root.

Context

StackExchange Computer Science Q#76866, answer score: 3

Revisions (0)

No revisions yet.