Skip to main content

Binary Search Tree in DSA


In Data Structures and Algorithms (DSA), Trees

Trees are a fundamental data structure that help in efficiently organizing and managing hierarchical data. They are used in many applications like databases, file systems, search engines, and network routing, among others.

How Trees Help in DSA:

  • Efficient Searching: Binary search trees (BST) allow fast searching, insertion, and deletion of data in O(log n) time (on average), making it much faster than linear search on an array.
  • Hierarchical Data Representation: Trees help in representing hierarchical data, like file systems or organizational charts, where each node represents a unit, and the edges represent the relationship between them.
  • Balanced Trees: Variants like AVL trees or Red-Black trees maintain balance, ensuring efficient performance in searching and updates.
  • Priority Queue: Heaps (a type of binary tree) are used in priority queues, where elements are inserted or removed based on priority rather than order of arrival.
  • Tree Traversal: Traversal algorithms like in-order, pre-order, and post-order are widely used in many scenarios like expression evaluation, syntax parsing, etc.

Example: Binary Search Tree (BST) Implementation

Let’s see an example of a Binary Search Tree (BST) in Python, and how it helps in organizing and searching data efficiently.


class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.value = key

class BinarySearchTree:
    def __init__(self):
        self.root = None
    
    def insert(self, root, key):
        if root is None:
            return Node(key)
        if key < root.value:
            root.left = self.insert(root.left, key)
        else:
            root.right = self.insert(root.right, key)
        return root
    
    def search(self, root, key):
        if root is None or root.value == key:
            return root
        if root.value < key:
            return self.search(root.right, key)
        return self.search(root.left, key)

    def inorder(self, root):
        return self.inorder(root.left) + [root.value] + self.inorder(root.right) if root else []

# Example Usage
bst = BinarySearchTree()
root = None

# Inserting nodes into the BST
keys = [20, 8, 22, 4, 12, 10, 14]
for key in keys:
    root = bst.insert(root, key)

# Searching for a key
search_key = 10
found_node = bst.search(root, search_key)

if found_node:
    print(f"Node {search_key} found in the tree.")
else:
    print(f"Node {search_key} not found in the tree.")

# In-order Traversal (Sorted order for BST)
print("In-order Traversal:", bst.inorder(root))

        

Explanation:

  • Node Class: Each node contains a value, a reference to the left child, and a reference to the right child.
  • insert() Method: This method inserts a new node into the tree by comparing the value to be inserted with the root. If the value is less than the current node, it goes to the left; if it's greater, it goes to the right.
  • search() Method: This method searches for a given key in the BST. It compares the key with the root and recursively searches in the left or right subtree depending on whether the key is smaller or larger than the current node's value.
  • inorder() Method: This method returns the in-order traversal of the tree (left-root-right). For a BST, this traversal gives the elements in sorted order.

Output:


Node 10 found in the tree.
In-order Traversal: [4, 8, 10, 12, 14, 20, 22]

        

Real-World Applications of Trees:

  • File System: The hierarchy of directories and files is often represented using trees, where each folder is a node and its subfolders or files are child nodes.
  • Search Engines: Binary search trees, B-trees, or tries are used to efficiently store and search a large number of documents or URLs.
  • Network Routing: Routing tables are often represented as trees or graphs, with nodes representing routers and edges representing paths between them.
  • Expression Parsing: In compilers, abstract syntax trees (ASTs) are used to represent the structure of expressions and statements in source code.

Variants of Trees:

  • Binary Search Tree (BST): Allows efficient search and insertion, but it can become unbalanced.
  • AVL Tree: A balanced BST, ensures O(log n) time for search, insertion, and deletion.
  • Red-Black Tree: Another self-balancing binary search tree.
  • Heap: A specialized tree-based structure that satisfies the heap property and is used in priority queues.

Keyword Search in Paragraphs Using Binary Search Tree (BST)

Certainly! This example adapts a Binary Search Tree (BST) to search for keywords within paragraphs and indicate their positions in a text file. Characters are not converted into numbers or tokens—instead, each paragraph and keyword is treated as a plain string.

Approach

  1. Create a Binary Search Tree (BST)
    • Each node stores a keyword
    • The BST allows fast keyword lookup
  2. Search for Keywords in Paragraphs
    • Each keyword is searched within each paragraph
    • All matching positions are recorded
  3. Indicate Keyword Positions
    • If a keyword is found, its index positions are displayed
  4. Write Results to a File
    • Results are saved in a text file for later review

Example Python Code


class Node:
    def __init__(self, key):
        self.left = None
        self.right = None
        self.value = key

class BinarySearchTree:
    def __init__(self):
        self.root = None
    
    def insert(self, root, key):
        if root is None:
            return Node(key)
        if key < root.value:
            root.left = self.insert(root.left, key)
        else:
            root.right = self.insert(root.right, key)
        return root
    
    def search(self, root, key):
        if root is None or root.value == key:
            return root
        if root.value < key:
            return self.search(root.right, key)
        return self.search(root.left, key)

