Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh221402 committed Jul 26, 2024
1 parent ada3306 commit 749ab3c
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ALGO/Tries/abc/decription.md
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
28 changes: 28 additions & 0 deletions ALGO/Tries/abc/index.html
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>
34 changes: 34 additions & 0 deletions ALGO/Tries/abc/trie.css
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;
}
81 changes: 81 additions & 0 deletions ALGO/Tries/abc/trie.js
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));
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</div>
<div id="container">
<div class="algo-container">
<a href="algo/Tries/searchBar/index.html" class="category_link">
<a href="algo/Tries/abc/index.html" class="category_link">
<div class="imageContainer">
<img src="assets/Images/Tries.png" class="image" />
</div>
Expand Down

0 comments on commit 749ab3c

Please sign in to comment.