{"id":71121,"date":"2025-01-30T16:42:03","date_gmt":"2025-01-30T11:12:03","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=71121"},"modified":"2026-01-07T13:37:56","modified_gmt":"2026-01-07T08:07:56","slug":"crud-operations-on-binary-trees-using-python","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/crud-operations-on-binary-trees-using-python\/","title":{"rendered":"CRUD Operations on Binary Trees Using Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p>What makes binary trees so crucial in computer science? If you\u2019ve ever worked with hierarchical data structures, you\u2019ve likely encountered them. From database indexing to expression parsing, binary trees provide an efficient way to store and manipulate data. Their ability to maintain order and optimize search operations makes them invaluable in various applications.<\/p>\n\n\n\n<p>But what happens when you need to modify this structured data? That\u2019s where CRUD operations come into play. Whether you&#8217;re inserting new values, retrieving specific nodes, updating existing data, or deleting elements, understanding these operations is key to managing binary trees effectively. In this guide, we\u2019ll break down the implementation of CRUD operations in Python, providing code snippets and explanations to help you master this essential data structure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Understanding the CRUD Operations<\/strong><\/h3>\n\n\n\n<p>CRUD stands for Create, Read, Update, and Delete, which are the four basic operations for persistent storage in databases and other data structures. When applied to binary trees, these operations allow for the dynamic management of nodes, facilitating everything from the insertion of new data to the retrieval, modification, and removal of existing data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating &amp; Reading Nodes in a Binary Tree<\/strong><\/h2>\n\n\n\n<p>The process of creating nodes in a <a href=\"https:\/\/www.guvi.in\/blog\/mastering-binary-trees-with-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">binary tree<\/a> involves adding new nodes to the tree while maintaining its properties. Here\u2019s how you can implement the creation of nodes in Python:<\/p>\n\n\n\n<p>class Node:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val : Stores data of Node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left : Reference to Left Child Node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right : Reference to Right Child Node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, val):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.val = val<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.right = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.left = None<\/p>\n\n\n\n<p>class SumanBinaryTree:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root : Reference to the root node of tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def insert_node(self, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.root is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(self.root, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __insert__recursive(self, node, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if data &lt; node.val:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.left is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.left = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.left, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.right is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.right = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.right, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def print_tree(self, node, level=0):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node is not None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.right, level + 1)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8216; &#8216; * 4 * level + &#8216;-&gt;&#8217;, node.val)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.left, level + 1)<\/p>\n\n\n\n<p>if __name__ == &#8220;__main__&#8221;:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Creating the binary tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree = SumanBinaryTree()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root = Node(10)&nbsp; # Root node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left = Node(20)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right = Node(30)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 2<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.left = Node(40)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.right = Node(50)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.left = Node(60)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right = Node(70)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 3<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right.left = Node(80)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right.right = Node(90)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Print the binary tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.print_tree(tree.root)<\/p>\n\n\n\n<p>Let&#8217;s break down and describe each part of the given code in detail. The code defines a binary tree structure and provides functionality to insert nodes and print the tree.<\/p>\n\n\n\n<p>class Node:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val : Stores data of Node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;left : Reference to Left Child Node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right : Reference to Right Child Node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, val):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.val = val<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.right = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.left = None<\/p>\n\n\n\n<p><strong>Description<\/strong>:<\/p>\n\n\n\n<ul>\n<li><strong>Purpose<\/strong>: The Node class encapsulates the properties and behaviors of each node within a binary tree. Each node stores a value and has references to its left and right child nodes.<br><\/li>\n\n\n\n<li><strong>Attributes<\/strong>:\n<ul>\n<li>val: Stores the data of the node.<\/li>\n\n\n\n<li>left: Reference to the left child node (initially set to None).<\/li>\n\n\n\n<li>right: Reference to the right child node (initially set to None).<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Constructor<\/strong>: The __init__ method initializes a new node with the given value and sets its left and right child references to None.<\/li>\n<\/ul>\n\n\n\n<p>class SumanBinaryTree:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root : Reference to the root node of tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def insert_node(self, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.root is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(self.root, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __insert__recursive(self, node, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if data &lt; node.val:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.left is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.left = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.left, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.right is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.right = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.right, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def print_tree(self, node, level=0):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node is not None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.right, level + 1)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8216; &#8216; * 4 * level + &#8216;-&gt;&#8217;, node.val)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.left, level + 1)<\/p>\n\n\n\n<p><strong>Description<\/strong>:<\/p>\n\n\n\n<ul>\n<li><strong>Purpose<\/strong>: The SumanBinaryTree class manages the structure and operations of the binary tree, such as inserting nodes and printing the tree.<\/li>\n\n\n\n<li><strong>Attributes<\/strong>:\n<ul>\n<li>root: Reference to the root node of the tree (initially set to None).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Constructor<\/strong>: The __init__ method initializes an empty binary tree with the root set to None.<br><\/li>\n\n\n\n<li><strong>Methods<\/strong>:\n<ul>\n<li>insert_node(self, data): Inserts a node with the given data into the binary tree.\n<ul>\n<li>If the tree is empty (root is None), the new node becomes the root.<\/li>\n\n\n\n<li>Otherwise, it calls the helper method __insert__recursive to insert the node recursively.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>__insert__recursive(self, node, data): A helper method to insert a node recursively.\n<ul>\n<li>If the given data is less than the current node&#8217;s value, it goes to the left subtree.<\/li>\n\n\n\n<li>If the given data is greater than or equal to the current node&#8217;s value, it goes to the right subtree.<\/li>\n\n\n\n<li>If the appropriate child node is None, it creates a new node with the given data.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>print_tree(self, node, level=0): Prints the binary tree in a structured format.\n<ul>\n<li>It recursively prints the right subtree, the current node, and then the left subtree.<\/li>\n\n\n\n<li>Uses indentation to represent the level of the tree.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p># Main function<\/p>\n\n\n\n<p>if __name__ == &#8220;__main__&#8221;:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Creating the binary tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree = SumanBinaryTree()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root = Node(10)&nbsp; # Root node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left = Node(20)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right = Node(30)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 2<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.left = Node(40)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.right = Node(50)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.left = Node(60)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right = Node(70)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 3<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right.left = Node(80)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right.right = Node(90)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Print the binary tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.print_tree(tree.root)<\/p>\n\n\n\n<p><strong>Description<\/strong>:<\/p>\n\n\n\n<ul>\n<li><strong>Purpose<\/strong>: This block of code creates a binary tree and demonstrates the insertion and printing of nodes.<br><\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>Tree Construction<\/strong>:\n<ul>\n<li>A SumanBinaryTree object tree is created.<\/li>\n\n\n\n<li>The root node is initialized with a value of 10.<\/li>\n\n\n\n<li>Nodes are inserted manually to build the tree with values at different levels.<\/li>\n\n\n\n<li>Level 1 nodes: 20 (left) and 30 (right).<\/li>\n\n\n\n<li>Level 2 nodes: 40 (left-left), 50 (left-right), 60 (right-left), 70 (right-right).<\/li>\n\n\n\n<li>Level 3 nodes: 80 (right-right-left), 90 (right-right-right).<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Printing the Tree<\/strong>:\n<ul>\n<li>The print_tree method is called to print the structure of the binary tree.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>The Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdzOWADgokdZMDOv6rc5JjOdu1n35hsOY87Fu5qwFGKama7PPaR9Ciy4SRjQw7OPwv9y0CQ0rvKt9Y6GjUbiZpUQ6dmCzJGlyuKITmTSD7uRdGIGuba-mmQvBgKCLiWwFeU8_ZNJw?key=Jnc31SKj7Xkp7Qe2VaEbQhVa\" alt=\"The Output\" title=\"\"><\/figure>\n\n\n\n<p>This format helps visualize the hierarchical structure of the binary tree, showing the relationships between parent and child nodes at different levels.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Updating Nodes in a Binary Tree<\/strong><\/h2>\n\n\n\n<p>Updating a node in a binary tree involves modifying the value of an existing node while ensuring the tree\u2019s structural integrity and properties remain intact. This operation begins with locating the target node using a traversal method such as in-order, pre-order, or post-order traversal. Once the node is identified, its value is updated with the new data. It\u2019s crucial to ensure that this update does not disrupt the binary tree\u2019s properties. For instance, in a binary search tree (BST), the left subtree must contain nodes with values less than the parent node, and the right subtree must contain nodes with values greater. After updating a node\u2019s value, maintaining these properties might require additional adjustments, such as reordering or rebalancing the tree.<\/p>\n\n\n\n<p><em>Now, before we dive deeper into the coding aspects of node updates, if you&#8217;re looking to build a strong foundation in Python and data structures, <strong>learning Python from scratch<\/strong> is a great place to start. Check out HCL GUVI\u2019s <a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=crud_operations\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=crud_operations\" target=\"_blank\" rel=\"noreferrer noopener\">Python course<\/a> which will guide you through everything from basic operations to complex implementations, including tree structures and beyond. You can enroll directly here to start your journey.<\/em><\/p>\n\n\n\n<p>Below is a given Python code which will demonstrate the updation of a Binary Tree node. Here, we are using Python to create a Binary Tree whose Root Node is 10. Under Root Node there will be two child nodes 20 and 30. Now, insert node 40 and 50 inside node 20 and 60 and 70 inside node 30.&nbsp; Create a method that can edit a particular node just by giving the old node data and new node data.<\/p>\n\n\n\n<p>&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>Using Python Create a Binary Tree whose Root Node is 10.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Under Root Node there will be two child nodes 20 and 30.<\/p>\n\n\n\n<p>Now, insert node 40 and 50 inside node 20 and 60 and 70 inside node 30.<\/p>\n\n\n\n<p>Create a method that can edit a particular node just by giving the old node data and new node data.<\/p>\n\n\n\n<p>&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>class Node:&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, val):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.val = val<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.right = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.left = None<\/p>\n\n\n\n<p>class SumanBinaryTree:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def insert_node(self, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.root is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(self.root, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __insert__recursive(self, node, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if data &lt; node.val:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.left is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.left = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.left, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.right is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.right = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.right, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def print_tree(self, node, level=0):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node is not None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.right, level + 1)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8216; &#8216; * 4 * level + &#8216;-&gt;&#8217;, node.val)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.left, level + 1)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def edit_node(self, node, old_data, new_data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return False<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Check if the current node contains the data to be edited<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.val == old_data:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.val = new_data<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return True<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Recursively check left and right subtrees<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.edit_node(node.left, old_data, new_data) or self.edit_node(node.right, old_data, new_data)<\/p>\n\n\n\n<p>if __name__ == &#8220;__main__&#8221;:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Creating the binary tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree = SumanBinaryTree()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root = Node(10)&nbsp; # Root node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left = Node(20)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right = Node(30)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 2<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.left = Node(40)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.right = Node(50)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.left = Node(60)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right = Node(70)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Print tree before editing<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Tree before editing:&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.print_tree(tree.root)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Edit node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;old_data = 40<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;new_data = 100<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if tree.edit_node(tree.root, old_data, new_data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f&#8221;Node with value {old_data} was edited to {new_data}.&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(f&#8221;Node with value {old_data} was not found.&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Print tree after editing<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Tree after editing:&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.print_tree(tree.root)<\/p>\n\n\n\n<p><strong>Class Node:<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>Purpose:<\/strong> Represents a single node in the binary tree.<\/li>\n\n\n\n<li><strong>Attributes:<\/strong>\n<ul>\n<li>val: Stores the data value of the node.<\/li>\n\n\n\n<li>left: Points to the left child node.<\/li>\n\n\n\n<li>right: Points to the right child node.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Class SumanBinaryTree:<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>Purpose:<\/strong> Manages the binary tree and provides operations for insertion, printing, and editing nodes.<\/li>\n\n\n\n<li><strong>Methods:<\/strong>\n<ul>\n<li><strong>__init__(self)<\/strong><strong>:<\/strong> Initializes an empty binary tree by setting the root node to None.<\/li>\n\n\n\n<li><strong>insert_node(self, data)<\/strong><strong>:<\/strong>\n<ul>\n<li>If the tree is empty, it creates a new root node with the given data.<\/li>\n\n\n\n<li>Otherwise, call the recursive __insert__recursive method to find the appropriate position for the new node based on its value.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>__insert__recursive(self, node, data)<\/strong><strong>:<\/strong>\n<ul>\n<li>Recursively traverses the tree to find the correct position for the new node.<\/li>\n\n\n\n<li>If the new data is less than the current node&#8217;s data, it&#8217;s inserted into the left subtree.<\/li>\n\n\n\n<li>If the new data is greater than or equal to the current node&#8217;s data, it&#8217;s inserted into the right subtree.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>print_tree(self, node, level=0)<\/strong><strong>:<\/strong>\n<ul>\n<li>Recursively traverses the tree and prints each node&#8217;s value with appropriate indentation.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>edit_node(self, node, old_data, new_data)<\/strong><strong>:<\/strong>\n<ul>\n<li>Recursively searches for the node with the old_data value.<\/li>\n\n\n\n<li>If found, update the node&#8217;s value to new_data.<\/li>\n\n\n\n<li>Returns True if the node is edited, False otherwise.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Main Execution:<\/strong><\/p>\n\n\n\n<ol>\n<li><strong>Tree Creation:<\/strong>\n<ul>\n<li>A binary tree is created with the root node having the value 10.<\/li>\n\n\n\n<li>Child nodes are added to create a binary tree structure.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Tree Printing:<\/strong>\n<ul>\n<li>The print_tree method is used to display the tree structure before editing.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Node Editing:<\/strong>\n<ul>\n<li>The edit_node method is called to find and modify the node with the value 40 to 100.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Tree Printing (Post-Edit):<\/strong>\n<ul>\n<li>The print_tree method is called again to display the modified tree structure.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>The Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeel8hD0a0caIcrmMVHDz1JEQUyurR73e_0TcZccvsjYvRY36LGrAHkJ8CkhyNdkjQwKwhcFS9YnaG3ZllE_wrRyEx30kzeVqeK5Mk3a25bXwAxdwg4UI8By2PO4BssI6T87u4hlg?key=Jnc31SKj7Xkp7Qe2VaEbQhVa\" alt=\"The Output\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Deleting a Node from a Binary Tree<\/strong><\/h2>\n\n\n\n<p>Deleting a node from a binary tree involves three primary cases: removing a leaf node, removing a node with one child, and removing a node with two children. In the first case, the node is simply removed. In the second case, the node is replaced by its child. The third case, which is the most complex, involves finding the node&#8217;s in-order predecessor or successor to replace the deleted node while maintaining the tree&#8217;s structure. This ensures the binary tree&#8217;s properties are preserved. Efficiently handling deletions is crucial for maintaining the tree\u2019s balance and performance.<\/p>\n\n\n\n<p>Below is a given Python code which will demonstrate the updation of a Binary Tree node. Here, we are using Python to create a Binary Tree Using Python Create a Binary Tree whose Root Node is 10. Under Root Node there will be two child nodes 20 and 30. Now, insert node 40 and 50 inside node 20 and 60 and 70 inside node 30. Create a Delete method that will delete&nbsp; a particular node when it exists inside the tree.<\/p>\n\n\n\n<p>&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>Using Python Create a Binary Tree whose Root Node is 10.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Under Root Node there will be two child nodes 20 and 30.<\/p>\n\n\n\n<p>Now, insert node 40 and 50 inside node 20 and 60 and 70 inside node 30.<\/p>\n\n\n\n<p>Create a Delete method that will delete&nbsp; a particular node when it exists<\/p>\n\n\n\n<p>inside the tree<\/p>\n\n\n\n<p>&#8220;&#8221;&#8221;<\/p>\n\n\n\n<p>class Node:&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, val):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.val = val<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.right = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.left = None<\/p>\n\n\n\n<p>class SumanBinaryTree:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = None<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def insert_node(self, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if self.root is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(self.root, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __insert__recursive(self, node, data):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if data &lt; node.val:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.left is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.left = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.left, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.right is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.right = Node(data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.__insert__recursive(node.right, data)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def print_tree(self, node, level=0):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node is not None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.right, level + 1)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#8216; &#8216; * 4 * level + &#8216;-&gt;&#8217;, node.val)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.print_tree(node.left, level + 1)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def find_min(self, node):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current = node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while current.left is not None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;current = current.left<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return current<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def delete_node(self, node, key):<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Traverse to find the node to delete<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if key &lt; node.val:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.left = self.delete_node(node.left, key)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif key &gt; node.val:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.right = self.delete_node(node.right, key)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Case 1: Node with only one child or no child<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if node.left is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return node.right<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif node.right is None:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return node.left<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Case 2: Node with two children, get the inorder successor<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;min_node = self.find_min(node.right)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.val = min_node.val<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;node.right = self.delete_node(node.right, min_node.val)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return node<\/p>\n\n\n\n<p>if __name__ == &#8220;__main__&#8221;:<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Creating the binary tree<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree = SumanBinaryTree()<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root = Node(10)&nbsp; # Root node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 1<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left = Node(20)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right = Node(30)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Level 2<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.left = Node(40)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.left.right = Node(50)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.left = Node(60)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root.right.right = Node(70)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Print tree before deletion<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Tree before deletion:&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.print_tree(tree.root)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Delete a node<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;key_to_delete = 30<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.root = tree.delete_node(tree.root, key_to_delete)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(f&#8221;\\nNode with value {key_to_delete} was deleted.\\n&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;# Print tree after deletion<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;print(&#8220;Tree after deletion:&#8221;)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;tree.print_tree(tree.root)<\/p>\n\n\n\n<p>This Python code implements a binary search tree (BST) data structure with basic operations like insertion, deletion, and printing.<\/p>\n\n\n\n<p><strong>Key Classes and Methods:<\/strong><\/p>\n\n\n\n<ol>\n<li><strong>Node Class:<\/strong><strong><br><\/strong>\n<ul>\n<li>Represents a single node in the binary tree.<\/li>\n\n\n\n<li>Stores the node&#8217;s value (val), left child (left), and right child (right).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>SumanBinaryTree Class:<\/strong><strong><br><\/strong>\n<ul>\n<li><strong>insert_node(data)<\/strong><strong>:<\/strong>\n<ul>\n<li>Inserts a new node with the given data into the tree.<\/li>\n\n\n\n<li>If the tree is empty, creates a new root node.<\/li>\n\n\n\n<li>Otherwise, recursively traverses the tree to find the appropriate position for the new node based on its value.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>print_tree(node, level=0)<\/strong><strong>:<\/strong>\n<ul>\n<li>Recursively prints the tree in a visually appealing format, with indentation representing the tree&#8217;s levels.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>find_min(node)<\/strong><strong>:<\/strong>\n<ul>\n<li>Finds the minimum value node in the subtree rooted at the given node.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>delete_node(node, key)<\/strong><strong>:<\/strong>\n<ul>\n<li>Deletes the node with the given key from the tree.<\/li>\n\n\n\n<li>Handles three cases:\n<ol>\n<li>Node to be deleted is not found.<\/li>\n\n\n\n<li>Node to be deleted has one or no child.<\/li>\n\n\n\n<li>Node to be deleted has two children. In this case, the in order successor (the smallest value in the right subtree) is found and used to replace the deleted node.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p><strong>Main Execution Block:<\/strong><\/p>\n\n\n\n<ul>\n<li>Creates a binary tree with the specified structure.<\/li>\n\n\n\n<li>Prints the tree before deletion.<\/li>\n\n\n\n<li>Deletes the node with the value 30.<\/li>\n\n\n\n<li>Prints the tree after deletion.<\/li>\n<\/ul>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>Binary Search Tree Property:<\/strong> The left subtree of a node contains values less than the node&#8217;s value, and the right subtree contains values greater than the node&#8217;s value.<\/li>\n<\/ul>\n\n\n\n<p><strong>The Output<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfd4R-Db7Szuw0I6QNCg2b9mTz9pQO9JU1bIibmE2YQpqFDANPXwdXohOrAjYo-b_7WzaG6QiLLqLQqkE1gQN04gF-uS-UUxwDSuMQ3i1GuhH6EaGx0eQZ1aVvz2ui1rdIifykdIg?key=Jnc31SKj7Xkp7Qe2VaEbQhVa\" alt=\"The Output\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Mastering CRUD operations in binary trees goes beyond just writing code, it\u2019s about understanding the logic behind each operation. Efficient insertion and retrieval techniques ensure fast data access, while proper update and delete methods maintain the tree\u2019s structural integrity. Whether you&#8217;re working on a simple project or designing a complex database system, knowing how to handle these operations can significantly improve your approach to data management.<\/p>\n\n\n\n<p>By implementing these techniques in Python, you can develop a solid foundation in tree-based algorithms and gain a deeper appreciation for their efficiency. As you continue exploring advanced concepts like balancing and traversal optimizations, these CRUD operations will serve as a strong base for building more sophisticated data structures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction What makes binary trees so crucial in computer science? If you\u2019ve ever worked with hierarchical data structures, you\u2019ve likely encountered them. From database indexing to expression parsing, binary trees provide an efficient way to store and manipulate data. Their ability to maintain order and optimize search operations makes them invaluable in various applications. But [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":71195,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717,37],"tags":[],"views":"4782","authorinfo":{"name":"Suman Gangopadhyay","url":"https:\/\/www.guvi.in\/blog\/author\/suman-gangopadhyay\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/CRUD-Operations-300x112.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/CRUD-Operations.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/71121"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=71121"}],"version-history":[{"count":8,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/71121\/revisions"}],"predecessor-version":[{"id":98482,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/71121\/revisions\/98482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/71195"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=71121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=71121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=71121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}