Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary tree from a list
#1
Hi all,

I need to create a binary tree, whose data are stored in a list:

[[parent1, one_of_children_from_1],[parent2, one_of_children_from_2],[parent1, one_of_children_from_1],...]   #they are in random order
Can you give me any hint, what should I use in order to create such a binary tree?
Reply
#2
Well, you could implement it as a list, with some appropriate functions. I would go for a class implementation, however. Binary trees are a common programming structure, so there is lots of information about them online.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Well, I'm using a class already, but I need some hint in the aspect of logic - how to create it? I was already thinking about using a queue, could this be useful in my case?
Reply
#4
I would use a dictionary of values to node instances. Then as you go through the list you know which values already have nodes, and what nodes you need to create/join.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
So, actually, I can create this tree in a way, where I create a lot of "subtrees" and then while iterating in a list just joining them, right?
Reply
#6
Yeah. Each node has two children, which are None or another node. First you get A as the parent of B, make nodes A and B, with B as a child of A. Then you get C as the parent of D, do the same thing. When you get B as the parent of C, you know from your dictionary that you already have a B node and a C node, so you make the existing C node the child of the existing B node.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Well, I get this logic, but here I meet with the following trouble:

dictionary = {parent1 : (1child1, 1child2),parent3 : (3child1, 3child2),parent2 : (2child1, 2child2),...}
self.root = self.Node(head)   # I already have a algorithm to find a head of the whole tree
parents = list(dictionary)
p = 0
while (p != len(parents)-1):
      # --- here I want to write a described algorithm, but I´m not sure if the system of curr_node is right
      # ... it doesn´t create the right connection between nodes
      curr_node = self.Node(parents[p], self.Node(dictionary.get(parents[p])[0]),self.Node(dictionary.get(parents[p])[1]))
      p += 1
Reply
#8
No, the dictionary starts empty. You have no head at the beginning. Taking the list from your original post as data:

nodes = {}
for parent, child in data:
    if parent not in nodes:
        nodes[parent] = Node(parent)
    if child not in nodes:
        nodes[child] = Node(child)
    # join nodes[parent] to node[child]
# Figure out head
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Thanks, that works (I also added following lines of code).

Now I met with one another trouble; I shouldn´t use a self attribute whenever in that class (except self.root). I need to use dictionary "nodes" also in other method, can you give me any hint, how to "transport" this dictionary there, without making an attribute from that?
Reply
#10
I don't understand the question. Why can't you use self? What method needs the dictionary? It would help if I could see the code for you nodes/tree.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 604 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  Binary tree AlexPython 3 994 Dec-07-2022, 06:41 AM
Last Post: ndc85430
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,499 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  Python3 binary tree not replacing parent nodes with child nodes Aspect11 0 1,779 Sep-23-2020, 02:22 PM
Last Post: Aspect11
  search binary file and list all founded keyword offset Pyguys 4 2,784 Mar-17-2020, 06:46 AM
Last Post: Pyguys
  hex file to binary or pcap to binary baran01 1 5,706 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Pack binary data to list of integers bearer 0 2,155 Mar-29-2019, 12:17 PM
Last Post: bearer
  Convert tree to list of lists Dylan_T_Rabbit 2 11,753 Jul-12-2017, 02:09 PM
Last Post: Dylan_T_Rabbit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020