-
Notifications
You must be signed in to change notification settings - Fork 4
/
BinaryTree.py
58 lines (42 loc) · 1.52 KB
/
BinaryTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#Author: Mateen SA
#Binary Tree implementation using nodes and edges
class BinaryTree:
def __init__(self, root):
self.root = root;
self.leftChild = None;
self.rightChild = None;
def setLeftChild(self, root, newNodeValue):
node = BinaryTree(newNodeValue);
if self.leftChild == None:
self.leftChild = node;
else:
node.leftChild = self.leftChild;
self.leftChild = node;
def setRightChild(self, root, newNodeValue):
node = BinaryTree(newNodeValue);
if self.rightChild == None:
self.rightChild = node;
else:
node.rightChild = self.rightChild;
self.rightChild = node;
def getLeftChild(self):
return self.leftChild;
def getRightChild(self):
return self.rightChild;
def getRootValue(self):
return self.root;
def setRootValue(self, newRootValue):
self.root = newRootValue;
def getNodeValue(self, node):
return BinaryTree(node).getRootValue();
btree = BinaryTree('a');
btree.setRootValue('A');
print(btree.getRootValue());
btree.setLeftChild('B', btree);
print(btree.getLeftChild().getRootValue());
btree.setRightChild('C', btree);
print(btree.getRightChild().getRootValue())
btree.setLeftChild('D', btree.getLeftChild())
print(btree.getLeftChild().getLeftChild().getRootValue())
btree.setLeftChild('E', btree.getRightChild());
btree.setRightChild('F', btree.getRightChild())