A SHED UNDER THE TREE DATA STRUCTURE

TREE DATA STRUCTURE TERMINOLOGIES

  • Root — The first node of the tree
  • Path — Sequence of linked nodes
  • Parent — The preceding node
  • Child — A sub-node
  • Internal Node — A Node linked to parent and child nodes
  • Leaf Node — A Node without a sub-node
  • Generations — All Nodes in the same level
  • Siblings — Nodes with the same parent
  • Height — Number of levels from a root node to a leaf node

HOW TREES ARE STRUCTURED

THE IMPLEMENTATION OF THE TREE DATA STRUCTURE (BINARY SEARCH TREE)

class Tree:
def __init__(self, data):
self.data = data
self.left_node = None
self.right_node = None
def printer(self): #prints the tree
if self.left_node:
self.left_node.printer()
print(self.data)
if self.right_node:
self.right_node.printer()

OPERATIONS ON THE BINARY SEARCH TREE DATA STRUCTURE

A binary search tree

INSERTING ELEMENTS TO THE DATA STRUCTURE

A binary search tree
class Tree:
def __init__(self, data):
self.data = data
self.left_node = None
self.right_node = None

def insert(self, data):
if self.data:
if data < self.data:
if self.left_node is None:
self.left_node = Tree(data)
else:
self.left_node.insert(data)

elif data >= self.data:
if self.right_node is None:
self.right_node = Tree(data)
else:
self.right_node.insert(data)
else:
self.data = data

SEARCHING FOR AN ELEMENT IN THE BINARY SEARCH TREE

def find(self, data):
if self.data:
if data < self.data:
if self.left_node is None:
return "Data Doesn't Exist"
return self.left_node.find(data)
elif data > self.data:
if self.right_node is None:
return "Data Doesn't Exist"
return self.right_node.find(data)

else:
return "Found it"
else:
return "Empty Tree"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ukeje Chukwuemeriwo Goodness

Ukeje Chukwuemeriwo Goodness

Mechanical Engineering Student. Interested in Computational Sciences, Human Philosophy and Psychology.