0% found this document useful (0 votes)
1 views23 pages

Trees

The document provides an overview of tree data structures, explaining their hierarchical nature and the advantages over linear data structures. It details various tree terminologies, types of binary trees, and the properties of binary search trees, along with operations such as insertion, searching, and deletion. Additionally, it covers tree traversal methods including preorder, inorder, and postorder.

Uploaded by

oludeadeola67
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)
1 views23 pages

Trees

The document provides an overview of tree data structures, explaining their hierarchical nature and the advantages over linear data structures. It details various tree terminologies, types of binary trees, and the properties of binary search trees, along with operations such as insertion, searching, and deletion. Additionally, it covers tree traversal methods including preorder, inorder, and postorder.

Uploaded by

oludeadeola67
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/ 23

Trees

MICHEAL OGUNDERO
What is a Tree?
A tree is a nonlinear hierarchical data structure that consists of nodes connected by edges.
Each Tree consists of a root node from which we can access each element of the tree.
Why Tree Data Structures?
Other data structures such as arrays, linked list, stack, and queue are linear data structures
that store data sequentially. In order to perform any operation in a linear data structure, the
time complexity increases with the increase in the data size. But, it is not acceptable in
today's computational world.

Different tree data structures allow quicker and easier access to the data as it is a non-linear
data structure.
Tree Terminologies
Node:

A node is an entity that contains a key or value and pointers to its child nodes.

The last nodes of each path are called leaf nodes or external nodes that do not contain a
link/pointer to child nodes.

The node having at least a child node is called an internal node.


Tree Terminologies
❖ Edge - It is the link between any two nodes.
❖ Root - It is the topmost node of a tree.
❖ Height - The height of a node is the number of edges from the node to the deepest leaf
(ie. the longest path from the node to a leaf node).
❖ Depth - is the number of edges from the root to the node.

N.B: The height of a Tree is the height of the root node or the depth of the deepest node.

❖ Degree of a node is the total number of branches of that node.


Binary Tree
A binary tree is a tree data structure in which each parent node can have at most two
children. Each node of a binary tree consists of three items:
❖ data item
❖ address of left child
❖ address of right child
Types of Binary Tree
The following are some of the different types of binary trees:

❖ Full Binary Tree


❖ Perfect Binary Tree
❖ Complete Binary Tree
Full Binary Tree
A full Binary tree is a special type of binary tree in which every parent node/internal node
has either two or no children.
Perfect Binary Tree
A perfect binary tree is a type of binary tree in which every internal node has exactly two
child nodes and all the leaf nodes are at the same level.
Complete Binary Tree

A complete binary tree is just like a full binary


tree, but with two major differences:

❖ Every level must be completely filled


❖ All the leaf elements must lean towards
the left.
❖ The last leaf element might not have a
right sibling i.e. a complete binary tree
doesn't have to be a full binary tree.
Binary Tree Representation

A node of a binary tree is represented by a


structure containing a data part and two
pointers to other structures of the same type.

class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
Binary Search Tree (BST)
Binary search tree is a data structure that quickly allows us to maintain a sorted list of
numbers.
❖ It is called a binary tree because each tree node has a maximum of two children.
❖ It is called a search tree because it can be used to search for the presence of a number in
O(log(n)) time.
Properties of a Binary Search Tree
The properties that separate a binary search tree from a regular binary tree are:

❖ All nodes of left subtree are less than the root node.
❖ All nodes of right subtree are more than the root node.

N.B: Both subtrees of each node are also BSTs i.e. they have the above two properties.
Regular Binary Tree Vs BST

class Node: class BST:

def __init__(self, value): def __init__(self):

self.value = value self.root = None

self.left = None

self.right = None
Insert Operation
def insert(self, value): if newNode.value<temp.value:

newNode = Node(value) if temp.left is None:

if self.root is None: temp.left = newNode

self.root = newNode return self

return self temp = temp.left

temp = self.root else:

while True: if temp.right is None:

if newNode.value==temp.value: temp.right = newNode

return None return self

temp = temp.right
Search Operation

def contains(value): elif value > temp.value:


if self.root is None: temp = temp.right
return False
else:
temp = self.root
return True
while temp:
return False
if value < temp.value:
temp = temp.left
Minimum Value

def minValueNode(currentNode):

while currentNode.left:

currentNode = currentNode.left

return currentNode
Delete a Node

def deleteNode(root, value): # If the node is with only one


child or no child
# Return if the tree is empty
if root.left is None:
if root is None:
temp = root.right
return root
root.right = None
# Find the node to be deleted
return temp
if value < root.value:
elif root.right is None:
root.left = deleteNode(root.left,
value) temp = root.left

elif(value > root.value): root.left = None

root.right=deleteNode(root.right, return temp


value)

else:
Delete a Node
# If the node has two children,

# place the inorder successor in position of the node to be deleted

temp = minValueNode(root.right)

root.key = temp.key

# Delete the inorder successor

root.right = deleteNode(root.right, temp.key)

return root
Tree Traversal
Traversing a tree means visiting every node in the tree. You might, for instance, want to add
all the values in the tree or find the largest one. For all these operations, you will need to visit
each node of the tree.

Linear data structures like arrays, stacks, queues, and linked list have only one way to read
the data. But a hierarchical data structure like a tree can be traversed in different ways.
Traverse Preorder

def traversePreOrder(self): [1, 2, 4, 5, 3, 6, 7]


print(self.val, end=' ')
if self.left:
self.left.traversePreOrder()
if self.right:
self.right.traversePreOrder()
Traverse Inorder

def traverseInOrder(self): [ ]
if self.left:
self.left.traverseInOrder()
print(self.val, end=' ')
if self.right:
self.right.traverseInOrder()
Traverse Postorder

def traversePostOrder(self):
if self.left:
self.left.traversePostOrder()
if self.right:
self.right.traversePostOrder()
print(self.val, end=' ')

You might also like