def search_keyword_in_paragraph(paragraph, keyword):
    positions = []
    start = 0
    while start < len(paragraph):
        start = paragraph.find(keyword, start)
        if start == -1:
            break
        positions.append(start)
        start += len(keyword)
    return positions

def write_to_file(file_name, results):
    with open(file_name, 'w') as file:
        for result in results:
            file.write(result + '\n')

def main():
    paragraphs = [
        "This is the first paragraph. It contains some words.",
        "The second paragraph has some different words.",
        "Here is the third one. Keywords are fun to search.",
        "The fourth paragraph also has the word search."
    ]
    
    keywords = ["search", "words", "fun"]
    
    bst = BinarySearchTree()
    root = None
    for keyword in keywords:
        root = bst.insert(root, keyword)
    
    results = []
    for i, paragraph in enumerate(paragraphs):
        result = f"Paragraph {i+1}: {paragraph}\n"
        for keyword in keywords:
            if bst.search(root, keyword):
                positions = search_keyword_in_paragraph(paragraph, keyword)
                if positions:
                    result += f"Keyword '{keyword}' found at positions: {positions}\n"
                else:
                    result += f"Keyword '{keyword}' not found in this paragraph.\n"
        results.append(result)
    
    write_to_file("keyword_search_results.txt", results)

if __name__ == "__main__":
    main()

            

Explanation of the Code

  • Node Class: Stores a keyword and references to left and right child nodes
  • BinarySearchTree Class: Handles insertion and searching of keywords
  • search_keyword_in_paragraph(): Finds all positions of a keyword in a paragraph
  • write_to_file(): Saves results to a text file
  • main(): Controls the program flow and ties everything together

Example Input


Paragraph 1: This is the first paragraph. It contains some words.
Paragraph 2: The second paragraph has some different words.
Paragraph 3: Here is the third one. Keywords are fun to search.
Paragraph 4: The fourth paragraph also has the word search.

            

Example Keywords

  • search
  • words
  • fun

Example Output (keyword_search_results.txt)


Paragraph 1: This is the first paragraph. It contains some words.
Keyword 'search' not found in this paragraph.
Keyword 'words' found at positions: [41]
Keyword 'fun' not found in this paragraph.

Paragraph 2: The second paragraph has some different words.
Keyword 'search' not found in this paragraph.
Keyword 'words' found at positions: [47]
Keyword 'fun' not found in this paragraph.

Paragraph 3: Here is the third one. Keywords are fun to search.
Keyword 'search' found at positions: [56]
Keyword 'words' not found in this paragraph.
Keyword 'fun' found at positions: [35]

Paragraph 4: The fourth paragraph also has the word search.
Keyword 'search' found at positions: [50]
Keyword 'words' not found in this paragraph.
Keyword 'fun' not found in this paragraph.

            

Summary

  • BST enables fast keyword lookup
  • String-based searching preserves original text structure
  • Keyword positions are clearly tracked and stored
  • Useful for text analysis, document scanning, and indexing

 

Further Reading

  1.  

People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *

Popular Posts

Constellation Diagrams of ASK, PSK, and FSK

📘 Overview of Energy per Bit (Eb / N0) 🧮 Online Simulator for constellation diagrams of ASK, FSK, and PSK 🧮 Theory behind Constellation Diagrams of ASK, FSK, and PSK 🧮 MATLAB Codes for Constellation Diagrams of ASK, FSK, and PSK 📚 Further Reading 📂 Other Topics on Constellation Diagrams of ASK, PSK, and FSK ... 🧮 Simulator for constellation diagrams of m-ary PSK 🧮 Simulator for constellation diagrams of m-ary QAM BASK (Binary ASK) Modulation: Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1.    BFSK (Binary FSK) Modulation: Transmits one of two signals: +√Eb​ ( On the y-axis, the phase shift of 90 degrees with respect to the x-axis, which is also termed phase offset ) or √Eb (on x-axis), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  BPSK (Binary PSK) Modulation: Transmits one of two signals...

Fading : Slow & Fast and Large & Small Scale Fading

📘 Overview 📘 LARGE SCALE FADING 📘 SMALL SCALE FADING 📘 SLOW FADING 📘 FAST FADING 🧮 MATLAB Codes 📚 Further Reading LARGE SCALE FADING The term 'Large scale fading' is used to describe variations in received signal power over a long distance, usually just considering shadowing.  Assume that a transmitter (say, a cell tower) and a receiver  (say, your smartphone) are in constant communication. Take into account the fact that you are in a moving vehicle. An obstacle, such as a tall building, comes between your cell tower and your vehicle's line of sight (LOS) path. Then you'll notice a decline in the power of your received signal on the spectrogram. Large-scale fading is the term for this type of phenomenon. SMALL SCALE FADING  Small scale fading is a term that describes rapid fluctuations in the received signal power on a small time scale. This includes multipath propagation effects as well as movement-induced Doppler fr...

Online Simulator for ASK, FSK, and PSK

