{"id":9022,"date":"2025-08-06T20:45:36","date_gmt":"2025-08-06T15:15:36","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=9022"},"modified":"2025-08-06T20:46:48","modified_gmt":"2025-08-06T15:16:48","slug":"binary-search-tree-introduction","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/binary-search-tree-introduction\/","title":{"rendered":"Binary Search Tree Introduction"},"content":{"rendered":"\n<!-- Prism.js for Code Highlighting -->\n<link\n  href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\"\n  rel=\"stylesheet\"\n\/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.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_theme {\n    --primary: #e58c32;\n    --secondary: #f8c291;\n    --light-bg: #fef9f4;\n    --text-dark: #2d2d2d;\n    --tab-radius: 12px;\n    --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);\n    --code-bg: #001f3f;\n    --code-text: #d4f1ff;\n  }\n\n  .wp_blog_container {\n    font-family: 'Segoe UI', sans-serif;\n    background: var(--light-bg);\n    margin: 0;\n    padding: 0;\n    color: var(--text-dark);\n  }\n\n  .wp_blog_main-heading {\n    text-align: center;\n    font-size: 2.4rem;\n    color: var(--primary);\n    margin-top: 2.5rem;\n    font-weight: bold;\n  }\n\n  .wp_blog_explanation {\n    max-width: 940px;\n    margin: 2rem auto;\n    padding: 2rem;\n    background: white;\n    border-radius: var(--tab-radius);\n    box-shadow: var(--shadow);\n  }\n\n  .wp_blog_explanation h2 {\n    font-size: 1.4rem;\n    color: var(--primary);\n    margin-bottom: 0.5rem;\n  }\n\n  .wp_blog_explanation h5 {\n    color: var(--primary);\n  }\n\n  .wp_blog_explanation p,\n  .wp_blog_explanation li {\n    font-size: 1.05rem;\n    line-height: 1.7;\n    margin: 0.5rem 0;\n  }\n\n  .wp_blog_explanation code {\n    background: #f9cea6;\n    color: #2d2d2d;\n    padding: 3px 6px;\n    border-radius: 4px;\n    font-family: 'Courier New', monospace;\n  }\n\n  .wp_blog_explanation img {\n    max-width: 100%;\n    border-radius: var(--tab-radius);\n    margin-top: 1rem;\n    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);\n  }\n\n  pre {\n    background: var(--code-bg);\n    color: var(--code-text);\n    padding: 1rem;\n    border-radius: var(--tab-radius);\n    overflow-x: auto;\n  }\n<\/style>\n\n<div class=\"wp_blog_container wp_blog_theme\">\n  <h1 class=\"wp_blog_main-heading\"><\/h1>\n\n  <div class=\"wp_blog_explanation\">\n    <h2>Binary Search Tree:<\/h2>\n    <ul>\n      <li>BST is a <strong>special type<\/strong> of Binary Tree with <code>atmost 2 childern<\/code>.<\/li>\n    <\/ul>\n\n    <ul>\n        <li><strong>For Every Node:<\/strong>\n        <ul>\n            <li>All nodes of left <code>subtree<\/code> have smaller value.<\/li>\n            <li>All node of <code>right subtree<\/code> have greater value.<\/li>\n        <\/ul>\n        <\/li>\n    <\/ul>\n    <!-- 50 -->\n    <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-2025-08-06-at-7.50.29\u202fPM.png\" alt=\"\">\n\n    <h2>Important Point to Note:<\/h2>\n    <!-- image complete-->\n    <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-2025-08-06-at-7.50.45\u202fPM.png\" alt=\"\">\n    <ul>\n      <li>All the <code>subtrees of a tree<\/code> should be BST.<\/li>\n      <li>A tree is a Binary Search Tree only if <code>every node follows the <strong>BST<\/strong> rules<\/code>.<\/li>\n    <\/ul>\n\n    <!-- Valid -->\n     <h2>Valid BST<\/h2>\n     <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-2025-08-06-at-8.06.40\u202fPM.png\" alt=\"\">\n    \n     <h2>Why it is called a Search Tree ?<\/h2>\n    <img decoding=\"async\" src=\"https:\/\/namastedev.com\/blog\/wp-content\/uploads\/2025\/08\/Screenshot-2025-08-06-at-8.37.05\u202fPM.png\" alt=\"\">\n    <h3>Search the value 75:<\/h3>\n    <ul> \n        <li>Here, we are finding it in a very easy way \u2014 <code>we don\u2019t need to check unnecessary nodes<\/code>.<\/li>\n        <li>At every iteration, the search space is reduced by <code>half (n\/2)<\/code>.<\/li>\n        <li>This makes searching <strong>very efficient<\/strong>, which is why it is called a <code>Binary Search Tree<\/code>.<\/li>\n        <li><strong>Time Complexity: <\/strong><code>O(log n)<\/code> As log n is the height of the tree.<\/li>\n    <\/ul>\n\n    <h3>Let&#8217;s try <strong>Inorder Traversal (left-node-right)<\/strong> of the tree:<\/h3>\n    <p><strong>10, 20, 25, 30, 35, 40, 45, 50, 60, 70, 75, 80, 85<\/strong><\/p>\n    <p><code>Note: Inorder-traversal of BST is always sorted.<\/code><\/p>\n  <\/div>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>Binary Search Tree: BST is a special type of Binary Tree with atmost 2 childern. For Every Node: All nodes of left subtree have smaller value. All node of right subtree have greater value. Important Point to Note: All the subtrees of a tree should be BST. A tree is a Binary Search Tree only<\/p>\n","protected":false},"author":108,"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":[210,322,260,176,175,211,811,810,174,172,173],"tags":[],"class_list":["post-9022","post","type-post","status-publish","format-standard","category-algorithms","category-algorithms-and-data-structures","category-c-c-plus-plus","category-csharp","category-cplusplus","category-data-structures","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/9022","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\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=9022"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/9022\/revisions"}],"predecessor-version":[{"id":9060,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/9022\/revisions\/9060"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=9022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=9022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=9022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}