Binary Tree in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 10:54:43

269 Views

Binary Tree is a special data structure used for data storage purposes. A binary tree has a special condition that each node can have a maximum of two children. A binary tree has the benefits of both an ordered array and a linked list as search is as quick as in a sorted array and insertion or deletion operation are as fast as in the linked list.Here is an illustration of a binary tree with some terms that we've discussed below −Important TermsFollowing are the important terms with respect to the tree.Path − Path refers to the sequence of nodes ... Read More

Binary Search Tree in JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 10:54:03

446 Views

A Binary Search tree exhibits a special behavior. A node's left child must have a value less than its parent's value and the node's right child must have a value greater than its parent value.We'll mostly focus on such trees in this section on trees.Operations on Binary Search TreesWe'll define the following operations on the Binary Search Tree −Inserting a key into a treeIn-order traversal in a treePre-order traversal in a treePost-order traversal in a treeSearching for values in a treeSearching for minimum value in a treeSearching for maximum value in a treeRemoving a leaf node in a treeRead More

Dictionary Data Structure in JavaScript

Samual Sam
Updated on 15-Jun-2020 10:53:14

1K+ Views

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears at most once in the collection. Note that a dictionary is also known as a map.The dictionary problem is a classic computer science problem: the task of designing a data structure that maintains a set of data during 'search', 'delete', and 'insert' operations. There are many different types of implementations of dictionaries. Hash Table implementation Tree-Based Implementation (Self-balancing and Unbalanced trees) List based implementationWhen to use a DictionaryDictionaries are not a silver bullet ... Read More

Creating Dictionary Using JavaScript

Monica Mona
Updated on 15-Jun-2020 10:52:03

2K+ Views

Let's create a MyMap class so that it doesn't hide the actual Map class in JS. We'll create a container object that'll keep track of all our values that we add to the map. We'll also create a display function that prints the map for us. Exampleclass MyMap {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    } }In ES6, you can directly create a dictionary using the Map class. For example,  Exampleconst map1 = new Map(); const map2 = new Map([    ["key1", "value1"],    ["key2", "value2"] ]);Checking if ... Read More

Add Elements to a Dictionary in JavaScript

Monica Mona
Updated on 15-Jun-2020 10:49:38

10K+ Views

Now we'll create the put method that'll allow us to put key-value pairs on the dictionary. Now using this we'll implement the put method.Note that JS has objects that act quite like dictionaries. We can just set the container's key property to value. Exampleput(key, value) {    this.container[key] = value; }You can test this and the previous functions using − Exampleconst myMap = new MyMap() myMap.put("key1", "value1") myMap.put("key2", "value2") myMap.display() console.log(myMap.hasKey("key1")); console.log(myMap.hasKey("key3"));OutputThis will give the output −{key1: "value1", key2: "value2"} true falseIn ES6, you can put a key-value pair in a map using the set method. For example,  Exampleconst myMap ... Read More

Remove Elements from a Dictionary Using JavaScript

Samual Sam
Updated on 15-Jun-2020 10:47:26

24K+ Views

To remove an element from the dictionary, we first need to check if it exists in the dictionary.We'll use the hasKey method for that. Then we can directly delete it using the delete operator.We'll return a Boolean so that the place where we call this method can know whether the key already existed or not in the dictionary. Exampledelete(key) {    if(this.hasKey(key)) {       delete this.container[key];       return true;    }    return false; }You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); myMap.display(); myMap.delete("key2"); myMap.display();OutputThis will give the output ... Read More

Search Element in a Dictionary using JavaScript

Monica Mona
Updated on 15-Jun-2020 10:43:22

3K+ Views

We'll implement the get method that searches a given key in the dictionary. Exampleget(key) {    if(this.hasKey(key)) {       return this.container[key];    }    return undefined; }Again, JS objects are very much implemented like dictionaries, hence have most of the functionality we can use directly without any more code needed. This is also heavily optimized, so you don't have to worry about the runtime of the function.You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); console.log(myMap.get("key1")) console.log(myMap.get("key2")) console.log(myMap.get("key3"))OutputThis will give the output −value1 value2 undefinedIn ES6, you have the same functionality using the ... Read More

Keys and Values Method in JavaScript

Samual Sam
Updated on 15-Jun-2020 10:41:20

238 Views

Sometimes when working with a dictionary, we need only the keys of the dictionary as an array for some task. We can easily get the properties of an object using Object.keys. We'll use this method to return the keys from our container object. Examplekeys() {    return Object.keys(this.container); }You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); console.log(myMap.keys());OutputThis will give the output −[ 'key1', 'key2' ]In ES6 Map, the same method is available that you can use. Note that it returns a MapIterator object, which you can convert to an array or use like a ... Read More

Clearing a Dictionary Using JavaScript

Monica Mona
Updated on 15-Jun-2020 10:37:12

2K+ Views

We'll implement a clear() function that simply clears the contents of the container. For example,  Exampleclear() {    this.container = {} }You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2");   myMap.display(); myMap.clear(); myMap.display();OutputThis will give the output −{ key1: 'value1', key2: 'value2' }You can use the clear method in the same way in ES6 maps as well. For example, Exampleconst myMap = new Map([    ["key1", "value1"],    ["key2", "value2"] ]); console.log(myMap) myMap.clear(); console.log(myMap)OutputThis will give the output −Map { 'key1' => 'value1', 'key2' => 'value2' } Map {}Read More

Loop Through a Dictionary in Javascript

Monica Mona
Updated on 15-Jun-2020 10:34:46

11K+ Views

Here we'll implement a for each function in our class and accept a callback that we can call on every key-value pair. Let's see how we can implement such a function − ExampleforEach(callback) {    for (let prop in this.container) {       // Call the callback as: callback(key, value)       callback(prop, this.container[prop]);    } }You can test this using − Exampleconst myMap = new MyMap(); myMap.put("key1", "value1"); myMap.put("key2", "value2"); myMap.forEach((k, v) => console.log(`Key is ${k} and value is ${v}`));OutputThis will give the output −Key is key1 and value is value1 Key is key2 and value is value2ES6 ... Read More

Advertisements