-1

i want to create a binary tree program but the final result returns unwanted result .i tried to compare the values ​​in it but i think it's the wrong way to get the right result.

the binary tree should be show the output :

15 25 20 30 10 7 13 14 6 9 11 17 27 32 23
root node : 6

this my program when i was create:

class Node:
  def __init__(self, value, left=None, right=None):
    self.left=None
    self.value=value
    self.right=None
  def insert(self, value):
    if self.value:
      if value < self.value:
        if self.left is None:
          self.left = Node(value)
        else:
          self.left.insert(value)
      elif value > self.value:
        if self.right is None:
          self.right = Node(value)
        else:
          self.right.insert(value)
    else:
      self.value = value
  def printTree(self):
    if self.left:
      self.left.printTree()
      print( self.value),
    if self.right:
      self.right.printTree()

root=Node(6)

root.insert(15)
root.insert(25)
root.insert(20)
root.insert(30)
root.insert(10)
root.insert(7)
root.insert(13)
root.insert(14)

root.insert(9)
root.insert(11)
root.insert(17)
root.insert(27)
root.insert(32)
root.insert(23)

root.printTree()

i need your opinion to fix this program.

ryand
  • 121
  • 10
  • What is your *expected* output?? There are three ways to present or traverse through a binary tree: InOrder PreOrder and PostOrder. please be specific of how would you like your tree to be shown please. – Or b Apr 23 '21 at 07:18
  • the final result show : 15 25 20 30 10 7 13 14 6 9 11 17 27 32 23 – ryand Apr 23 '21 at 07:23
  • 1
    you are sure you want to do that from scratch ? thought xml.etree.ElementTree could work for you – user3732793 Apr 23 '21 at 07:29
  • @rey why do you want to show the nodes in the same order you place them. while you can simply use a Queue instead. – Or b Apr 23 '21 at 07:31

1 Answers1

0
self.left.insert(value) and self.right.insert(value) is the wrong way

just do

self.left = value
self.right = value

becaues you are in the same class there is no need to call a function for this task.

and than have a look at your state machine:

if self.left is None:
and 
if self.right is None:

This only matches if the value is None after the first time assigning values to it, than all code in this if is completly ignored.

Another thing:

self.left = Node(value)

assigns the class Node with a value to your local inner class value left which is not want you desire, you just simply wants the value assigned to self.left

Hopefully it helps?

  • so i delete all if else statement and only put if self.left is None:and and if self.right is None: in insert function? – ryand Apr 23 '21 at 07:44
  • just google it https://stackoverflow.com/questions/2598437/how-to-implement-a-binary-tree ;) – Christoph Schwalbe Apr 25 '21 at 12:01
  • There is nice presentation here: http://www.pythonschool.net/data-structures-algorithms/binary-tree/ The Tree is a data structure and needs somehow knowlegde about: "how are my items are organized?" "So my tree have Nodes?" --> "how can I reach them?" "do I have something in left? do I have something in right?" "is there an end?" --> if the left is None --> there is a left end --> if the right is None --> there is a right end – Christoph Schwalbe Apr 25 '21 at 12:06
  • And to print out those Node data, just print it if left is not None or if right is not Node than go a level deeper in the Tree and ask the Node again: "Do I have something in left? Do I have something in right?" That is all. The binary tree it self, needs to know about it can have Nodes and add them, delete them, print them. – Christoph Schwalbe Apr 25 '21 at 12:08