Perform BFS on a Binary Tree
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Task: Perform Breadth first traversal on a Binary Search Tree and print the elements traversed.
class Node(object):
def __init__(self, value,left=None, right=None):
self.value = value
self.left = left
self.right = right
class BinarySearchTree(object):
def __init__(self, value):
self.root = Node(value)
def insert(self, value):
current = self.root
while current:
if value > current.value:
if current.right is None:
current.right = Node(value)
break
else:
current = current.right
else:
if current.left is None:
current.left = Node(value)
break
else:
current = current.left
def Breadth_first_search(self,root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
visited =
if root:
visited.append(root)
print root.value
current = root
while current :
if current.left:
print current.left.value
visited.append(current.left)
if current.right:
print current.right.value
visited.append(current.right)
visited.pop(0)
if not visited:
break
current = visited[0]
t = BinarySearchTree(100)
t.insert(12)
t.insert(92)
t.insert(112)
t.insert(123)
t.insert(2)
t.insert(11)
t.insert(52)
t.insert(3)
t.insert(66)
t.insert(10)
print "Output of Breadth First search is "
t.Breadth_first_search(t.root)
The code runs acceptably and gives the correct output. Is there better solution to this problem?
python binary-search breadth-first-search
add a comment |Â
up vote
0
down vote
favorite
Task: Perform Breadth first traversal on a Binary Search Tree and print the elements traversed.
class Node(object):
def __init__(self, value,left=None, right=None):
self.value = value
self.left = left
self.right = right
class BinarySearchTree(object):
def __init__(self, value):
self.root = Node(value)
def insert(self, value):
current = self.root
while current:
if value > current.value:
if current.right is None:
current.right = Node(value)
break
else:
current = current.right
else:
if current.left is None:
current.left = Node(value)
break
else:
current = current.left
def Breadth_first_search(self,root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
visited =
if root:
visited.append(root)
print root.value
current = root
while current :
if current.left:
print current.left.value
visited.append(current.left)
if current.right:
print current.right.value
visited.append(current.right)
visited.pop(0)
if not visited:
break
current = visited[0]
t = BinarySearchTree(100)
t.insert(12)
t.insert(92)
t.insert(112)
t.insert(123)
t.insert(2)
t.insert(11)
t.insert(52)
t.insert(3)
t.insert(66)
t.insert(10)
print "Output of Breadth First search is "
t.Breadth_first_search(t.root)
The code runs acceptably and gives the correct output. Is there better solution to this problem?
python binary-search breadth-first-search
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Task: Perform Breadth first traversal on a Binary Search Tree and print the elements traversed.
class Node(object):
def __init__(self, value,left=None, right=None):
self.value = value
self.left = left
self.right = right
class BinarySearchTree(object):
def __init__(self, value):
self.root = Node(value)
def insert(self, value):
current = self.root
while current:
if value > current.value:
if current.right is None:
current.right = Node(value)
break
else:
current = current.right
else:
if current.left is None:
current.left = Node(value)
break
else:
current = current.left
def Breadth_first_search(self,root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
visited =
if root:
visited.append(root)
print root.value
current = root
while current :
if current.left:
print current.left.value
visited.append(current.left)
if current.right:
print current.right.value
visited.append(current.right)
visited.pop(0)
if not visited:
break
current = visited[0]
t = BinarySearchTree(100)
t.insert(12)
t.insert(92)
t.insert(112)
t.insert(123)
t.insert(2)
t.insert(11)
t.insert(52)
t.insert(3)
t.insert(66)
t.insert(10)
print "Output of Breadth First search is "
t.Breadth_first_search(t.root)
The code runs acceptably and gives the correct output. Is there better solution to this problem?
python binary-search breadth-first-search
Task: Perform Breadth first traversal on a Binary Search Tree and print the elements traversed.
class Node(object):
def __init__(self, value,left=None, right=None):
self.value = value
self.left = left
self.right = right
class BinarySearchTree(object):
def __init__(self, value):
self.root = Node(value)
def insert(self, value):
current = self.root
while current:
if value > current.value:
if current.right is None:
current.right = Node(value)
break
else:
current = current.right
else:
if current.left is None:
current.left = Node(value)
break
else:
current = current.left
def Breadth_first_search(self,root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
visited =
if root:
visited.append(root)
print root.value
current = root
while current :
if current.left:
print current.left.value
visited.append(current.left)
if current.right:
print current.right.value
visited.append(current.right)
visited.pop(0)
if not visited:
break
current = visited[0]
t = BinarySearchTree(100)
t.insert(12)
t.insert(92)
t.insert(112)
t.insert(123)
t.insert(2)
t.insert(11)
t.insert(52)
t.insert(3)
t.insert(66)
t.insert(10)
print "Output of Breadth First search is "
t.Breadth_first_search(t.root)
The code runs acceptably and gives the correct output. Is there better solution to this problem?
python binary-search breadth-first-search
edited Apr 13 at 17:53
Sam Onela
5,78461544
5,78461544
asked Apr 13 at 15:17
Latika Agarwal
861216
861216
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
PEP-0008: your method names should follow
snake_case
style, and there should be a space between arguments. So change your method singature to:def breadth_first_search(self, root):
# remaining codeYou maintain a list called as
visited
, however the nodes added in it are ones which are yet to be visited. Call itto_visit
instead. One should use the variable namevisited
to keep track of nodes that are visited already.to_visit =
if root:
to_visit.append(root)
print( root.value)
# remainig code with to_vist replacing visitedYou assign the node at the end, and pop the current node just before that. Better to do this in the beginning of the loop, and iterate over the existence of elements within the list. This way, your code gets much concise and clean:
while to_visit:
current = to_visit.pop(0)
if current.left:
print( current.left.value)
to_visit.append(current.left)
if current.right:
print( current.right.value)
to_visit.append(current.right)You pre-check and print the value of the left and right child, and that of root as well. It would be better if these statements could be reduced:
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
So now, your function looks like
def breadth_first_search(self, root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
to_visit =
if root:
to_visit.append(root)
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
But this gets called as
t.Breadth_first_search(t.root)
, since root is already present, we can use a default value here. Also, the list can be initialized with an element, so we save theappend
call:def breadth_first_search(self, root=None):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
root = self.root if root is None else root
to_visit = [root]
while to_visit:
current = to_visit.pop(0)
print(current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)And now, you can call it as
t.breadth_first_search()
.But wait, if it is breadth_first_search, shouldn't you be searching for something? Well no, your question states to do a breadth first traversal, so you should rename your method to:
def breadth_first_traversal(self, root=None):
# remaining code as above
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
PEP-0008: your method names should follow
snake_case
style, and there should be a space between arguments. So change your method singature to:def breadth_first_search(self, root):
# remaining codeYou maintain a list called as
visited
, however the nodes added in it are ones which are yet to be visited. Call itto_visit
instead. One should use the variable namevisited
to keep track of nodes that are visited already.to_visit =
if root:
to_visit.append(root)
print( root.value)
# remainig code with to_vist replacing visitedYou assign the node at the end, and pop the current node just before that. Better to do this in the beginning of the loop, and iterate over the existence of elements within the list. This way, your code gets much concise and clean:
while to_visit:
current = to_visit.pop(0)
if current.left:
print( current.left.value)
to_visit.append(current.left)
if current.right:
print( current.right.value)
to_visit.append(current.right)You pre-check and print the value of the left and right child, and that of root as well. It would be better if these statements could be reduced:
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
So now, your function looks like
def breadth_first_search(self, root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
to_visit =
if root:
to_visit.append(root)
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
But this gets called as
t.Breadth_first_search(t.root)
, since root is already present, we can use a default value here. Also, the list can be initialized with an element, so we save theappend
call:def breadth_first_search(self, root=None):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
root = self.root if root is None else root
to_visit = [root]
while to_visit:
current = to_visit.pop(0)
print(current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)And now, you can call it as
t.breadth_first_search()
.But wait, if it is breadth_first_search, shouldn't you be searching for something? Well no, your question states to do a breadth first traversal, so you should rename your method to:
def breadth_first_traversal(self, root=None):
# remaining code as above
add a comment |Â
up vote
3
down vote
accepted
PEP-0008: your method names should follow
snake_case
style, and there should be a space between arguments. So change your method singature to:def breadth_first_search(self, root):
# remaining codeYou maintain a list called as
visited
, however the nodes added in it are ones which are yet to be visited. Call itto_visit
instead. One should use the variable namevisited
to keep track of nodes that are visited already.to_visit =
if root:
to_visit.append(root)
print( root.value)
# remainig code with to_vist replacing visitedYou assign the node at the end, and pop the current node just before that. Better to do this in the beginning of the loop, and iterate over the existence of elements within the list. This way, your code gets much concise and clean:
while to_visit:
current = to_visit.pop(0)
if current.left:
print( current.left.value)
to_visit.append(current.left)
if current.right:
print( current.right.value)
to_visit.append(current.right)You pre-check and print the value of the left and right child, and that of root as well. It would be better if these statements could be reduced:
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
So now, your function looks like
def breadth_first_search(self, root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
to_visit =
if root:
to_visit.append(root)
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
But this gets called as
t.Breadth_first_search(t.root)
, since root is already present, we can use a default value here. Also, the list can be initialized with an element, so we save theappend
call:def breadth_first_search(self, root=None):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
root = self.root if root is None else root
to_visit = [root]
while to_visit:
current = to_visit.pop(0)
print(current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)And now, you can call it as
t.breadth_first_search()
.But wait, if it is breadth_first_search, shouldn't you be searching for something? Well no, your question states to do a breadth first traversal, so you should rename your method to:
def breadth_first_traversal(self, root=None):
# remaining code as above
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
PEP-0008: your method names should follow
snake_case
style, and there should be a space between arguments. So change your method singature to:def breadth_first_search(self, root):
# remaining codeYou maintain a list called as
visited
, however the nodes added in it are ones which are yet to be visited. Call itto_visit
instead. One should use the variable namevisited
to keep track of nodes that are visited already.to_visit =
if root:
to_visit.append(root)
print( root.value)
# remainig code with to_vist replacing visitedYou assign the node at the end, and pop the current node just before that. Better to do this in the beginning of the loop, and iterate over the existence of elements within the list. This way, your code gets much concise and clean:
while to_visit:
current = to_visit.pop(0)
if current.left:
print( current.left.value)
to_visit.append(current.left)
if current.right:
print( current.right.value)
to_visit.append(current.right)You pre-check and print the value of the left and right child, and that of root as well. It would be better if these statements could be reduced:
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
So now, your function looks like
def breadth_first_search(self, root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
to_visit =
if root:
to_visit.append(root)
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
But this gets called as
t.Breadth_first_search(t.root)
, since root is already present, we can use a default value here. Also, the list can be initialized with an element, so we save theappend
call:def breadth_first_search(self, root=None):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
root = self.root if root is None else root
to_visit = [root]
while to_visit:
current = to_visit.pop(0)
print(current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)And now, you can call it as
t.breadth_first_search()
.But wait, if it is breadth_first_search, shouldn't you be searching for something? Well no, your question states to do a breadth first traversal, so you should rename your method to:
def breadth_first_traversal(self, root=None):
# remaining code as above
PEP-0008: your method names should follow
snake_case
style, and there should be a space between arguments. So change your method singature to:def breadth_first_search(self, root):
# remaining codeYou maintain a list called as
visited
, however the nodes added in it are ones which are yet to be visited. Call itto_visit
instead. One should use the variable namevisited
to keep track of nodes that are visited already.to_visit =
if root:
to_visit.append(root)
print( root.value)
# remainig code with to_vist replacing visitedYou assign the node at the end, and pop the current node just before that. Better to do this in the beginning of the loop, and iterate over the existence of elements within the list. This way, your code gets much concise and clean:
while to_visit:
current = to_visit.pop(0)
if current.left:
print( current.left.value)
to_visit.append(current.left)
if current.right:
print( current.right.value)
to_visit.append(current.right)You pre-check and print the value of the left and right child, and that of root as well. It would be better if these statements could be reduced:
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
So now, your function looks like
def breadth_first_search(self, root):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
to_visit =
if root:
to_visit.append(root)
while to_visit:
current = to_visit.pop(0)
print( current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)
But this gets called as
t.Breadth_first_search(t.root)
, since root is already present, we can use a default value here. Also, the list can be initialized with an element, so we save theappend
call:def breadth_first_search(self, root=None):
"""In BFS the Node Values at each level of the Tree are traversed before going to next level"""
root = self.root if root is None else root
to_visit = [root]
while to_visit:
current = to_visit.pop(0)
print(current.value)
if current.left:
to_visit.append(current.left)
if current.right:
to_visit.append(current.right)And now, you can call it as
t.breadth_first_search()
.But wait, if it is breadth_first_search, shouldn't you be searching for something? Well no, your question states to do a breadth first traversal, so you should rename your method to:
def breadth_first_traversal(self, root=None):
# remaining code as above
edited Apr 14 at 10:26
answered Apr 14 at 10:03
mu ç¡
314211
314211
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191984%2fperform-bfs-on-a-binary-tree%23new-answer', 'question_page');
);
Post as a guest
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
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
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