Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-144

Ternary Search Tree Lite

Problem

Simulate a ternary search tree supporting insert and exact search.

operations contains "insert" or "search" and words provides the argument. Each node holds one character plus left, middle, right, and end-of-word markers; the left and right pointers order characters and the middle pointer advances to the next character.

Return the list of recorded results (None for insert, a boolean for search).

Hint

Characters smaller go left, larger go right, and matching characters descend to the middle.

Test cases

case 1
in: [["insert","search","search"],["cat","cat","car"]]
expected: [null,true,false]
case 2
in: [["insert","insert","search","search"],["a","ab","a","ab"]]
expected: [null,null,true,true]
case 3
in: [["search"],["x"]]
expected: [false]
case 4
in: [["insert","search","search"],["go","go","god"]]
expected: [null,true,false]
Starter codePython
def tst_ops(operations, words):
    # Your code here
    pass

4 test cases · run your code against them in the editor