Verify binary search tree with duplicate keys
up vote
0
down vote
favorite
Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right
def IsBinarySearchTree(tree):
# Implement correct algorithm here
min = -sys.maxsize
max = sys.maxsize
key_hash = {}
def is_bst_until(idx_node,maxi,mini, is_left = False):
if idx_node == -1:
return True
node = tree[idx_node]
key = node[0]
idx_left = node[1]
idx_right = node[2]
if (key>maxi) or (key<mini):
return False
if is_left:
if (key==mini):
return False
return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))
if len(tree) == 0:
return True
return is_bst_until(0, max, min)
def main():
nodes = int(sys.stdin.readline().strip())
tree =
for i in range(nodes):
tree.append(list(map(int, sys.stdin.readline().strip().split())))
if IsBinarySearchTree(tree):
print("CORRECT")
else:
print("INCORRECT")
threading.Thread(target=main).start()
Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right
Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.
Examples:
3
2 1 2
2 -1 -1
3 -1 -1
#Incorrect
5
1 -1 1
2 -1 2
3 -1 3
4 -1 4
5 -1 -1
#Correct
Frankly speaking I don't think my solution is a good one. I'm concerned about is_left
I'll appreciate if you suggest me an alternative solution
python algorithm binary-search
add a comment |
up vote
0
down vote
favorite
Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right
def IsBinarySearchTree(tree):
# Implement correct algorithm here
min = -sys.maxsize
max = sys.maxsize
key_hash = {}
def is_bst_until(idx_node,maxi,mini, is_left = False):
if idx_node == -1:
return True
node = tree[idx_node]
key = node[0]
idx_left = node[1]
idx_right = node[2]
if (key>maxi) or (key<mini):
return False
if is_left:
if (key==mini):
return False
return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))
if len(tree) == 0:
return True
return is_bst_until(0, max, min)
def main():
nodes = int(sys.stdin.readline().strip())
tree =
for i in range(nodes):
tree.append(list(map(int, sys.stdin.readline().strip().split())))
if IsBinarySearchTree(tree):
print("CORRECT")
else:
print("INCORRECT")
threading.Thread(target=main).start()
Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right
Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.
Examples:
3
2 1 2
2 -1 -1
3 -1 -1
#Incorrect
5
1 -1 1
2 -1 2
3 -1 3
4 -1 4
5 -1 -1
#Correct
Frankly speaking I don't think my solution is a good one. I'm concerned about is_left
I'll appreciate if you suggest me an alternative solution
python algorithm binary-search
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right
def IsBinarySearchTree(tree):
# Implement correct algorithm here
min = -sys.maxsize
max = sys.maxsize
key_hash = {}
def is_bst_until(idx_node,maxi,mini, is_left = False):
if idx_node == -1:
return True
node = tree[idx_node]
key = node[0]
idx_left = node[1]
idx_right = node[2]
if (key>maxi) or (key<mini):
return False
if is_left:
if (key==mini):
return False
return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))
if len(tree) == 0:
return True
return is_bst_until(0, max, min)
def main():
nodes = int(sys.stdin.readline().strip())
tree =
for i in range(nodes):
tree.append(list(map(int, sys.stdin.readline().strip().split())))
if IsBinarySearchTree(tree):
print("CORRECT")
else:
print("INCORRECT")
threading.Thread(target=main).start()
Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right
Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.
Examples:
3
2 1 2
2 -1 -1
3 -1 -1
#Incorrect
5
1 -1 1
2 -1 2
3 -1 3
4 -1 4
5 -1 -1
#Correct
Frankly speaking I don't think my solution is a good one. I'm concerned about is_left
I'll appreciate if you suggest me an alternative solution
python algorithm binary-search
Given a binary tree with integers as its keys I need to test whether it is a correct binary search tree. Duplicate integers allowed. Smaller elements are to the left, bigger elements are to the right, and duplicates are always to the right
def IsBinarySearchTree(tree):
# Implement correct algorithm here
min = -sys.maxsize
max = sys.maxsize
key_hash = {}
def is_bst_until(idx_node,maxi,mini, is_left = False):
if idx_node == -1:
return True
node = tree[idx_node]
key = node[0]
idx_left = node[1]
idx_right = node[2]
if (key>maxi) or (key<mini):
return False
if is_left:
if (key==mini):
return False
return (is_bst_until(idx_right, maxi, key)) and (is_bst_until(idx_left, key-1, mini, is_left=True))
if len(tree) == 0:
return True
return is_bst_until(0, max, min)
def main():
nodes = int(sys.stdin.readline().strip())
tree =
for i in range(nodes):
tree.append(list(map(int, sys.stdin.readline().strip().split())))
if IsBinarySearchTree(tree):
print("CORRECT")
else:
print("INCORRECT")
threading.Thread(target=main).start()
Input Format. The first line contains the number of vertices n. The vertices of the tree are numbered from 0 to n−1. Vertex 0 is the root. The next n lines contain information about vertices 0, 1, ..., 𝑛 − 1 in order. Each of these lines contains three integers key, left and right
Output Format. If the given binary tree is a correct binary search tree, output one word “CORRECT”. Otherwise, output one word “INCORRECT”.
Examples:
3
2 1 2
2 -1 -1
3 -1 -1
#Incorrect
5
1 -1 1
2 -1 2
3 -1 3
4 -1 4
5 -1 -1
#Correct
Frankly speaking I don't think my solution is a good one. I'm concerned about is_left
I'll appreciate if you suggest me an alternative solution
python algorithm binary-search
python algorithm binary-search
asked 2 mins ago
Daniel Chepenko
1335
1335
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208588%2fverify-binary-search-tree-with-duplicate-keys%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown