Question: 5. (20 points) Given the API of the Trie data structure given below, add a method with signature String longestKey() to this class that traverses

 5. (20 points) Given the API of the Trie data structure

5. (20 points) Given the API of the Trie data structure given below, add a method with signature "String longestKey() to this class that traverses the data structure and returns the key with the longest string length. Write a complete Java implementation of your solution. Hint: you may use recursion, and a helper function for recursion. public class TrieST { private static int R = 256; // radix private Node root; // root of trie private static class Node { private Object val; private Node[] next = new Node [R]; } public Value get(String key) { Node x = get(root, key, 0); if (x == null) return null; return (Value) x.val; } private Node get(Node x, String key, int d) { // Return value associated with key in the subtrie rooted at x. if (x == null) return null; if (d == key.length()) return x; char c = key.charAt(d); // Use dth key char to identify subtrie. return get(x.next[c], key, d+1); } public void put(String key, Value val) { root - put(root, key, val, 0); } private Node put(Node x, String key, Value val, int d) { !! Change value associated with key if in subtrie rooted at x. if (x == null) X = new Node(); if (d == key.length()) { x.val = val; return x; } char c = key.charAt(d); // Use dth key char to identify subtrie. x.next[c] = put(x.next[c], key, val, d+1); return x; } } (a) public String longestKey() ( ... } (b) What is the complexity of your algorithm? Discuss in a few sentences

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!