binary tree python

Solutions on MaxInterview for binary tree python by the best coders in the world

showing results for - "binary tree python"
Adriana
23 Oct 2017
1class Binarytree:
2    def __init__(self,data):
3        self.data = data
4        self.left = None
5        self.right = None
6    
7    def addChild(self, data):
8        if data == self.data:
9            return
10        
11        if data < self.data:
12            if self.left:
13                self.left.addChild(data)
14            else:
15                self.left = Binarytree(data)
16        else:
17            if self.right:
18                self.right.addChild(data)
19            else:
20                self.right = Binarytree(data)
21    
22    def inorder(self):
23        element = [ ]
24        
25        if self.left:
26            element += self.left.inorder()
27        
28        element.append(self.data)
29        
30        if self.right:
31            element += self.right.inorder()
32        
33        return element
34    
35    def search(self,val):
36        if val == self.data:
37            return True
38        if val < self.data:
39            if self.left:
40                return self.left.search(val)
41            else:
42                return False
43        else:
44            if self.right:
45                return self.right.search(val)
46            else:
47                return False
48
49def buildtree(element):
50    root = Binarytree(element[0])
51    for i in range(1,len(element)):
52        root.addChild(element[i])
53    return root
54    
55if __name__ == '__main__':
56    element = [39, 87, 21, 42, 95, 52, 12]
57    tree = buildtree(element)
58    print(tree.inorder())
59    print(tree.search(38))
Daphné
31 Jun 2019
1class BinaryTree:
2
3    def __init__(self, value):
4
5        self.left = None
6        self.right = None
7        self.value = value
8
9    def insert(self, value):
10
11        if self.value:
12            if data < self.value:
13                if self.left is None:
14                    self.left = BinaryTree(value)
15                else:
16                    self.left.insert(value)
17            elif data > self.value:
18                if self.right is None:
19                    self.right = BinaryTree(value)
20                else:
21                    self.right.insert(value)
22        else:
23            self.value = value
24
25    def PrintTree(self):
26        if self.left:
27            self.left.PrintTree()
28        print( self.data),
29        if self.right:
30            self.right.PrintTree()
31
32root = BinaryTree(100)
33root.insert(50)
34root.insert(55)
35root.insert(60)
36root.insert(20)
37root.insert(52)
38
39
40root.PrintTree()
41PythonCopy
Johanna
20 Jan 2020
1// Binary Tree in Golang
2package main
3  
4import (
5    "fmt"
6    "os"
7    "io"
8)
9  
10type BinaryNode struct {
11    left  *BinaryNode
12    right *BinaryNode
13    data  int64
14}
15  
16type BinaryTree struct {
17    root *BinaryNode
18}
19  
20func (t *BinaryTree) insert(data int64) *BinaryTree {
21    if t.root == nil {
22        t.root = &BinaryNode{data: data, left: nil, right: nil}
23    } else {
24        t.root.insert(data)
25    }
26    return t
27}
28  
29func (n *BinaryNode) insert(data int64) {
30    if n == nil {
31        return
32    } else if data <= n.data {
33        if n.left == nil {
34            n.left = &BinaryNode{data: data, left: nil, right: nil}
35        } else {
36            n.left.insert(data)
37        }
38    } else {
39        if n.right == nil {
40            n.right = &BinaryNode{data: data, left: nil, right: nil}
41        } else {
42            n.right.insert(data)
43        }
44    }   
45}
46  
47func print(w io.Writer, node *BinaryNode, ns int, ch rune) {
48    if node == nil {
49        return
50    }
51  
52    for i := 0; i < ns; i++ {
53        fmt.Fprint(w, " ")
54    }
55    fmt.Fprintf(w, "%c:%v\n", ch, node.data)
56    print(w, node.left, ns+2, 'L')
57    print(w, node.right, ns+2, 'R')
58}
59  
60func main() {
61    tree := &BinaryTree{}
62    tree.insert(100).
63        insert(-20).
64        insert(-50).
65        insert(-15).
66        insert(-60).
67        insert(50).
68        insert(60).
69        insert(55).
70        insert(85).
71        insert(15).
72        insert(5).
73        insert(-10)
74    print(os.Stdout, tree.root, 0, 'M')
75}
76
Viktoria
27 Oct 2020
1class Binary:
2    def __init__(self, data):
3        self.data = data
4        self.left = None
5        self.right = None
6    
7    def addChild(self, data):
8        if data == self.data:
9            return
10        if data < self.data:
11            if self.left:
12                self.left.addChild(data)
13            else:
14                self.left = Binary(data)
15        else:
16            if self.right:
17                self.right.addChild(data)
18            else:
19                self.right = Binary(data)
20    
21    def inorder(self):
22        ele = []
23        
24        if self.left:
25            ele += self.left.inorder()
26        ele.append(self.data)
27        
28        if self.right:
29            ele += self.right.inorder()
30        
31        return ele
32    
33    def search(self, data):
34        if data == self.data:
35            return True
36        if data < self.data:
37            if self.left:
38                return self.left.search(data)
39            else:
40                return False
41        else:
42            if self.right:
43                return self.right.search(data)
44            else:
45                return False
46    
47    def find_min(self):
48        if self.left is None:
49            return self.data
50        return self.left.find_min()
51    
52    def find_max(self):
53        if self.right is None:
54            return self.data
55        return self.right.find_max()
56    
57    def delete(self, val):
58        if val < self.data:
59            if self.left:
60                self.left = self.left.delete(val)
61        elif val > self.data:
62            if self.right:
63                self.right = self.right.delete(val)
64        else:
65            if self.left is None and self.right is None:
66                return None
67            if self.left is None:
68                return self.right
69            if self.right is None:
70                return self.left
71            
72            min_val = self.right.find_min()
73            self.data = min_val
74            self.right = self.right.delete(val)
75        
76        return self
77
78
79
80def build(element):
81    root = Binary(element[0])
82    for i in range(1,len(element)):
83        root.addChild(element[i])
84    return root
85
86if __name__ == '__main__':
87    element = [32, 89, 12, 94, 23, 61, 2]
88    tree = build(element)
89    print(tree.inorder())
90    print(tree.search(62))
91    print(tree.find_min())
92    print(tree.find_max())
93    tree.delete(12)
94    print(tree.inorder())
95    
Raphael
13 Oct 2016
1Binary Tree implementation at this link:
2  
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
Eugenie
20 Jan 2020
1Binary Search Tree at this link:
2  
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
queries leading to this page
python non binary treehow to create a binary search tree in pythonhow to create binary search tree in pythonhow to make binary tree pythonpython binary treesbinary tree code example pythonhow to remove binary tree node pythonbinary tree in golangbinary tree in pythonssbinary tree in golangcode for binary search tree in pythonwhat is binary search tree in pythongolang treebinary tree model in pythonbinary search tree in pythonbinary tree implementation in pythonbinary search tree using gobinary search trees in pythonnode from binary tree module in pythongolang non binary treepython program for binary search treewhat is a binary tree in pythonbinary tree python packagehow to make a binary tree in pythonbinary tree in python geeksforgeeksis binary tree a binary search tree pythontree golangpython binary tree syntaxgolang binary treesgolang binary search treebinary search tree implementation in pythonbinary tree program in pythonpython search tree implementationbinary tree python arraygo map binary treehow to do a binary tree in pythonhow to write a binary tree in pythonbinary tree in pythonsbinary tree representation as array pythonbinasy search tree in pythonpython binary tree examplebinary tree structure pythonbinary tree pythombinary trees in pythonfull binary search tree implementation pythoncode for binary tree in pythonpython binairy treepython program to implement binary search treepython binary search tree implementationbinary tree in pythonpython tree searchbinary serach tree pyhtonsearch elements in binary tree with pythonsscreate a binary tree in pythonarray binary tree pythonbinary treees in pythonsearch tree in pythonbinary tree python formulabinary tree values in a list pythonbst tree golangtree ds in pythoin binary tree data structure in pythonbuild binary tree in pythoncomplete binary treecreation code in pythonbinary tree golang examplebinary tree example pythonbinary search tree algorithm in pythoncreate binary tree using pythonpython binary tree librarydeclare binary tree in pythonbinary tree library pythonis this a binary search tree pythonsearch element in binary tree in pythonbinary tree node pythonbinary search tree class pythonbinary tree pypyton binary treebinary tree python librarybig binary tree java with nullsbinary search tree python searchgolang binary tree librarypython binary search tree librarybinary search tree using pythonbinary tree node python examplehow to make a binary search tree with an array pythonbinary search tree inplementation in pythonbinary tree python how to guidetree search in pythondefine binary tree in pythonpython binary search treebinary tree code pythonpython binary treebinary search tree tree pythonbinary tree class pythonbinary tree class python nodebst tree in pythonprint binary search tree pythonbinary tree algorithm in pythonbinary tree in pythinsearch element in binary tree pythongo binary treepython binary tree searchbinary tree with pythonbuilding a binary tree pythonhow to get element in a binary tree pythonbinary tree data structure implementation pythonbinary tree codehow to code a binary tree in pythonbinary search tree 28bst 29 implementation in pythongolang binary treesearching in binary search tree pythonbinary tree python real pythonbin tree in pythonbinary search tree isnert ptyhonhow to build a binary tree in pythonis there a binary tree built in in pythonpython binary tree codepython import binary treecomplete binary tree python python binarytreethe binary tree pythontree search pythonbinary tree in pytonbinary tree python codebst tree code pythonbinary tree search in data structure pythonpython binary serch treebinary tree in data structure with pythonhow to print a binary tree in pythonpython create a binary treebinary trees data structure pythonbinsary search tree python 5cbuild a binary tree in pythonhow to display binary tree in pythonbinary search tree in golangbst tree pythonbinary tree 28array implementation 29 in pythonsearch tree algorithm pythonpython biary treepthon binary treego language binary treeeimplement a binary search tree in pythonbinary tree pythonbinary tree using pythonhow to construct a binary tree in pythonsearch element in binary tree by pythonhow to work with binary tree in pythondjango binary tree modelbinary tree in python modulecreating a binary tree in pythonare binary trees used in pythonbinary tree using dictionary in python binary tree python 5cgolang binary treebinary trees pythonhow to handle binary tree in pythoncreate binary tree pythonbinary search tree python usessearch the value in binary tree pythonbinary search tree 28bst 29 pythonbinary search tree data structure in pythonhow to make binary tree in pythontree in gobinary tree module in pythonpython code that implements a binary tree data structureconstruct a binary tree in pythonbinary search tree with golangtree binary search tree in pythonpython binary tree nodebinary tree search algorithm pythonhow to write binary tree class pythongolang tree nodewhat is a binary tree pythonbinary search tree pythonpython binary tree implementationbinary search tree python codebinary tree in data structure program pythonbinary tree implementation pythongolang binary tree examplebinary tree creation in pythonbinary tree golanggolang tree implementationimplementing binary search tree in pythonbinary tree python