How to Convert Adjacency list into binary tree?

Viewed 724

For my project i need to convert Adjacency list into binary tree. i'm using java language. i can't figure out how to do it. Any one knows how to do it or any document to refer??

2 Answers

If you know the adjacency list correspond to a binary tree then search for the root (only node with zero indegree). Later, perform a DFS (Depth First Search) starting at the root to create the tree. And, that is.

Try,

  1. Put the nodes into a sorted list or dictionary.
  2. Scan that list, pick up each node, find its parent node in the same list (binary search or dictionary lookup), add it to the Children collection of the parent node.
Related