A binary tree is a tree-like structure that is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element.
The topmost node in the tree is called the root. The root has no parent. There is precisely one root.
There is no cycle.
Each node can point to two other different nodes, called its left child and its right child. If a node c is the child of another node p, then we say that "p is c's parent".
Each node, except the root, has exactly one parent; the root has no parent.
Nodes with no children are called leaves, or external nodes.
Nodes that are not leaves are called internal nodes.
Nodes with the same parent are called siblings.
The depth of a node is the number of edges from the root to the node.
The height of a node is the number of edges from the node to the deepest leaf.
The height of a tree is a height of the root.
Node Class
class node:
def __init__(self, new_data):
self.data = new_data
self.left = None //Reference to the left child
self.right = None //Reference to the right child
def __str__(self):
return str(self.data)
def getdata(self):
return self.data
def getleft(self):
return self.left
def getright(self):
return self.right
def setleft(self, new_node):
self.left = next_node
def setright(self, new_node):
self.right = next_node
Binary Tree Class
class binary_tree:
def __init__(self):
self.root = None
<to be updated>