-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ada3306
commit 749ab3c
Showing
5 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<h1>Introduction to project </h1> | ||
The project implements a search bar that utilizes the Trie data structure for efficient string searching. It allows users to insert new words dynamically and search for words with autocomplete functionality. | ||
|
||
# Trie-based Search Bar Project | ||
|
||
This project implements a search bar using the Trie data structure. Trie, also known as a prefix tree, is used for efficient string searching operations, making it an ideal choice for implementing search functionalities. | ||
|
||
# Insertion feature | ||
|
||
You can easily insert new word and perform search for that word . | ||
|
||
## Installation | ||
|
||
To run the project locally, follow these steps: | ||
|
||
1. Clone the repository to your local machine: | ||
|
||
```bash | ||
git clone https://github.com/saurabh221402/JS-BEGINNER-10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="trie.css"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<!--da--> | ||
<div id="fun1"> | ||
<h3>Search My ClassMate Name </h3> | ||
<input type="text" id="search" oninput="main()"> | ||
<div id="search_id"> | ||
|
||
</div> | ||
</div> | ||
<div id="fun2"> | ||
<h4>Insert your name if you are my ClassMate </h4> | ||
<input type="text" id="in"> | ||
<button onclick="insert_new()">INSERT</button> | ||
<span id="mess"></span> | ||
</div> | ||
|
||
<div id="list_box"></div> | ||
<script src="trie.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
* | ||
{ | ||
margin: 0 auto; | ||
box-sizing: border-box; | ||
} | ||
input{ | ||
padding: 20px; | ||
width: 70%; | ||
border:2px solid teal; | ||
margin: 20px auto; | ||
background-color:#eafafb; | ||
} | ||
#fun1 | ||
{ | ||
border: 1px solid black; | ||
width: 40%; | ||
min-width: 300px; | ||
height: 400px; | ||
display: flex; | ||
flex-direction: column; | ||
|
||
} | ||
#fun2{ | ||
border: 1px solid black; | ||
width: 40%; | ||
min-width: 300px; | ||
height: 400px; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
#mess | ||
{ | ||
margin-top: 20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
class TN { | ||
constructor() { | ||
this.children = new Array(26).fill(null); | ||
this.AC_word_idx = []; | ||
this.isEnd = false; | ||
} | ||
} | ||
|
||
function insert(root, s) { | ||
//console.log(s); | ||
let temp = root; | ||
for (let i = 0; i < s.length; i++) { | ||
let idx = s.charCodeAt(i) - 'a'.charCodeAt(0); | ||
if (!temp.children[idx]) temp.children[idx] = new TN(); | ||
temp = temp.children[idx]; | ||
temp.AC_word_idx.push(s); | ||
} | ||
temp.isEnd = true; | ||
} | ||
|
||
function search(root, s) { | ||
let temp = root; | ||
for (let i = 0; i < s.length; i++) { | ||
let idx = s.charCodeAt(i) - 'a'.charCodeAt(0); | ||
if (!temp.children[idx]) return ["Not found"]; | ||
temp = temp.children[idx]; | ||
} | ||
return temp.AC_word_idx; | ||
} | ||
//global | ||
let root = new TN(); | ||
let words; | ||
function initialise() | ||
{ | ||
words = ["saurabh","sunny","suryansh","aparana","aporva","shivam","shivani","shivansh","tanish","jagdish","sauurabhp", | ||
"sakshi","prince","abhishek","rachit","prakhar","prakash","paras","avantika","sarthak","rahul","sanchit", | ||
"saurya", "ravi", "ravikishan", "rahul", "lavnesh","mehul","ankit","avinash","kunal","mayank","samyak","ankita","siya","rajni","rajnish" | ||
,"pranjal","nancy","satyam","astha","pragati","anim","aayush","adarsh","aman","amit","aryan","deepak","kartik","kartikey"]; | ||
for (let x of words) insert(root, x); | ||
} | ||
initialise(); | ||
function insert_new() | ||
{ | ||
let val=document.getElementById("in").value; | ||
|
||
if(val===""){document.getElementById("mess").innerHTML="empty value";} | ||
else{ | ||
insert(root,val); | ||
document.getElementById("mess").innerHTML="name inserted"; | ||
} | ||
saveData(); | ||
document.getElementById("in").value=""; | ||
setTimeout(()=>{ | ||
document.getElementById("mess").innerHTML=""; | ||
},3000); | ||
} | ||
function main() { | ||
let qur = document.getElementById("search").value; | ||
let search_box=document.getElementById("search_id"); | ||
while (search_box.firstChild) { | ||
search_box.removeChild(search_box.firstChild); | ||
} | ||
let ans = []; | ||
|
||
ans.push(search(root,qur)); | ||
for (let i = 0; i < ans.length; i++) { | ||
for (let str of ans[i]){ | ||
let temp=document.createElement('div'); | ||
temp.innerText=str; | ||
search_box.appendChild(temp); | ||
//console.log(str + " "); | ||
} | ||
// console.log(""); | ||
} | ||
|
||
} | ||
|
||
//storing locally | ||
function saveData() { | ||
localStorage.setItem("rootData", JSON.stringify(root)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters