title: How can I construct a process tree?
body: <p>I have a list of processes.</p>
  <p>I want to take this process list and organize it in a tree structure so that I can easily visualize parent-child relationships, and identify suspect or unwanted processes.</p>
  <p>However, I am struggling to come up with an algorithm or pseudo code to create the process tree.</p>
  <p>I can't use tools like pstree because I'm dealing with textual process logs.</p>
  <p>Can you point me to any data structures or algorithms that can be applied to this problem?</p>
  <p>Here's some sample code to illustrate the problem:</p>
  <pre><code>class Process():
      def __init__(self, name, pid, parent_pid):
          self.name = name
          self.pid = pid
          self.ppid = parent_pid
  
  # assume you have a process list with these fields:
  # process name, PID, Parent PID
  proc1 = Process(&quot;one&quot;, 1, 0)
  proc2 = Process(&quot;two&quot;, 2, 1)
  proc3 = Process(&quot;three&quot;, 3, 2)
  proc4 = Process(&quot;four&quot;, 4, 3)
  proc5 = Process(&quot;five&quot;, 5, 3)
  proc6 = Process(&quot;six&quot;, 6, 2)
  
  # append each process to a list
  process_list = [proc1, proc2, proc3, proc4, proc5, proc6]
  
  for each_proc in process_list:
      # how do I do this?
      get_child_processes()
      display_as_tree()
  
  &quot;&quot;&quot;
  Desired output:
  
  one
  |
  |__two
     |
     |__three
     |   |
     |   |_four
     |   |
     |   |_five
     |
     |__six
  
  
  &quot;&quot;&quot;
              
  </code></pre>