0% found this document useful (0 votes)
7 views

Tree

Uploaded by

Yash Jariwala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Tree

Uploaded by

Yash Jariwala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

General Tree

In [1]:

class TreeNode:
def __init__(self,data) -> None:
self.data=data
self.children=[]
self.parent=None

def add_child(self,child):
child.parent=self
self.children.append(child)

def get_level(self):
level=0
p=self.parent
while p:
level+=1
p=p.parent
return level

def print_tree(self):
spaces=' ' * self.get_level() * 3
prefix=spaces+"|__" if self.parent else ""
print(prefix+self.data)
if self.children:
for child in self.children:
child.print_tree()

def build_product_tree():
root=TreeNode("Electronics")

laptop=TreeNode("Laptop")
laptop.add_child(TreeNode("Mac"))
laptop.add_child(TreeNode("Windows"))
laptop.add_child(TreeNode("Linux"))

phone=TreeNode("Smart Phone")
phone.add_child(TreeNode("iPhone"))
phone.add_child(TreeNode("Google Pixel"))
phone.add_child(TreeNode("Samsung"))

tv=TreeNode("TV")
tv.add_child(TreeNode("VU"))
tv.add_child(TreeNode("LG"))

root.add_child(laptop)
root.add_child(phone)
root.add_child(tv)
return root

root=build_product_tree()
root.print_tree()

Electronics
|__Laptop
|__Mac
|__Windows
|__Linux
|__Smart Phone
|__iPhone
|__Google Pixel
|__Samsung
|__TV
|__VU
|__LG

Binary Tree
each node has at most two children (left and right child)
Values in left subtree are less than the root value
Values in right subtree are greater than the root value
Values are unique (no duplicates)
Tree traversals (Depth First Search):

1. Inorder (left, root, right)


2. Preorder (root, left, right)
3. Postorder (left, right, root)
In [12]:

class BinarySearchTree:
def __init__(self,data) -> None:
self.data =data
self.left=None
self.right=None

# add child
def add_child(self,data):
if self.data== data:
return # node already exist

if data<self.data:
if self.left:
# insert into left subtree
self.left.add_child(data)
else:
self.left=BinarySearchTree(data)
else:
if self.right:
# insert into right subtree
self.right.add_child(data)
else:
self.right=BinarySearchTree(data)

def search(self, val):


if self.data == val:
return True

if val < self.data:


if self.left:
return self.left.search(val)
else:
return False

if val > self.data:


if self.right:
return self.right.search(val)
else:
return False

def in_order_traversal(self):
ele=[]
# visit left subtree
if self.left:
ele+=self.left.in_order_traversal()

ele.append(self.data)

# visit right subtree


if self.right:
ele+=self.right.in_order_traversal()
return ele

def build_tree(ele):
print("Buiding tree with: ", ele)
root=BinarySearchTree(ele[0])

for i in range(1,len(ele)):
root.add_child(ele[i])
return root

root=BinarySearchTree
elements=[17,4,1,20,9,23,18,34]
root=build_tree(elements)
print("Binary Search Tree (in order): ",root.in_order_traversal())
print(root.search(18))

Buiding tree with: [17, 4, 1, 20, 9, 23, 18, 34]


Binary Search Tree (in order): [1, 4, 9, 17, 18, 20, 23, 34]
True

Exercise

find_min()
find_max()
calculate_sum()
post_order_traversal()
pre_order_traversal()

You might also like