How to implement a binary tree? How to implement a binary tree? python python

How to implement a binary tree?


Here is my simple recursive implementation of binary search tree.

#!/usr/bin/pythonclass Node:    def __init__(self, val):        self.l = None        self.r = None        self.v = valclass Tree:    def __init__(self):        self.root = None    def getRoot(self):        return self.root    def add(self, val):        if self.root is None:            self.root = Node(val)        else:            self._add(val, self.root)    def _add(self, val, node):        if val < node.v:            if node.l is not None:                self._add(val, node.l)            else:                node.l = Node(val)        else:            if node.r is not None:                self._add(val, node.r)            else:                node.r = Node(val)    def find(self, val):        if self.root is not None:            return self._find(val, self.root)        else:            return None    def _find(self, val, node):        if val == node.v:            return node        elif (val < node.v and node.l is not None):            return self._find(val, node.l)        elif (val > node.v and node.r is not None):            return self._find(val, node.r)    def deleteTree(self):        # garbage collector will do this for us.         self.root = None    def printTree(self):        if self.root is not None:            self._printTree(self.root)    def _printTree(self, node):        if node is not None:            self._printTree(node.l)            print(str(node.v) + ' ')            self._printTree(node.r)#     3# 0     4#   2      8tree = Tree()tree.add(3)tree.add(4)tree.add(0)tree.add(8)tree.add(2)tree.printTree()print(tree.find(3).v)print(tree.find(10))tree.deleteTree()tree.printTree()


# simple binary tree# in this implementation, a node is inserted between an existing node and the rootclass BinaryTree():    def __init__(self,rootid):      self.left = None      self.right = None      self.rootid = rootid    def getLeftChild(self):        return self.left    def getRightChild(self):        return self.right    def setNodeValue(self,value):        self.rootid = value    def getNodeValue(self):        return self.rootid    def insertRight(self,newNode):        if self.right == None:            self.right = BinaryTree(newNode)        else:            tree = BinaryTree(newNode)            tree.right = self.right            self.right = tree    def insertLeft(self,newNode):        if self.left == None:            self.left = BinaryTree(newNode)        else:            tree = BinaryTree(newNode)            tree.left = self.left            self.left = treedef printTree(tree):        if tree != None:            printTree(tree.getLeftChild())            print(tree.getNodeValue())            printTree(tree.getRightChild())# test treedef testTree():    myTree = BinaryTree("Maud")    myTree.insertLeft("Bob")    myTree.insertRight("Tony")    myTree.insertRight("Steven")    printTree(myTree)

Read more about it Here:-This is a very simple implementation of a binary tree.

This is a nice tutorial with questions in between


[What you need for interviews] A Node class is the sufficient data structure to represent a binary tree.

(While other answers are mostly correct, they are not required for a binary tree: no need to extend object class, no need to be a BST, no need to import deque).

class Node:    def __init__(self, value = None):        self.left  = None        self.right = None        self.value = value

Here is an example of a tree:

n1 = Node(1)n2 = Node(2)n3 = Node(3)n1.left  = n2n1.right = n3

In this example n1 is the root of the tree having n2, n3 as its children.

enter image description here