Java 以哈希映射值作为键的递归

Java 以哈希映射值作为键的递归,java,data-structures,collections,Java,Data Structures,Collections,我有一个HashMap>。 1={2,3},2={4,5},3={6,7} 值列表的元素本身是同一HashMap中的键 考虑到1是我的根,我想检索顺序为1,2,3,4,5,6,7的值 尝试使用递归,但没有成功-( 我的代码: package WrapperClasses; import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import

我有一个HashMap>。 1={2,3},2={4,5},3={6,7}

值列表的元素本身是同一HashMap中的键

考虑到1是我的根,我想检索顺序为1,2,3,4,5,6,7的值

尝试使用递归,但没有成功-(

我的代码:

package WrapperClasses;

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

import Collections.BinaryTree.Node;

// Class name should be "Source",
// otherwise solution won't be accepted
public class Source3_Try2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // Declare the variable
        int a;
        // Read the variable from STDIN
        a = in.nextInt();
        String key, value;
        HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
        ArrayList<String> keyList = new ArrayList<String>();
        for (int i = 0; i < a; i++) {
            ArrayList<String> al = new ArrayList<String>();
            key = in.next();
            value = in.next();
            if (hm.containsKey(key)) {
                al = hm.get(key);
            } else {
                keyList.add(key);
            }
            al.add(value);
            hm.put(key, al);

        }
        ArrayList<Node> nodeList = new ArrayList<Node>();
        String f = in.next();
        BinaryTree bt = new BinaryTree();
        bt.insertNode(f);
        ArrayList<String> valueList= hm.get(f);
        insertInTree(bt,valueList,hm,0,0);
        for (String k : keyList) {
            if (!k.equalsIgnoreCase(f)) {
            }
        }
        // Output the variable to STDOUT

        bt.inorderTraversal(bt.root);

    }

    public static void insertInTree(BinaryTree bt , ArrayList<String> valueList,HashMap<String, ArrayList<String>> hm, int flag, int size){
        for(String v : valueList){
            bt.insertNode(v);
        }
        if(flag==size){
        for(String v2 : valueList){
            flag++;
            insertInTree(bt,hm.get(v2),hm,flag,valueList.size());
        }
        }
    }

    // InnerClass: Binary Tree Implementaion - Starts
            static class BinaryTree {
                class Node {
                    String data;
                    Node left;
                    Node right;
                    Node parent;

                    public Node(String data) {
                        this.data = data;
                        this.left = null;
                        this.right = null;
                    }
                }

                public Node root;

                public BinaryTree() {
                    root = null;
                }

                public void insertNode(String data) {
                    Node newNode = new Node(data);
                    if (root == null) {
                        root = newNode;
                        return;
                    } else {
                        Queue<Node> queue = new LinkedList<Node>();
                        queue.add(root);
                        while (true) {
                            Node node = queue.remove();
                            if (node.left != null && node.right != null) {
                                queue.add(node.left);
                                queue.add(node.right);
                            } else {
                                if (node.left == null) {
                                    newNode.parent=node;
                                    node.left = newNode;
                                    queue.add(node.left);
                                } else {
                                    newNode.parent=node;
                                    node.right = newNode;
                                    queue.add(node.right);
                                }
                                break;
                            }
                        }
                    }
                }

                public void inorderTraversal(Node node) {
                    if (root == null) {
                        System.out.println("Tree is empty");
                        return;
                    } else {
                        System.out.print(node.data + "\n");
                        if (node.left != null)
                            inorderTraversal(node.left);
                        if (node.right != null) {
                            inorderTraversal(node.right);
                        }
                    }
                }
            } // InnerClass: Binary Tree Implementaion - Ends
}
包包装类;
导入java.io.*;
导入java.util.*;
导入java.text.*;
导入java.math.*;
导入java.util.regex.*;
导入Collections.BinaryTree.Node;
//类名应为“源”,
//否则,该解决方案将不被接受
公共类源3\u Try2{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
//声明变量
INTA;
//从STDIN读取变量
a=in.nextInt();
字符串键,值;
HashMap hm=新的HashMap();
ArrayList键列表=新的ArrayList();
for(int i=0;i
CL输入:
5
伦敦-巴黎
伦敦罗马
巴黎-柏林
巴黎阿姆斯特丹
罗马威尼斯
伦敦

预期输出:
伦敦
巴黎
柏林
阿姆斯特丹
罗马
威尼斯

包装类;
package WrapperClasses;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.text.*;
import java.math.*;
import java.util.regex.*;

import Collections.BinaryTree.Node;

// Class name should be "Source",
// otherwise solution won't be accepted
public class Source3_MyOptimization {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // Declare the variable
        int a;
        // Read the variable from STDIN
        a = in.nextInt();
        String key, value;
        HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
        ArrayList<String> keyList = new ArrayList<String>();
        for (int i = 0; i < a; i++) {
            ArrayList<String> al = new ArrayList<String>();
            key = in.next();
            value = in.next();
            if (hm.containsKey(key)) {
                al = hm.get(key);
            } else {
                keyList.add(key);
            }
            al.add(value);
            hm.put(key, al);
        }

        System.out.println("hm: " + hm);

        for (Map.Entry<String, ArrayList<String>> hmEntry : hm.entrySet()) {
            String key1 = hmEntry.getKey();
            ArrayList<String> value1 = hmEntry.getValue();
            System.out.println(key1 + " :" + value1);
        }

        for (String key2 : hm.keySet()) {
            ArrayList<String> value2 = hm.get(key2);
            System.out.println(key2 + " :" + value2);
        }

        Iterator itr = hm.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry<String, ArrayList<String>> hmE = (Map.Entry<String, ArrayList<String>>) itr.next();
            String key3 = hmE.getKey();
            ArrayList<String> value3 = hmE.getValue();
            System.out.println(key3 + " :" + value3);
        }

        Iterator itr2 = hm.keySet().iterator();
        while (itr2.hasNext()) {
            String key4 = (String) itr2.next();
            ArrayList<String> value4 = hm.get(key4);
            System.out.println(key4 + " :" + value4);
        }

        String f = in.next();
        BinaryTree bt = new BinaryTree();
        bt.insertNode(f);
        ArrayList<String> valueList = hm.get(f);
        Queue<String> queue = new LinkedList<String>();
        queue.addAll(valueList);
        queueForInsert(bt, hm, queue);
        bt.inorderTraversal(bt.root);
    }

    public static void queueForInsert(BinaryTree bt, HashMap<String, ArrayList<String>> hm, Queue<String> queue) {
        while(!queue.isEmpty()){
        bt.insertNode(queue.peek());
        if(hm.containsKey(queue.peek())){
        queue.addAll(hm.get(queue.remove()));
        }
        else{
            queue.remove(); 
        }
        queueForInsert(bt, hm, queue);
        }
    }

    // InnerClass: Binary Tree Implementaion - Starts
    static class BinaryTree {
        class Node {
            String data;
            Node left;
            Node right;
            Node parent;

            public Node(String data) {
                this.data = data;
                this.left = null;
                this.right = null;
            }
        }

        public Node root;

        public BinaryTree() {
            root = null;
        }

        public void insertNode(String data) {
            Node newNode = new Node(data);
            if (root == null) {
                root = newNode;
                return;
            } else {
                Queue<Node> queue = new LinkedList<Node>();
                queue.add(root);
                while (true) {
                    Node node = queue.remove();
                    if (node.left != null && node.right != null) {
                        queue.add(node.left);
                        queue.add(node.right);
                    } else {
                        if (node.left == null) {
                            newNode.parent = node;
                            node.left = newNode;
                            queue.add(node.left);
                        } else {
                            newNode.parent = node;
                            node.right = newNode;
                            queue.add(node.right);
                        }
                        break;
                    }
                }
            }
        }

        public void inorderTraversal(Node node) {
            if (root == null) {
                System.out.println("Tree is empty");
                return;
            } else {
                System.out.print(node.data + "\n");
                if (node.left != null)
                    inorderTraversal(node.left);
                if (node.right != null) {
                    inorderTraversal(node.right);
                }
            }
        }
    } // InnerClass: Binary Tree Implementaion - Ends
}
导入java.io.*; 导入java.util.*; 导入java.util.Map.Entry; 导入java.text.*; 导入java.math.*; 导入java.util.regex.*; 导入Collections.BinaryTree.Node; //类名应为“源”, //否则,该解决方案将不被接受 公共类源3\u MyOptimization{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(系统输入); //声明变量 INTA; //从STDIN读取变量 a=in.nextInt(); 字符串键,值; HashMap hm=新的HashMap(); ArrayList键列表=新的ArrayList(); for(int i=0;i