{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/binarySearchTree","result":{"data":{"blog":{"frontmatter":{"title":"BINARY SEARCH TREES","thumbnail":"blog40","date":"January 14, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1GTOWOGXEMo9f-z1E8Dgqe5xK56FYBC6w/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is are binary search trees,\n                applications, operations and C++ code implementation for BST.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Defintion</h4>\n              <div class=\"m-2\">\n                <p>\n                  A binary tree in which all the values of left sub-tree nodes\n                  are less than values of right sub-trees is called as binary\n                  search tree.\n                </p>\n                <p>\n                  In binary search trees, all the left subtree elements should\n                  be less than root data and all the right subtree elements\n                  should be greater than root data. Both the left and right\n                  subtrees must also be binary search trees. This is called\n                  binary search tree property.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>Used for indexing and multi-indexing.</li>\n                <li>Used to implement various sorting algorigthms.</li>\n                <li>\n                  TreeMap and TreeSet data structures are internally implemented\n                  using self-balancing BSTs.\n                </li>\n                <li>\n                  The inOrder traversal itself gives the sorted order of the\n                  elements in BST.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Standard Operations</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following thoery.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Traversal:</strong>\n                  <ol class=\"pl-3\">\n                    <li>\n                      Inorder Traversal(Left,Root,Right): Traverse the left\n                      sub-tree in Inorder, Visit the Root, Traverse the right\n                      sub-tree in Inorder.\n                    </li>\n                    <li>\n                      Postorder Traversal(Left,Right,Root): Traverse the left\n                      sub-tree in Postorder, Traverse the right sub-tree in\n                      Postorder, Visit the Root.\n                    </li>\n                    <li>\n                      Preorder Traversal(Root,Left,Right): Visit the Root,\n                      Traverse the left sub-tree in Preorder, Traverse the right\n                      sub-tree in Preorder.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Insertion: </strong>If root is null then return the\n                  new node. Else assign root to a temporary variable called\n                  child node and keep a pointer to refer the parent node. <br />\n                  Loop until child node is not NULL. First assign child node to\n                  previous node and then compare the values of child node and\n                  new node.<br />\n                  If child node's value is greater than new node's value then\n                  assign child node's left pointer to child node(traverse left\n                  sub-tree). Else assign child node's right pointer to child\n                  node(traverse right sub-tree). <br />\n                  After looping, if previous node's value is greater than new\n                  node's value then assign previous node's left pointer to new\n                  node else assign right pointer to new node.\n                </li>\n                <li>\n                  <strong>Search: </strong>\n                  If root's data is equal to data to be searched then return\n                  root.\n                  <br />\n                  If root's data is greater than data to be searched then\n                  traverse left sub-tree.\n                  <br />\n                  If root's data is smaller than data to be searched then\n                  traverse right sub-tree.\n                </li>\n                <li>\n                  <strong>Deletion: </strong>\n                  If root's data is equal to data to be deleted and root has\n                  child nodes then we can't perform delete operation. If root's\n                  data is equal to data to be deleted and root has no child\n                  nodes then assign NULL value to root and return.\n                  <br />\n                  Else find the node to be deleted and it's parent using search\n                  operation. If it is a leaf node then check whether this node\n                  is parent's left or right child and assign NULL to that\n                  particular pointer of parent's node.\n                  <br />\n                  Else if the node isn't a leaf and has only one child then\n                  check which child(either left or right) node exists and assign\n                  that to parent's left or right node(depending on whether the\n                  node to be deleted is parent's left or right child).\n                  <br />\n                  Else if the node isn't a leaf and has both the child then find\n                  the inorder successor(minimum value in right sub-tree) of the\n                  node to be deleted. Assign it's value to the node to be\n                  deleted and delete the inorder successor.\n                  <br />\n                  <p class=\"text-muted\">\n                    Deletion operation sounds clumsy but it's easy once\n                    understood the coding part.\n                  </p>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/tree\">Tree Data Structure</a> |\n              <a href=\"/blogs/binaryTree\">Binary Trees</a> |\n              <a href=\"/blogs/binaryHeap\">Binary Heap</a>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <p>\n                In this blog post will study what is are binary search trees,\n                applications, operations and C++ code implementation for BST.\n              </p>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Defintion</h4>\n              <div class=\"m-2\">\n                <p>\n                  A binary tree in which all the values of left sub-tree nodes\n                  are less than values of right sub-trees is called as binary\n                  search tree.\n                </p>\n                <p>\n                  In binary search trees, all the left subtree elements should\n                  be less than root data and all the right subtree elements\n                  should be greater than root data. Both the left and right\n                  subtrees must also be binary search trees. This is called\n                  binary search tree property.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Applications</h4>\n              <ul class=\"pl-4\">\n                <li>Used for indexing and multi-indexing.</li>\n                <li>Used to implement various sorting algorigthms.</li>\n                <li>\n                  TreeMap and TreeSet data structures are internally implemented\n                  using self-balancing BSTs.\n                </li>\n                <li>\n                  The inOrder traversal itself gives the sorted order of the\n                  elements in BST.\n                </li>\n              </ul>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Standard Operations</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following thoery.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  <strong>Traversal:</strong>\n                  <ol class=\"pl-3\">\n                    <li>\n                      Inorder Traversal(Left,Root,Right): Traverse the left\n                      sub-tree in Inorder, Visit the Root, Traverse the right\n                      sub-tree in Inorder.\n                    </li>\n                    <li>\n                      Postorder Traversal(Left,Right,Root): Traverse the left\n                      sub-tree in Postorder, Traverse the right sub-tree in\n                      Postorder, Visit the Root.\n                    </li>\n                    <li>\n                      Preorder Traversal(Root,Left,Right): Visit the Root,\n                      Traverse the left sub-tree in Preorder, Traverse the right\n                      sub-tree in Preorder.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Insertion: </strong>If root is null then return the\n                  new node. Else assign root to a temporary variable called\n                  child node and keep a pointer to refer the parent node. <br />\n                  Loop until child node is not NULL. First assign child node to\n                  previous node and then compare the values of child node and\n                  new node.<br />\n                  If child node's value is greater than new node's value then\n                  assign child node's left pointer to child node(traverse left\n                  sub-tree). Else assign child node's right pointer to child\n                  node(traverse right sub-tree). <br />\n                  After looping, if previous node's value is greater than new\n                  node's value then assign previous node's left pointer to new\n                  node else assign right pointer to new node.\n                </li>\n                <li>\n                  <strong>Search: </strong>\n                  If root's data is equal to data to be searched then return\n                  root.\n                  <br />\n                  If root's data is greater than data to be searched then\n                  traverse left sub-tree.\n                  <br />\n                  If root's data is smaller than data to be searched then\n                  traverse right sub-tree.\n                </li>\n                <li>\n                  <strong>Deletion: </strong>\n                  If root's data is equal to data to be deleted and root has\n                  child nodes then we can't perform delete operation. If root's\n                  data is equal to data to be deleted and root has no child\n                  nodes then assign NULL value to root and return.\n                  <br />\n                  Else find the node to be deleted and it's parent using search\n                  operation. If it is a leaf node then check whether this node\n                  is parent's left or right child and assign NULL to that\n                  particular pointer of parent's node.\n                  <br />\n                  Else if the node isn't a leaf and has only one child then\n                  check which child(either left or right) node exists and assign\n                  that to parent's left or right node(depending on whether the\n                  node to be deleted is parent's left or right child).\n                  <br />\n                  Else if the node isn't a leaf and has both the child then find\n                  the inorder successor(minimum value in right sub-tree) of the\n                  node to be deleted. Assign it's value to the node to be\n                  deleted and delete the inorder successor.\n                  <br />\n                  <p class=\"text-muted\">\n                    Deletion operation sounds clumsy but it's easy once\n                    understood the coding part.\n                  </p>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <strong>Similar posts:</strong>\n              <a href=\"/blogs/tree\">Tree Data Structure</a> |\n              <a href=\"/blogs/binaryTree\">Binary Trees</a> |\n              <a href=\"/blogs/binaryHeap\">Binary Heap</a>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M38%2092l-1%2010v8h11v-9l2%204c1%204%202%205%204%205s3-1%203-10v-9h-2c-3%200-3%200-3%203v4l-1-4-4-3c-2%200-3%201-3%203-1%203-1%203-1%200%200-4%200-4-2-4s-3%201-3%202m27-1c-1%201-3%203-4%208-4%209-4%2011%200%2011l2-1%201-2%202-1c2%200%202%200%202%202%200%201%201%202%203%202s3-1%201-11c-2-7-4-9-7-8m14%200h-4v6c-2%2011-1%2013%201%2013s3-1%203-3c0-4%201-4%203%200%201%203%202%204%204%201v-4c-1-1-1-3%201-5%203-5%200-9-8-8m51%200h-5v13l-2-3c-3-3-3-3-1-3l2-3c0-3-1-4-5-4-7%200-9%205-4%2010%204%204%204%205%200%204-4%200-5%203-1%204%204%202%207%201%209-2l2-2v2c0%202%200%203%203%203%206%200%209-1%209-3s-1-2-3-2l-4-1c-1-1%201-2%204-2l2-2c0-2-1-2-3-2-4%200-4-2%201-3%203%200%204-1%204-3l-2-2-6%201m16%200c-1%201-3%203-4%208-4%209-4%2011%200%2011l2-1%201-2%202-1c2%200%202%200%202%202%200%201%201%202%203%202s3-1%201-11c-2-7-4-9-7-8m14%200h-4v6c-2%2011-1%2013%201%2013s3-1%203-3c0-4%201-4%203%200%201%203%202%204%204%201v-4c-1-1-1-3%201-5%203-5%200-9-8-8m15%200c-7%203-6%2019%201%2019%202%200%207-4%207-6s-2-2-4%200c-2%203-5%200-3-5%201-3%203-6%203-2%200%201%200%202%202%202%204%200%204-5%201-7s-4-2-7-1m11%200l-1%2010-1%209h3c3%200%203%200%203-4%200-3%200-4%202-4l1%204c0%204%200%204%203%204h3V90h-2c-3%200-3%201-3%204-1%205-4%206-4%200%200-3%200-4-2-4l-2%201m27%200c-5%200-5%200-5%203l2%202c2%200%202%201%202%207%200%207%200%207%203%207%202%200%202%200%202-7s0-8%202-8%203-1%203-2c0-3-2-3-9-2m14%200h-4v6c-2%2011-1%2013%201%2013s3-1%203-3c0-4%201-4%203%200%201%203%202%204%204%201v-4c-1-1-1-3%201-5%203-5%200-9-8-8m16%200c-6%200-6%201-6%2011v8h4c6%200%209-1%209-3s-1-2-4-2c-5%200-5-2-1-3%204%200%204-4%200-4s-4-2%201-2c3-1%204-2%204-4l-2-2-5%201m13%200c-5%200-5%201-5%2012v7h4c6%200%209-1%209-3s-1-2-4-2c-5%200-5-2-1-3%204%200%204-4%200-4s-4-2%201-2c5-1%207-6%202-6l-6%201M24%2094l-1%2010-1%206h5c7%200%2010-2%209-7v-6c2-4%200-6-6-6s-6%200-6%203m65-2l2%207%202%208c0%204%205%204%205%200l2-9c4-8%204-7%201-7-2%200-3%201-4%203l-1%204-2-4c-1-3-5-5-5-2m179%2010c-3%203-5%2013-3%2017%201%202%204%203%205%201%201-1-1-9-3-10-4-1%202-9%208-9%207%200%209%205%206%2011-2%205-2%208%200%209%204%201%207-8%204-9l-1-4c0-8-10-12-16-6m31%204l-1%208v7h17l-5%205a66%2066%200%2000-19%2037l-2%2010v1l6-7c7-9%2017-15%2017-10%200%202%201%203%203%203s3-1%201-2l-1-3h4c3%200%202%201%200%204l-2%204v3h-17v7c0%206%200%207-2%208-3%202-3%203%200%203%202%200%203%204%201%206l-6%201c-9%200-11-4-7-9l2-5c0-3-8%206-8%2010-1%203-1%203-10%204-8%200-8%200-8%202s1%203%205%203c5%200%206%200%204%205-1%202%201%202%2048%202h49l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2-1-2c-7-1-7-4-3-12%207-13%207-17%200-22-4-3-5-4-5-8l-1-4c-1-1-1%201-1%204%200%205%200%205-2%204l-23%201-3%201v-10l-14-1-13%201m2%208v6h22v-13h-22v7m-31%2027l1%209h12v-18h-13v9m55%2016c0%201-1%202-3%202s-2%201-2%204v3h6c9%200%209%200%208%207l1%208v2l-1%203c0%204%200%204%205%204%207%201%2010-1%207-7v-5l-1-3-3-5-1-3h-1v-2l-3-5c-1-2-3-3-7-4l-5-2v3m-52%207l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%204-2%203-2-2-1-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/0b2406d362f0583a063de813c9d1d78c/ee604/blog40.png","srcSet":"/static/0b2406d362f0583a063de813c9d1d78c/69585/blog40.png 200w,\n/static/0b2406d362f0583a063de813c9d1d78c/497c6/blog40.png 400w,\n/static/0b2406d362f0583a063de813c9d1d78c/ee604/blog40.png 800w,\n/static/0b2406d362f0583a063de813c9d1d78c/f3583/blog40.png 1200w,\n/static/0b2406d362f0583a063de813c9d1d78c/e4d72/blog40.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"binarySearchTree","thumbnail":"thumbnails/blog40.png"}},"staticQueryHashes":["2987289216","63159454"]}