Cannot add BinarySubTree

  binary-tree, data-structures, python

It works well with only attach left or right tree. But when it comes to attach sub tree it turns out not successfully added. Here is the code:

class BinaryTree():
class BinaryTreeNode():
    def __init__(self,leftchildnode,rightchildnode,item):
        self.leftchildnode = leftchildnode
        self.rightchildnode = rightchildnode
        self.item = item

def __init__(self):
    self.root = None
    
def setRoot(self,item):
    self.cur = self.BinaryTreeNode(None,None,item)
    self.root = self.cur

def attachLeft(self,item):
    self.left = self.BinaryTreeNode(self.root.leftchildnode,None,item)
    self.root.leftchildnode = self.left

def attachRight(self,item):
    self.right = self.BinaryTreeNode(None,self.root.rightchildnode,item)
    self.root.rightchildnode = self.right

def attachLeftSub(self,Binarytree):
    global BinaryTree
    self.root.leftchildnode = BinaryTree
    BinaryTree = None
    
def attachRightSub(self,Binarytree):
    global BinaryTree
    self.root.rightchildnode = BinaryTree
    BinaryTree = None

def inorderdisplay(self):
    self.recursive_inorder_display(self.root)

def recursive_inorder_display(self,BinaryTreeNode):
    if BinaryTreeNode != None:
        self.recursive_inorder_display(BinaryTreeNode.leftchildnode)
        print(BinaryTreeNode.item)
        self.recursive_inorder_display(BinaryTreeNode.rightchildnode)

tree = BinaryTree()
tree.setRoot("J")
tree1 = BinaryTree()
tree1.setRoot("D")
tree1.attachLeft("E")
tree1.attachRight("F")
tree2 = BinaryTree()
tree2.setRoot("G")
tree2.attachLeft("H")
tree2.attachRight("I")
tree.attachLeftSub(tree1)
tree.attachLeftSub(tree2)
tree.inorderdisplay()

It should have show all the item in inorder but the only item I get is J. Can you point out wheres the error?

Source: Python Questions

LEAVE A COMMENT