Answers for "binary tree data structure python"

5

binary tree python

class Binarytree:
    def __init__(self,data):
        self.data = data
        self.left = None
        self.right = None
    
    def addChild(self, data):
        if data == self.data:
            return
        
        if data < self.data:
            if self.left:
                self.left.addChild(data)
            else:
                self.left = Binarytree(data)
        else:
            if self.right:
                self.right.addChild(data)
            else:
                self.right = Binarytree(data)
    
    def inorder(self):
        element = [ ]
        
        if self.left:
            element += self.left.inorder()
        
        element.append(self.data)
        
        if self.right:
            element += self.right.inorder()
        
        return element
    
    def search(self,val):
        if val == self.data:
            return True
        if val < self.data:
            if self.left:
                return self.left.search(val)
            else:
                return False
        else:
            if self.right:
                return self.right.search(val)
            else:
                return False

def buildtree(element):
    root = Binarytree(element[0])
    for i in range(1,len(element)):
        root.addChild(element[i])
    return root
    
if __name__ == '__main__':
    element = [39, 87, 21, 42, 95, 52, 12]
    tree = buildtree(element)
    print(tree.inorder())
    print(tree.search(38))
Posted by: Guest on August-03-2021
3

tree data structure in python

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.children = []
        self.parent = None

    def add_child(self, child):
        child.parent = self
        self.children.append(child)

    def getlevel(self):
        level = 0
        p = self.parent
        while p:
            level += 1
            p = p.parent
        return level

    def printt(self):
        prefix = (" " * 4 * self.getlevel()) + ("|--" if self.parent else "")
        print(prefix + self.data)
        if self.children:
            for child in self.children:
                child.printt()


def build_tree():
    root = TreeNode("Food")

    italy = TreeNode("Italy")
    italy.add_child(TreeNode("Pizza"))
    italy.add_child(TreeNode("Lasgna"))
    italy.add_child(TreeNode("Pistacho Ice"))

    chinese = TreeNode("Chineese")
    chinese.add_child(TreeNode("Noodles"))
    chinese.add_child(TreeNode("Rice balls"))
    chinese.add_child(TreeNode("Fried Rice"))

    mexican = TreeNode("Mexican")
    mexican.add_child(TreeNode('Tacos'))
    mexican.add_child(TreeNode('Gyro'))
    mexican.add_child(TreeNode('Shawarma'))

    root.add_child(italy)
    root.add_child(chinese)
    root.add_child(mexican)

    return root

    # mexican.printt()


if __name__ == "__main__":
    root = build_tree()
    root.printt()
Posted by: Guest on July-25-2021

Code answers related to "binary tree data structure python"

Python Answers by Framework

Browse Popular Code Answers by Language