{"id":10235,"date":"2025-10-13T10:38:19","date_gmt":"2025-10-13T05:08:19","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10235"},"modified":"2025-10-13T10:48:57","modified_gmt":"2025-10-13T05:18:57","slug":"binary-search-tree-introduction-2","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/binary-search-tree-introduction-2\/","title":{"rendered":"Binary Search Tree &#8211; Introduction"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n  }\n\n  .wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n  }\n\n  .wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content > pre {\n    background: #242b33 !important;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block !important;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n  }\n\n  .wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n  }\n\n  .wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n  }\n\n  .wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n  }\n\n  .wp_blog_explanation h1,\n  .wp_blog_explanation h2,\n  .wp_blog_explanation h3,\n  .wp_blog_explanation h4,\n  .wp_blog_explanation h5,\n  .wp_blog_explanation h6,\n  .wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n  }\n<\/style>\n\n<div class=\"wp_blog_explanation\">\n    <p>A <strong>Binary Search Tree (BST)<\/strong> is a binary tree where each node follows the following properties:<\/p>\n    <ul>\n      <li>The left child contains only nodes with values <strong>less than<\/strong> the parent node.<\/li>\n      <li>The right child contains only nodes with values <strong>greater than<\/strong> the parent node.<\/li>\n      <li>Both the left and right subtrees must also be binary search trees.<\/li>\n    <\/ul>\n  \n    <h2>Why is it Called a Search Tree?<\/h2>\n    <p>It is called a &#8220;Search Tree&#8221; because it allows efficient search operations using the tree structure. If the tree is balanced, searching has a time complexity of:<\/p>\n    <p><code>O(log n)<\/code><\/p>\n  \n    <h2>Inorder Traversal<\/h2>\n    <p>If we perform an <strong>inorder traversal<\/strong> (left \u2192 node \u2192 right), the values we get are in sorted order.<\/p>\n    <p><strong>Example Tree:<\/strong><\/p>\n    <pre><code>\n           50\n         \/    \\\n       30      70\n      \/  \\    \/  \\\n     20  40  60  80\n    \/ \\  \/ \\     \/ \\\n  10 25 35 45  75  85\n    <\/code><\/pre>\n    <p><strong>Inorder Traversal:<\/strong> <code>10 20 25 30 35 40 45 50 60 70 75 80 85<\/code> (Sorted)<\/p>\n  \n    <h2>Time &#038; Space Complexity<\/h2>\n    <ul>\n      <li><strong>Search (Balanced BST):<\/strong> <code>O(log n)<\/code><\/li>\n      <li><strong>Traversal:<\/strong> <code>O(n)<\/code><\/li>\n      <li><strong>Space:<\/strong> <code>O(h)<\/code> (Recursive call stack, where h = height)<\/li>\n    <\/ul>\n  <\/div>\n  \n\n  <div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c#\">C#<\/button>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\n  \/\/ Inorder Traversal of BST\n  function inorderTraversal(root) {\n      if (!root) return;\n      inorderTraversal(root.left);\n      console.log(root.val);\n      inorderTraversal(root.right);\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n  void inorderTraversal(TreeNode* root) {\n      if (!root) return;\n      inorderTraversal(root->left);\n      cout << root->val << \" \";\n      inorderTraversal(root->right);\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n  void inorderTraversal(struct TreeNode* root) {\n      if (root == NULL) return;\n      inorderTraversal(root->left);\n      printf(\"%d \", root->val);\n      inorderTraversal(root->right);\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\n  void inorderTraversal(TreeNode root) {\n      if (root == null) return;\n      inorderTraversal(root.left);\n      System.out.print(root.val + \" \");\n      inorderTraversal(root.right);\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\n  def inorderTraversal(root):\n      if not root:\n          return\n      inorderTraversal(root.left)\n      print(root.val)\n      inorderTraversal(root.right)\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c#\">\n      <pre><code class=\"language-csharp\">\n  void InorderTraversal(TreeNode root) {\n      if (root == null) return;\n      InorderTraversal(root.left);\n      Console.Write(root.val + \" \");\n      InorderTraversal(root.right);\n  }\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n  \n\n  <a href=\"https:\/\/leetcode.com\/problems\/sqrtx\/description\/\" target=\"blank\">Solve this problem.<\/a>\n  <script>\n  document.addEventListener(\"DOMContentLoaded\", function () {\n    const buttons = document.querySelectorAll(\".wp_blog_code-tab-button\");\n    const contents = document.querySelectorAll(\".wp_blog_code-tab-content\");\n\n    buttons.forEach((button) => {\n      button.addEventListener(\"click\", () => {\n        const lang = button.getAttribute(\"data-lang\");\n\n        buttons.forEach((btn) => btn.classList.remove(\"active\"));\n        button.classList.add(\"active\");\n\n        contents.forEach((content) => {\n          content.classList.toggle(\"active\", content.getAttribute(\"data-lang\") === lang);\n        });\n      });\n    });\n  });\n<\/script>\n\n\n","protected":false},"excerpt":{"rendered":"<p>A Binary Search Tree (BST) is a binary tree where each node follows the following properties: The left child contains only nodes with values less than the parent node. The right child contains only nodes with values greater than the parent node. Both the left and right subtrees must also be binary search trees. Why<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[811],"tags":[],"class_list":{"0":"post-10235","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-data-structures-and-algorithms"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10235"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10235\/revisions"}],"predecessor-version":[{"id":10236,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10235\/revisions\/10236"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}