Question
Create a binary treethat performs a Depth First Search (DFS). Python I was provided the following Python code and the data to createa binary tree
Create a binary treethat performs a Depth First Search (DFS).Python
I was provided the following Python code and the data to createa binary tree that performs a Depth First Search (DFS). Pleasedon't sort the data. How should I update the code and complete therequirement? Please show the logic. Thanks so much!
Code - bn_tree.py
class Node: def __init__(self, data): self.data = data self.right_child = None self.left_child = None
class Tree: def __init__(self): self.root_node = None
def insert(self, data): node = Node(data) if self.root_node is None: self.root_node =node else: current =self.root_node parent = None while True: parent =current ifnode.data < parent.data: current = current.left_child if current is None: parent.left_child = node return else: current = current.right_child if current is None: parent.right_child = node return
def search(self, data): current = self.root_node while True: if current is None: returnNone elif current.data isdata: returndata elif current.data >data: current =current.left_child else: current =current.right_child
def get_node_with_parent(self, data): parent = None current = self.root_node if current is None: return (parent,None) while True: if current.data ==data: return(parent, current) elif current.data >data: parent =current current =current.left_child else: parent =current current =current.right_child
return (parent, current)
Data: practice_data.csv
149 | 3318 | 9336 | 6480 | 1396 | 7015 | 5015 | 3914 | 3185 | 5910 | 4113 | 136 | 1296 | 3330 | 9374 | 9207 | 4149 | 8473 | 1627 | 1173 | 4357 | 2586 | 4255 | 3447 | 7759 | 9995 | 1679 | 6471 | 5031 | 8242 | 8127 | 3924 | 2968 | 2243 | 7747 | 6030 | 1578 | 5365 | 5550 | 6823 | 9053 | 4429 | 5965 | 9062 | 2580 | 9255 | 6176 | 9490 | 1484 | 2127 | 2983 | 4484 | 2619 | 248 | 7858 | 1250 | 3102 | 313 | 9619 | 8507 | 6583 | 8946 | 3252 | 8507 | 8227 | 9938 | 8634 | 7059 | 3982 | 2877 | 7884 | 680 | 2773 | 9169 | 7392 | 6005 | 7345 | 4705 | 9275 | 1449 | 5001 | 4760 | 5641 | 3772 | 6493 | 1303 | 1765 | 7850 | 7251 | 7580 | 5417 | 2141 | 3730 | 7346 | 6582 | 3349 | 6663 | 9874 | 6197 | 1151 |
# bintree_tree.py class Node: definit__(self, self.data = data self.right_child = None self.left_child = None class Tree: definit__(self): self.root_node = None data): def insert (self, data): node Node (data) if self.root_node is None: self.root_node = node else: current = self.root_node parent None while True: parent current if node.data < parent.data: current = current.left_child if current is None: parent.left_child = node else: return current current.right_child if current is None: parent.right_child = node def search(self, data): else: return current = self.root_node while True: if current is None: return None elif current. data is data: return data elif current.data > data: current current.left_child current = current.right_child def get_node_with_parent(self, data): None parent current = self.root_node if current is None: return (parent, None) while True: else: if current.data == data: eturn (parent, current) elif current.data > data: parent current current = current.left_child parent current current = current.right_child return (parent, current)
Step by Step Solution
3.35 Rating (155 Votes )
There are 3 Steps involved in it
Step: 1
Node class for binary tree class Node def initself data selfdata data selfrightchild None selfleftch...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started