0% found this document useful (0 votes)
25 views2 pages

Code: Binary Tree: Class DFS: Def - Init - (Self, Data None, Left None, Right None)

This document defines a Binary Tree class called dfs that initializes nodes with data, left, and right pointers. It also defines an inorder traversal method that recursively prints the data of nodes by traversing the left subtree, then the node, then the right subtree. The main method creates a binary tree with nested nodes and calls inorder to print the values in ascending order from left to right.

Uploaded by

Ch saab14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Code: Binary Tree: Class DFS: Def - Init - (Self, Data None, Left None, Right None)

This document defines a Binary Tree class called dfs that initializes nodes with data, left, and right pointers. It also defines an inorder traversal method that recursively prints the data of nodes by traversing the left subtree, then the node, then the right subtree. The main method creates a binary tree with nested nodes and calls inorder to print the values in ascending order from left to right.

Uploaded by

Ch saab14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Code:

Binary Tree
class dfs:

def __init__(self, data=None, left=None, right=None):

self.data = data

self.left = left

self.right = right

def inorder(root):

inorder(root.left)

print(root.data, end=' ')

inorder(root.right)

if __name__ == '__main__':
root = dfs(1)

root.left = dfs(2)

root.right = dfs(3)

root.left.left = dfs(4)

root.right.left = dfs(5)

root.right.right = dfs(6)

root.right.left.left = dfs(7)

root.right.left.right = dfs(8)

inorder(root)

You might also like