πŸ™
Frontend Dev Guide
  • ⚑Read Me First
  • πŸ“–Frontend Interview Guide
    • πŸ’¬Technical Behavior
      • Frontend Interview Questions
      • Answering Interview Questions
    • πŸ’»Coding Challenges
      • Frontend Code Challenges
      • Data Structures and Algorithms
        • Binary Search Tree
        • Blind 75 and Neetcode
      • Take Home Assignments
    • πŸ•ΈοΈFrontend System Design
      • What is Frontend System Design?
        • Parts of the Frontend System Design
          • 1) Gather Requirements
          • 2) Architecture/High Level Design
          • 3) Data Model and Flow
          • 4) API
          • 5) Optimization and Deep Dive
        • Example: Design Spotify
  • πŸ”¦Frontend Deep Dive
    • πŸ₯žMicrofrontends
    • 🧩Fundmentals
      • πŸ”΅Cross Browser Compatibility
    • πŸ“šFrameworks/Libraries
      • 🟒Vue
      • πŸ”΅React
        • Waterfalls, Unidirectional Data Flow
        • React Server Components
    • 🏁Patterns
      • 🟑Design Patterns
      • βšͺRendering Patterns
      • 🟣Performance Patterns
    • πŸ”‹Performance
      • βšͺNetwork Optimizations
      • 🟠Build Optimizations
      • 🟣Asset Optimizations
      • πŸ”΅Core Web Vitals
  • 🐍Python
    • βšͺDjango
      • Classbased Views (CBV)
      • Cross-Site Request Forgery (CSRF)
  • πŸ—»Working Life
    • 🟣Technical Communication
  • πŸ“šGlossary
  • πŸ’‘Resources
  • πŸ‘©β€πŸ’»About Me
Powered by GitBook
On this page
  1. Frontend Interview Guide
  2. Coding Challenges
  3. Data Structures and Algorithms

Binary Search Tree

A binary search tree (BST) is a data structure that stores elements in a way that allows for efficient search, insertion, and deletion operations. A BST is a type of binary tree, where each node has a

Introduction Binary Search Tree (BST)

A binary search tree (BST) is a data structure that stores elements in a way that allows for efficient search, insertion, and deletion operations. A BST is a type of binary tree, where each node has at most two children: a left child and a right child.

The BST satisfies the following property: for any node in the tree, the value of the node's left child is less than the node's value, and the value of the node's right child is greater than the node's value. This means that values in the tree are organized in a way that allows for efficient searching: if we want to search for a value x in the tree, we can start at the root and follow the appropriate branches based on whether x is greater or less than the values we encounter, until we find the value we're looking for or determine that it's not in the tree.

Insertion and deletion operations in a BST also maintain this property. When inserting a new value, we start at the root and follow the appropriate branches until we find an empty spot to insert the new value. When deleting a value, we first search for the node containing the value, and then replace it with its successor (the next highest value in the tree) or its predecessor (the next lowest value in the tree), maintaining the BST property.

The efficiency of BST operations depends on the balance of the tree. A balanced BST has a height of O(log n), where n is the number of nodes in the tree, allowing for efficient operations. However, an unbalanced BST can have a height of O(n), reducing the efficiency of operations.

Inorder Tree Traversal

Inorder traversal is a way of visiting all the nodes in a binary search tree (BST) in a specific order. Inorder traversal follows a left-root-right order when visiting nodes. Here's the algorithm for inorder traversal:

  1. Check if the current node is null. If it is, return.

  2. Traverse the left subtree by recursively calling the inorder function on the left child of the current node.

  3. Visit the current node.

  4. Traverse the right subtree by recursively calling the inorder function on the right child of the current node.

In other words, inorder traversal involves visiting the left subtree, then visiting the current node, and then visiting the right subtree.

In a BST, inorder traversal will visit nodes in ascending order, because the left subtree of any node contains values less than the node's value, and the right subtree contains values greater than the node's value. Therefore, by visiting the left subtree first, we are guaranteed to visit all smaller values before visiting the current node, and by visiting the right subtree last, we are guaranteed to visit all larger values after visiting the current node.

PreviousData Structures and AlgorithmsNextBlind 75 and Neetcode

Last updated 2 years ago

πŸ“–
πŸ’»