{"id":17219,"date":"2025-06-12T09:42:42","date_gmt":"2025-06-12T09:42:42","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=17219"},"modified":"2025-06-12T09:42:44","modified_gmt":"2025-06-12T09:42:44","slug":"a-binary-search-tree-is-a-data-structure-that-keeps-items-in-sorted-order","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/a-binary-search-tree-is-a-data-structure-that-keeps-items-in-sorted-order\/","title":{"rendered":"A binary search tree is a data structure that keeps items in sorted order"},"content":{"rendered":"\n<p>A binary search tree is a data structure that keeps items in sorted order.<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>The correct answer and explanation is:<\/strong><\/mark><\/p>\n\n\n\n<p><strong>Correct Answer:<\/strong><br>A <strong>Binary Search Tree (BST)<\/strong> is a data structure that stores elements (typically numbers or keys) in a hierarchical tree structure, where each node has at most two children. For each node:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The left child contains only nodes with values <strong>less than<\/strong> the parent node\u2019s value.<\/li>\n\n\n\n<li>The right child contains only nodes with values <strong>greater than<\/strong> the parent node\u2019s value.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Explanation (300 words):<\/strong><br>A Binary Search Tree (BST) is a specialized form of a binary tree that maintains sorted order among its elements. It is widely used in computer science for searching, sorting, and organizing data efficiently.<\/p>\n\n\n\n<p>In a BST, each node has three main parts: a value (key), a left child, and a right child. The key principle of BSTs is that for any given node:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>All values in the <strong>left subtree<\/strong> are smaller.<\/li>\n\n\n\n<li>All values in the <strong>right subtree<\/strong> are larger.<\/li>\n<\/ul>\n\n\n\n<p>This property allows for efficient operations. Searching in a BST starts at the root and proceeds either left or right, depending on the value being sought. On average, this search takes O(log n) time, where n is the number of nodes, making it much faster than linear search in an unsorted list.<\/p>\n\n\n\n<p>Insertion in a BST follows a similar path: we compare the new value with nodes from the root downward and place it as a new leaf where the correct position is found. Deletion is more complex and involves three cases:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The node is a leaf \u2013 simply remove it.<\/li>\n\n\n\n<li>The node has one child \u2013 replace it with its child.<\/li>\n\n\n\n<li>The node has two children \u2013 replace it with its in-order successor (smallest value in the right subtree) or in-order predecessor (largest value in the left subtree).<\/li>\n<\/ol>\n\n\n\n<p>A well-balanced BST keeps operations efficient. However, if elements are inserted in a sorted order without balancing (e.g., 1, 2, 3, 4&#8230;), the tree degenerates into a linked list, making operations O(n) in time complexity.<\/p>\n\n\n\n<p>To overcome this, <strong>self-balancing BSTs<\/strong> like AVL trees or Red-Black trees are often used, ensuring that the tree remains balanced for consistently fast operations.<\/p>\n\n\n\n<p>BSTs are foundational in algorithms, databases, and file systems due to their speed and efficiency.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A binary search tree is a data structure that keeps items in sorted order. The correct answer and explanation is: Correct Answer:A Binary Search Tree (BST) is a data structure that stores elements (typically numbers or keys) in a hierarchical tree structure, where each node has at most two children. For each node: Explanation (300 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17219","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/17219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=17219"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/17219\/revisions"}],"predecessor-version":[{"id":17221,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/17219\/revisions\/17221"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=17219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=17219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=17219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}