From 749ab3cb5ceb49e3249879798255f4f3f1626380 Mon Sep 17 00:00:00 2001 From: saurabh gupta Date: Fri, 26 Jul 2024 20:02:30 +0530 Subject: [PATCH] update --- ALGO/Tries/abc/decription.md | 19 +++++++++ ALGO/Tries/abc/index.html | 28 +++++++++++++ ALGO/Tries/abc/trie.css | 34 +++++++++++++++ ALGO/Tries/abc/trie.js | 81 ++++++++++++++++++++++++++++++++++++ index.html | 2 +- 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 ALGO/Tries/abc/decription.md create mode 100644 ALGO/Tries/abc/index.html create mode 100644 ALGO/Tries/abc/trie.css create mode 100644 ALGO/Tries/abc/trie.js diff --git a/ALGO/Tries/abc/decription.md b/ALGO/Tries/abc/decription.md new file mode 100644 index 0000000..47ce95b --- /dev/null +++ b/ALGO/Tries/abc/decription.md @@ -0,0 +1,19 @@ +

Introduction to project

+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 \ No newline at end of file diff --git a/ALGO/Tries/abc/index.html b/ALGO/Tries/abc/index.html new file mode 100644 index 0000000..26aabc3 --- /dev/null +++ b/ALGO/Tries/abc/index.html @@ -0,0 +1,28 @@ + + + + + + + Document + + + +
+

Search My ClassMate Name

+ +
+ +
+
+
+

Insert your name if you are my ClassMate

+ + + +
+ +
+ + + \ No newline at end of file diff --git a/ALGO/Tries/abc/trie.css b/ALGO/Tries/abc/trie.css new file mode 100644 index 0000000..02b5730 --- /dev/null +++ b/ALGO/Tries/abc/trie.css @@ -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; +} \ No newline at end of file diff --git a/ALGO/Tries/abc/trie.js b/ALGO/Tries/abc/trie.js new file mode 100644 index 0000000..e53f338 --- /dev/null +++ b/ALGO/Tries/abc/trie.js @@ -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)); +} \ No newline at end of file diff --git a/index.html b/index.html index bc00ee7..c410c11 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@