Try our new Digital Signal Processing Simulator!   Start Simulator for binary ASK Modulation Message Bits (e.g. 1,0,1,0) Carrier Frequency (Hz) Sampling Frequency (Hz) Run Simulation Simulator for binary FSK Modulation Input Bits (e.g. 1,0,1,0) Freq for '1' (Hz) Freq for '0' (Hz) Sampling Rate (Hz) Visualize FSK Signal Simulator for BPSK Modulation ...

DFTs-OFDM vs OFDM: Why DFT-Spread OFDM Reduces PAPR Effectively (with MATLAB Code)

DFT-spread OFDM (DFTs-OFDM) has lower Peak-to-Average Power Ratio (PAPR) because it "spreads" the data in the frequency domain before applying IFFT, making the time-domain signal behave more like a single-carrier signal rather than a multi-carrier one like OFDM. Deeper Explanation: Aspect OFDM DFTs-OFDM Signal Type Multi-carrier Single-carrier-like Process IFFT of QAM directly QAM → DFT → IFFT PAPR Level High (due to many carriers adding up constructively) Low (less fluctuation in amplitude) Why PAPR is High Subcarriers can add in phase, causing spikes DFT "pre-spreads" data, smoothing it Used in Wi-Fi, LTE downlink LTE uplink (as SC-FDMA) In OFDM, all subcarriers can...

Theoretical BER vs SNR for m-ary PSK and QAM

Relationship Between Bit Error Rate (BER) and Signal-to-Noise Ratio (SNR) The relationship between Bit Error Rate (BER) and Signal-to-Noise Ratio (SNR) is a fundamental concept in digital communication systems. Here’s a detailed explanation: BER (Bit Error Rate): The ratio of the number of bits incorrectly received to the total number of bits transmitted. It measures the quality of the communication link. SNR (Signal-to-Noise Ratio): The ratio of the signal power to the noise power, indicating how much the signal is corrupted by noise. Relationship The BER typically decreases as the SNR increases. This relationship helps evaluate the performance of various modulation schemes. BPSK (Binary Phase Shift Keying) Simple and robust. BER in AWGN channel: BER = 0.5 × erfc(√SNR) Performs well at low SNR. QPSK (Quadrature...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...

📘 Overview of BER and SNR 🧮 Online Simulator for BER calculation of m-ary QAM and m-ary PSK 🧮 MATLAB Code for BER calculation of M-ary QAM, M-ary PSK, QPSK, BPSK, ... 📚 Further Reading 📂 View Other Topics on M-ary QAM, M-ary PSK, QPSK ... 🧮 Online Simulator for Constellation Diagram of m-ary QAM 🧮 Online Simulator for Constellation Diagram of m-ary PSK 🧮 MATLAB Code for BER calculation of ASK, FSK, and PSK 🧮 MATLAB Code for BER calculation of Alamouti Scheme 🧮 Different approaches to calculate BER vs SNR What is Bit Error Rate (BER)? The abbreviation BER stands for Bit Error Rate, which indicates how many corrupted bits are received (after the demodulation process) compared to the total number of bits sent in a communication process. BER = (number of bits received in error) / (total number of tran...

Theoretical BER vs SNR for binary ASK, FSK, and PSK

📘 Overview & Theory 🧮 MATLAB Codes 📚 Further Reading Theoretical BER vs SNR for Amplitude Shift Keying (ASK) The theoretical Bit Error Rate (BER) for binary ASK depends on how binary bits are mapped to signal amplitudes. For typical cases: If bits are mapped to 1 and -1, the BER is: BER = Q(√(2 × SNR)) If bits are mapped to 0 and 1, the BER becomes: BER = Q(√(SNR / 2)) Where: Q(x) is the Q-function: Q(x) = 0.5 × erfc(x / √2) SNR : Signal-to-Noise Ratio N₀ : Noise Power Spectral Density Understanding the Q-Function and BER for ASK Bit '0' transmits noise only Bit '1' transmits signal (1 + noise) Receiver decision threshold is 0.5 BER is given by: P b = Q(0.5 / σ) , where σ = √(N₀ / 2) Using SNR = (0.5)² / N₀, we get: BER = Q(√(SNR / 2)) Theoretical BER vs ...

MATLAB Code for ASK, FSK, and PSK

📘 Overview & Theory 🧮 MATLAB Code for ASK 🧮 MATLAB Code for FSK 🧮 MATLAB Code for PSK 🧮 Simulator for binary ASK, FSK, and PSK Modulations 📚 Further Reading ASK, FSK & PSK HomePage MATLAB Code MATLAB Code for ASK Modulation and Demodulation % The code is written by SalimWireless.Com % Clear previous data and plots clc; clear all; close all; % Parameters Tb = 1; % Bit duration (s) fc = 10; % Carrier frequency (Hz) N_bits = 10; % Number of bits Fs = 100 * fc; % Sampling frequency (ensure at least 2*fc, more for better representation) Ts = 1/Fs; % Sampling interval samples_per_bit = Fs * Tb; % Number of samples per bit duration % Generate random binary data rng(10); % Set random seed for reproducibility binary_data = randi([0, 1], 1, N_bits); % Generate random binary data (0 or 1) % Initialize arrays for continuous signals t_overall = 0:Ts:(N_bits...