Tree Operations print the ring in the directed graph, treeoperations

Source: Internet
Author: User

Tree Operations print the ring in the directed graph, treeoperations

Question:

You are given a binary tree with unique integer values on each node. however, the child pointers on each node may point to any other node in the tree including itself, introducing cycles into the binary tree. A cycle is defined when you can traverse back to the same node by following its descendants. write a function that takes in the root node of the tree and prints out the cycles, if any, in the binary tree. the only operations available on each node are node. left (returns another Node or null), node. right, and node. value (returns the integer value of the node ). pseudo code is fine.

Find the loops (loop or cycle) in the directed graph and print them all out.

Example: http://www.bkjia.com/uploads/allimg/140818/0U2301J5-0.png


Cycles: [1, 2, 4], [5], [3, 6]


Answer:

import java.util.ArrayList;public class Graph {    enum VertexState {        White, Gray, Black    }        public static void main(String[] args) {        Node node = new Node(0);        node.color = VertexState.White;        Node left = new Node(1);        left.color = VertexState.White;        node.left = left;        Node right = new Node(2);        right.color = VertexState.White;        Node rightright = new Node(3);               node.right = right;        left.left = node;        right.right = rightright;        rightright.right = node;                ArrayList<Node> list = new ArrayList<Node>();        ArrayList<ArrayList<Node>> ret = new ArrayList<ArrayList<Node>>();        rec(node, list, ret);        System.out.println(ret);    }    public static void rec(Node node, ArrayList<Node> list, ArrayList<ArrayList<Node>> ret) {        if(node.color == VertexState.Gray) {            ret.add(new ArrayList<Node>(list));            return;        }        node.color = VertexState.Gray;        list.add(node);        if(node.left != null && node.left.color != VertexState.Black) {            rec(node.left, list, ret);        }        if(node.right != null && node.right.color != VertexState.Black) {            rec(node.right, list, ret);        }        list.remove(list.size()-1);        node.color = VertexState.Black;    }        public static class Node {        int val;        Node left;        Node right;        VertexState  color;        public Node(int val_) {            val = val_;        }        @Override        public String toString() {            return this.val + "";        }    }}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.