Write a Python source code to implement the Depth-First-Search algorithm for the following tree in Figure 2, where 5 is the root node. The output of the source code should be the order of the nodes visited in the graph. Implement the dfs() function. When a node has two children, visit the left child first then the right child.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a Python source code to implement the Depth-First-Search algorithm for the
following tree in Figure 2, where 5 is the root node. The output of the source code should be the
order of the nodes visited in the graph. Implement the dfs() function. When a node has two children, visit the left child first then the right child.

Use this Template:

class TreeNode:
    def __init__(self, val=0):
        self.val = val
        self.left = None
        self.right = None


# Traverse a tree DFS style.
def traverse(root):
    
    # List that represents visited order.
    ret = []
    
    def dfs(node):
        # TODO: Implement DFS algorithm here
        return
    
    dfs(root)
    return ret


def main():
    nodes = [None] * 9
    for i in range(9):
        nodes[i] = TreeNode(i+1)
    # Tree construction
    # 5
    nodes[4].left = nodes[3]
    nodes[4].right = nodes[5]
    # 4
    nodes[3].left = nodes[1]
    # 2
    nodes[1].left = nodes[0]
    nodes[1].right = nodes[2]
    # 6
    nodes[5].right = nodes[7]
    # 8
    nodes[7].left = nodes[6]
    nodes[7].right = nodes[8]

    root = nodes[4]
    print(traverse(root))


if __name__ == "__main__":
    main()

 

Figure 2
2
4
(3
5
8
(9
Transcribed Image Text:Figure 2 2 4 (3 5 8 (9
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Types of trees
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education