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))
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
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
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
1Binary Tree implementation at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees
1Binary Search Tree at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/BinaryTrees