Trees Class - 2

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 172:- Trees Class - 2

Of course! Let's dive deeper into a more advanced tree class implementation. In this example, I'll demonstrate the creation of a Binary Tree class along with common tree traversal methods like in-order, pre-order, and post-order traversals.

pythonCopy code

class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None class BinaryTree: def __init__(self, root_data): self.root = TreeNode(root_data) def insert(self, data): self._insert_recursively(self.root, data) def _insert_recursively(self, node, data): if data < node.data: if node.left is None: node.left = TreeNode(data) else: self._insert_recursively(node.left, data) elif data > node.data: if node.right is None: node.right = TreeNode(data) else: self._insert_recursively(node.right, data) def in_order_traversal(self, node): if node: self.in_order_traversal(node.left) print(node.data, end=" ") self.in_order_traversal(node.right) def pre_order_traversal(self, node): if node: print(node.data, end=" ") self.pre_order_traversal(node.left) self.pre_order_traversal(node.right) def post_order_traversal(self, node): if node: self.post_order_traversal(node.left) self.post_order_traversal(node.right) print(node.data, end=" ") # Example usage tree = BinaryTree(10) tree.insert(5) tree.insert(15) tree.insert(3) tree.insert(7) tree.insert(12) tree.insert(18) print("In-order traversal:") tree.in_order_traversal(tree.root) print("\nPre-order traversal:") tree.pre_order_traversal(tree.root) print("\nPost-order traversal:") tree.post_order_traversal(tree.root)

In this example, we've defined two classes: TreeNode and BinaryTree. The TreeNode class represents individual nodes in the tree, with references to their left and right children. The BinaryTree class contains methods for inserting elements and performing different types of tree traversals.

The insert method is used to insert data into the binary tree. The _insert_recursively method is a helper method that recursively navigates the tree to find the appropriate position for insertion.

The in_order_traversal, pre_order_traversal, and post_order_traversal methods implement the respective traversal algorithms. These traversal methods visit the nodes of the tree in a specific order and print their data.

This example demonstrates a simple binary tree and its traversal methods. Binary trees are just one type of tree structure, and you can further explore other types of trees, such as binary search trees, AVL trees, and more complex tree-related algorithms and data structures.

24. Trees - I

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?