ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Verilog N-bit Binary Search Tree Node Insertion

Verilog

Goal -- WPM

Ready
Exercise Algorithm Area
1/*
2* Module: bst_node_insert
3* Description: Inserts a node with a given N-bit value into a Binary Search Tree.
4* This implementation simulates recursion using state machines and explicit pointers.
5* Assumes nodes are dynamically allocated or managed externally.
6*/
7module bst_node_insert (
8// System clock and reset
9input wire clk,
10input wire reset,
11
12// Input value to insert
13input wire signed [N-1:0] insert_val,
14input wire insert_en,
15
16// Root of the BST
17// In a real system, this would be a pointer to the root node structure.
18// For simulation, we'll use a simplified representation.
19// Let's assume a pointer to a node structure is passed by reference or similar.
20// For this example, we'll simplify and assume a direct connection to node management logic.
21// This module focuses on the traversal and insertion logic.
22
23// Output signals indicating operation status
24output reg insert_done,
25output reg duplicate_found
26);
27
28// Parameter for the bit width of the node values
29parameter N = 8;
30
31// State machine for insertion process
32typedef enum logic [1:0] {
33IDLE,
34TRAVERSE,
35INSERT_NEW,
36HANDLE_DUPLICATE
37} state_t;
38
39reg state_t current_state, next_state;
40
41// Pointers to current node and parent node during traversal
42// In a real hardware implementation, these would be addresses or indices.
43// For this simulation, we'll use placeholder signals.
44wire signed [N-1:0] current_node_val;
45wire current_node_is_valid; // Indicates if current_node_val points to an actual node
46wire current_node_is_left_child; // Indicates if current node is left child of parent
47
48// Placeholder for node management interface (e.g., memory interface)
49// In a real design, you'd interface with a memory controller or node pool.
50// For this example, we abstract this away.
51// `assign current_node_val = ...;`
52// `assign current_node_is_valid = ...;`
53
54// Logic to find the next node based on comparison
55wire move_left = insert_val < current_node_val;
56wire move_right = insert_val > current_node_val;
57
58// State register
59always_ff @(posedge clk or posedge reset) begin
60if (reset) begin
61current_state <= IDLE;
62end else begin
63current_state <= next_state;
64end
65end
66
67// Next state logic
68always_comb begin
69next_state = current_state;
70insert_done = 1'b0;
71duplicate_found = 1'b0;
72
73case (current_state)
74IDLE:
75if (insert_en) begin
76// Check if the tree is empty (root is null/invalid)
77// This check would typically involve querying the root pointer.
78// For simulation, let's assume a mechanism to check root validity.
79// If root is invalid, transition to INSERT_NEW.
80// If root is valid, transition to TRAVERSE.
81// Placeholder: Assume root is valid for now, will refine.
82next_state = TRAVERSE;
83end
84TRAVERSE:
85// Check if current node is valid
86if (current_node_is_valid) begin
87if (insert_val == current_node_val) begin
88// Duplicate value found
89next_state = HANDLE_DUPLICATE;
90end else if (move_left) begin
91// Move to left child
92// In a real system, update pointer to left child.
93// For simulation, we'd fetch left child's info.
94next_state = TRAVERSE; // Stay in TRAVERSE to fetch next node
95end else begin // move_right
96// Move to right child
97// In a real system, update pointer to right child.
98// For simulation, we'd fetch right child's info.
99next_state = TRAVERSE; // Stay in TRAVERSE to fetch next node
100end
101end else begin
102// Reached a null pointer, need to insert here
103next_state = INSERT_NEW;
104end
105INSERT_NEW:
106// Logic to allocate and initialize a new node
107// and link it to the parent.
108// This involves memory allocation and pointer updates.
109// For simulation, we just signal completion.
110insert_done = 1'b1;
111next_state = IDLE;
112HANDLE_DUPLICATE:
113// Logic to handle duplicate value (e.g., ignore, update, error)
114// For this example, we signal that a duplicate was found.
115duplicate_found = 1'b1;
116next_state = IDLE;
117default:
118next_state = IDLE;
119endcase
120end
121
122// Placeholder for actual node fetching and linking logic
123// This would involve memory read/write operations based on current_state
124// and comparison results.
125// For example:
126// always_ff @(posedge clk) begin
127// if (current_state == TRAVERSE) begin
128// if (move_left) begin
129// // Fetch left child pointer/data
130// end else begin
131// // Fetch right child pointer/data
132// end
133// end else if (current_state == INSERT_NEW) begin
134// // Allocate new node, set its value, and link it to parent
135// end
136// end
137
138endmodule
Algorithm description viewbox

Verilog N-bit Binary Search Tree Node Insertion

Algorithm description:

This Verilog module implements the logic for inserting a node into an N-bit Binary Search Tree (BST). It simulates the recursive nature of BST insertion using a finite state machine (FSM) and explicit pointer management. The module handles traversal, comparison of values, and the actual insertion of a new node or detection of duplicates. This is a fundamental operation in many data management and searching algorithms, often used in databases and efficient data retrieval systems.

Algorithm explanation:

The `bst_node_insert` module uses a state machine to manage the insertion process into a Binary Search Tree. The states include `IDLE`, `TRAVERSE`, `INSERT_NEW`, and `HANDLE_DUPLICATE`. In the `IDLE` state, it waits for an insertion request. Upon receiving `insert_en`, it transitions to `TRAVERSE`. In `TRAVERSE`, it compares the `insert_val` with the `current_node_val`. If they match, it moves to `HANDLE_DUPLICATE`. If `insert_val` is smaller, it attempts to move to the left child; otherwise, it attempts to move to the right child. If a null child pointer is encountered during traversal, it transitions to `INSERT_NEW`. In `INSERT_NEW`, logic for allocating a new node and linking it to its parent would be implemented (abstracted here). `HANDLE_DUPLICATE` signals that the value already exists. The time complexity for insertion in a balanced BST is O(log N), where N is the number of nodes, but in the worst case (a skewed tree), it can be O(N). The space complexity is O(1) for the FSM and pointer management logic itself, excluding the space occupied by the BST nodes.

Pseudocode:

module bst_node_insert(clk, reset, insert_val, insert_en, insert_done, duplicate_found)
  parameter N = 8
  input clk, reset, insert_en
  input signed [N-1:0] insert_val
  output insert_done, duplicate_found

  state_t current_state, next_state

  // State machine logic
  always_ff @(posedge clk or posedge reset)
    if (reset) current_state <= IDLE
    else current_state <= next_state

  always_comb
    next_state = current_state
    insert_done = 0
    duplicate_found = 0

    case (current_state)
      IDLE:
        if (insert_en) next_state = TRAVERSE
      TRAVERSE:
        // Assume current_node_val and current_node_is_valid are available
        if (insert_val == current_node_val) next_state = HANDLE_DUPLICATE
        else if (insert_val < current_node_val) next_state = TRAVERSE // Move left
        else next_state = TRAVERSE // Move right

        if (!current_node_is_valid) next_state = INSERT_NEW
      INSERT_NEW:
        // Logic to allocate and link new node
        insert_done = 1
        next_state = IDLE
      HANDLE_DUPLICATE:
        duplicate_found = 1
        next_state = IDLE
    endcase
endmodule