Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image

Data: practice_data.csv

1493318933664801396701550153914318559104113136129633309374920741498473162711734357258642553447775999951679647150318242812739242968224377476030157853655550682390534429596590622580925561769490148421272983448426192487858125031023139619850765838946325285078227993886347059398228777884680277391697392600573454705927514495001476056413772649313031765785072517580541721413730734665823349666398746197

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... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students also viewed these Electrical Engineering questions

Question

=+b) In which graph is a larger value of a used?

Answered: 1 week ago

Question

Show that for 0 k n, where H (x) is the entropy function (C.7).

Answered: 1 week ago