{"id":70959,"date":"2025-01-29T12:40:19","date_gmt":"2025-01-29T07:10:19","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=70959"},"modified":"2026-01-07T13:46:45","modified_gmt":"2026-01-07T08:16:45","slug":"unearthing-even-gems-in-a-binary-tree-a-python-adventure","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/unearthing-even-gems-in-a-binary-tree-a-python-adventure\/","title":{"rendered":"Unearthing Even Gems in a Binary Tree: A Python Adventure"},"content":{"rendered":"\n<p>What if your binary tree holds hidden treasures, even gems waiting to be discovered? Imagine finding all the even-numbered nodes, regardless of their position, and storing them in a list for easy access.&nbsp;<\/p>\n\n\n\n<p>This blog takes you on an engaging journey where Python becomes your trusty sidekick to unearth these even gems within a binary tree. Let&#8217;s dive into the quest!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Quest: Building Our Binary Tree<\/strong><\/h2>\n\n\n\n<p>Before we dive into finding the even-numbered nodes, let&#8217;s set up our binary tree as specified in the problem statement. The code for the Binary tree is as follows :&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\nUsing Python write a python to Create a Binary Tree whose Root Node is 252.\nUnder Root Node there will be two child nodes 21 and 56.\nNow, insert node 49 and 98 inside node 21 and 189 and 1 inside node 56.\nUnder node 1 insert node 22 and 41 and under node 49 insert node 102 and 103.\nFind all the EVEN number nodes and save it inside a list\nwhether it is present in left side or right side from the binary tree.\n\"\"\"\n\n\nclass Node:\n    def __init__(self, val):\n        self.val = val\n        self.right = None\n        self.left = None\n\n\nclass SumanBinaryTree:\n    def __init__(self):\n        self.root = None\n\n\n    def insert_node(self, data):\n        if self.root is None:\n            self.root = Node(data)\n        else:\n            self.__insert__recursive(self.root, data)\n\n\n    def __insert__recursive(self, node, data):\n        if data &lt; node.val:\n            if node.left is None:\n                node.left = Node(data)\n            else:\n                self.__insert__recursive(node.left, data)\n        else:\n            if node.right is None:\n                node.right = Node(data)\n            else:\n                self.__insert__recursive(node.right, data)\n\n\n    def print_tree(self, node, level=0):\n        if node is not None:\n            self.print_tree(node.right, level + 1)\n            print(' ' * 4 * level + '-&gt;', node.val)\n            self.print_tree(node.left, level + 1)\n\n\n    def find_even_nodes(self, node, even_nodes):\n        if node is not None:\n            if node.val % 2 == 0:\n                even_nodes.append(node.val)\n            self.find_even_nodes(node.left, even_nodes)\n            self.find_even_nodes(node.right, even_nodes)\n\n\n# Create the binary tree\ntree = SumanBinaryTree()\ntree.insert_node(252)\ntree.insert_node(21)\ntree.insert_node(56)\ntree.insert_node(49)\ntree.insert_node(98)\ntree.insert_node(189)\ntree.insert_node(1)\ntree.insert_node(22)\ntree.insert_node(41)\ntree.insert_node(102)\ntree.insert_node(103)\n\n\n# Find even nodes\neven_nodes = &#91;]\ntree.find_even_nodes(tree.root, even_nodes)\n\n\nprint(\"Even nodes:\", even_nodes)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Define the Node Class<\/strong><\/h3>\n\n\n\n<p>The Node class represents a single node in our binary tree.<\/p>\n\n\n\n<ul>\n<li>__init__<strong> method:<\/strong> The constructor initializes a node with a value (val) and sets the left and right children to None.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Define the SumanBinaryTree Class<\/strong><\/h3>\n\n\n\n<p>The SumanBinaryTree class represents the binary tree and contains methods to insert nodes, print the tree, and find even nodes.<\/p>\n\n\n\n<ul>\n<li>__init__<strong> method:<\/strong> Initializes the root of the tree to None.<\/li>\n\n\n\n<li>insert_node<strong> method:<\/strong> Inserts a new node into the tree. If the tree is empty (root is None), it creates the root node. Otherwise, it calls the private method __insert__recursive to insert the node recursively.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Recursive Insertion<\/strong><\/h2>\n\n\n\n<p>The __insert__recursive method handles the recursive insertion of nodes.<\/p>\n\n\n\n<p>If the data is less than the current node&#8217;s value (node.val), it attempts to insert it into the left subtree. If the left child is None, it creates a new node there. Otherwise, it recurses into the left child.<\/p>\n\n\n\n<p>Similarly, if the data is greater than or equal to the current node&#8217;s value, it follows the same logic for the right subtree.<\/p>\n\n\n\n<p><em>Ready to master Python and unlock endless career opportunities in tech? HCL GUVI\u2019s <strong><a href=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=Unearthing+Even+Gems+in+a+Binary+Tree%3A+A+Python+Adventure\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/zen-class\/python-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=Unearthing+Even+Gems+in+a+Binary+Tree%3A+A+Python+Adventure\" target=\"_blank\" rel=\"noreferrer noopener\">Python Course<\/a><\/strong> is designed for learners of all levels, offering a comprehensive curriculum, hands-on projects, and expert mentorship. This course is designed to equip you with the knowledge and tools to excel in fields like AI, Data Science, and Web Development. Take the first step towards transforming your career with Python!<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Through this exploration, we\u2019ve built a binary tree, traversed its depths, and uncovered its hidden treasures: the even-numbered nodes. By combining recursive functions and <a href=\"https:\/\/www.guvi.in\/blog\/advantages-of-learning-python-regional-languages\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/advantages-of-learning-python-regional-languages\/\" rel=\"noreferrer noopener\">Python<\/a>&#8216;s versatility, you now have a powerful method to extract these &#8220;gems&#8221; effortlessly.&nbsp;<\/p>\n\n\n\n<p>Beyond this specific problem, the techniques discussed here can be extended to tackle numerous binary tree challenges, making you a more confident coder. So, keep exploring, keep coding, and let<a href=\"https:\/\/www.python.org\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.python.org\/\" rel=\"noreferrer noopener\"> Python <\/a>guide you in uncovering the secrets hidden within data structures!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1737966161830\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What is a binary tree, and why is it important?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A binary tree is a hierarchical data structure where each node has at most two children (referred to as left and right). It is widely used for efficient searching, sorting, and hierarchical data representation.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1737966216637\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">2. <strong>Why is the largest number set as the root in this example?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Even gems refer to all the even-numbered nodes present in a binary tree, regardless of their position.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1737966249353\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">3. <strong>\u00a0Why should I learn about binary trees?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Binary trees are fundamental to many computer science concepts and are widely used in applications like searching, sorting, and hierarchical data storage.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1737966278130\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">4. <strong>How are binary trees different from binary search trees?<\/strong><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A binary tree allows any structure, while a binary search tree (BST) follows a specific ordering property: left child &lt; root &lt; right child.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>What if your binary tree holds hidden treasures, even gems waiting to be discovered? Imagine finding all the even-numbered nodes, regardless of their position, and storing them in a list for easy access.&nbsp; This blog takes you on an engaging journey where Python becomes your trusty sidekick to unearth these even gems within a binary [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":70981,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[717,37],"tags":[],"views":"1785","authorinfo":{"name":"Suman Gangopadhyay","url":"https:\/\/www.guvi.in\/blog\/author\/suman-gangopadhyay\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/Binary-tree-300x112.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/Binary-tree.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/70959"}],"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=70959"}],"version-history":[{"count":5,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/70959\/revisions"}],"predecessor-version":[{"id":98487,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/70959\/revisions\/98487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/70981"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=70959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=70959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=70959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}