1. 程式人生 > >使用遞迴演算法從給定樹上任意幾個節點將這幾個節點的所有下級 和 上級返回出來

使用遞迴演算法從給定樹上任意幾個節點將這幾個節點的所有下級 和 上級返回出來

  1. 首先,建立名稱為Node的節點類,用來存放節點屬性
    import java.util.ArrayList;
    
    public class Node {
    	public String ID = null;       //節點id
    	public String PID = null;		//父節點id
    	public Node(String ID1, String ID2)
    	{
    		this.ID = ID1;
    		this.PID = ID2;
    	}
    	//定義上一節點變數
    	public Node upNode = null;
    	//定義向下節點集合
    	public ArrayList<Node> subList = new ArrayList<Node>();
    }
    

  2. 其次,建立名稱為Test的處理類,用來進行遞迴實現題目運算
    import java.util.ArrayList;
       import java.util.HashMap;
       import java.util.List;
       public class Test
       {
    	public static void main(String[] args)
    	{	
    		//建立集合,建立題目中的樹形結構
    		List<Node> list = new ArrayList<Node>();
    		list.add(new Node("1", ""));
    		list.add(new Node("11", "1"));
    		list.add(new Node("12", "1"));
    		list.add(new Node("111", "11"));
    		list.add(new Node("1111", "111"));
    		list.add(new Node("121", "12"));
    		list.add(new Node("122", "12"));
    		list.add(new Node("1211", "121"));
    		list.add(new Node("1221", "122"));
    		
            //使用hashmap存放節點
    		HashMap<String, Node> nodeMap = new HashMap<String, Node>();
    		for (Node node : list)
    		{
    			//將ID對應的node放到HashMap中
    			nodeMap.put(node.ID, node);
    		}
    	
    		//建立動態陣列,用來處理頂級節點“1”
    		List<Node> toplist = new ArrayList<Node>();
    		
    		for (Node node : list)
    		{
    			if(node.PID != null && !"".equals(node.PID))
    			{
    				Node upNode = nodeMap.get(node.PID);
    				if(upNode != null)
    				{
    					// 新增下級
    					upNode.subList.add(node);
    					// 設定上級
    					node.upNode = upNode;
    				}else
    				{
    					toplist.add(node);
    				}
    			}
    			else
    			{
    				toplist.add(node);
    			}
    		}
    		
    		// 給定節點 111,則返回資料有 1,11,111,1111
    	
    		System.out.println(getNode("111", nodeMap));
    
    		// 給定節點 121,則返回1,12,121,1211
    		System.out.println(getNode("121", nodeMap));
    
    		// 給定節點 12,則返回1,12,121,1211,122,1221
    		System.out.println(getNode("12", nodeMap));
    
    		// 給定節點 111 和 1211,則返回1,11,111,111,12,121,1211
    		System.out.println(getNode("1211", nodeMap));
    	}
    
    	private static String getNode(String id, HashMap<String, Node> nodeMap)
    	{
    		Node node = nodeMap.get(id);
    		if(node == null)
    		{
    			return "Node不存在!";
    		}
    		else
    		{
    			List<Node> resultList = new ArrayList<Node>();
    			getNodeUpNode(node, resultList);
    			getNodeSubNode(node, resultList);
    			boolean bFirst = true;
    			StringBuffer sb = new StringBuffer();
    			for (Node t : resultList)
    			{
    				if(bFirst)
    				{
    					bFirst = false;
    				}
    				else
    				{
    					sb.append(",");
    				}
    				sb.append(t.ID);
    			}
    			return sb.toString();
    		}
    		
    		
    	}
    	//獲取給定節點的上級節點
    	private static void getNodeUpNode(Node node, List<Node> resultList)
    	{
    		if(node.upNode != null)
    		{
    			//遞迴呼叫
    			getNodeUpNode(node.upNode, resultList);
    		}
    		resultList.add(node);
    
    	}
    	//獲取給定節點的下級節點
    	private static void getNodeSubNode(Node node, List<Node> resultList)
    	{
    		for (Node subNode : node.subList)
    		{
    			//遞迴呼叫
    			resultList.add(subNode);
    			getNodeSubNode(subNode, resultList);
    			
    		}
    	}
       }
    
  3. 最後,執行即可輸出如題結果