You are on page 1of 11

Data Structures with Python-20CS41P Week 11

Week-11
The Tree data structure – Example: File explorer/Folder structure, Domain name server. Tree Terminologies,
Tree node representation. Binary trees, Binary search trees, Properties, Implementation of tree operations –
insertion, deletion, search, Tree traversals (in order, pre order and post order).

The Tree Data structure:


 A tree is a connected acyclic graph.
 A tree is non linear data structure in which all the values are arranged in a non linear form.
 A tree is a data structure which is a collection of zero or more nodes connected by edges.
 A Tree Data Structure consists of nodes and edges that organize group of data in a hierarchical fashion.
 The data elements are stored in nodes and pairs of nodes are connected by edges.
 A classic example of a tree structure is:
 File/Folder Explorer: The representation of files, directories and subdirectories in a file system
 Organization web pages in website
 Content/Index page of text book
File/Folder Explorer:

Content/Index page of text book:

Dept. of Computer Science & Engg. Page 1 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

Tree Terminologies:

Root node: The topmost (first) node in a tree is called as root node.

Ex: - 10 is the root node


Leaf node: A node which does not have any child node is called as leaf node.

Ex: - 20 and 30 are leaf nodes.


Child node: The node obtained from the parent node is called as child node.

Ex: - 20 and 30 are children of 10


Parent node: The node having further sub branches is called as the parent node.

Dept. of Computer Science & Engg. Page 2 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

Ex: - 10 is the parent of 20 and 30


Level of tree: The distance of a node from the root node is called as level of a node.

Ex: - In above tree level is 2.


Internal node: The non leaf nodes in a tree are called as internal node.

Ex: - In above tree 10, 20 are the internal nodes


External node: The leaf nodes in a tree are called as external node.
Ex: - In above tree 40, 50 and 30 are the external nodes
Sibling: Two or more nodes having the same parent are called siblings.
Ex: - 40 and 50 are siblings
Ex: - 20 and 30 are siblings
Degree of a node: The number of branches associated with each node is called degree of a node.
Ex: - degree of 20 is 2
Ex: - degree of 40 is 0
Degree of a tree: The maximum degree in the tree is the degree of a tree.
Ex: - Degree of an above tree is 2.
Depth: The height/depth of a tree is defined as the maximum level of any leaf node in the tree is called as depth
of a tree. Ex: - In above tree depth is 3
Path: The sequence of nodes and edges from one node to another node is called as path.
Ex: - The path from 10 to 40 is 10->20->40
Ancestors: - The nodes in the path above the child are called ancestors.
Descendents: - The nodes in the path below parent are called descendents.
Subtree: Every node can be the root of its own subtree, which consists of a subset of nodes and edges of the
larger tree.

Dept. of Computer Science & Engg. Page 3 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

Ex:

Applications of trees:
 Searching and Sorting (Binary Search Tree)
 Manipulation of arithmetic expressions.
 Symbol table construction
 Trees are used in syntax analysis of compiler design.
 Heap trees
 Router algorithms
 Huffman coding
 To solve database problems such as indexing

Binary tree: Binary tree is a tree in which every node can have maximum of two children i.e. each node can
have zero children, one child or two children, but not more than two children.
Strictly binary tree:
 A binary tree in which every node can have either 2 children or 0 children is called strictly binary tree.
 It is also called as full binary tree or proper binary tree.
 It is used to represent mathematical expressions.

Complete binary tree: A complete binary tree is a tree in which every level except the last level is completely
filled and in last level all nodes are left aligned.

Dept. of Computer Science & Engg. Page 4 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

Ex:

Perfect binary tree: A binary tree in which every internal node must have exactly two children and all leaf
nodes at the same level is called perfect binary tree.
Ex:

Tree node representation:


 A linked representation of each node in a binary tree has three fields left, data, right
 The node of a binary tree is represented as follows:

 The left contains the address of left sub tree.


 The right contains the address of right sub tree.
 If any sub tree is empty corresponding pointer stores a None value.
 Consider the following binary tree:

 The linked representation of above binary tree is given below:


Dept. of Computer Science & Engg. Page 5 Govt. Polytechnic Koppal
Data Structures with Python-20CS41P Week 11

 The python code to create a node is given below.


class Node:
def __init__(self,value):
self.left = None
self.data = value
self.right =None
Binary search tree:-
 A binary search tree (BST) is a binary tree in which each node contains a value and it should have the
following properties:
 Values of Nodes of left subtree must be smaller
 Values of Nodes of right subtree must be greater than or equal.
 A binary search tree is a binary tree in which for each node say x in the tree, elements in the left subtree are
less than x and elements in the right subtree are greater than or equal to x.
 Consider the binary search tree in Figure, which contains integer values. The root node contains value 60
and all values in the root's left subtree are less than 60 and all of the values in the right subtree are greater
than 60.

Insert Operation:
 When we create Binary Search Tree, nodes are inserted one at a time by creating new node. Suppose we
want to build a binary search tree from the value list [60, 25, 100, 35, 17, 80] by inserting the keys in the
order they are listed.
Dept. of Computer Science & Engg. Page 6 Govt. Polytechnic Koppal
Data Structures with Python-20CS41P Week 11

 We start by inserting value 60. A node is created and its data field set to that value. Since the tree is initially
empty, this first node becomes the root of the tree (part a).
 Next, we insert value 25. Since it is smaller than 60, it has to be inserted to the left of the root, which means
it becomes the left child of the root (part b).
 Value 100 is then inserted in a node linked as the right child of the root since it is larger than 60 (part c).
 Similarly, we insert 35, 17 and 80 (part d, e f).

Tree traversals (in order, pre order and post order):


 Tree Traversal is defined as “Visiting each and every node of a tree in some predefined order”.
 Traversing a binary tree can be defined as a procedure with which each node in a given tree is visited
exactly once in some predetermined order.
 There are 3 ways of a traversing a binary tree
1. Preorder (NLR)
2. Postorder (LRN)
3. Inorder (LNR)
Preorder (NLR):
 In this node is visited before the subtrees
Algorithm: - It can be recursively defined as follows
 If the tree is not empty
Step 1: visit the root node
Step 2: Traverse the left sub tree in preorder
Step 3: Traverse the right sub tree in preorder.
 Consider the following example:

Dept. of Computer Science & Engg. Page 7 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

 The preorder traversal of the above tree is 10, 20, 30


 The python code of preorder traversal is given below:
def preorder(self, rt):
print(rt.data, end=" ")
if rt.left is not None:
self.preorder(rt.left)
if rt.right is not None:
self.preorder(rt.right)
Postorder (LRN):
 In this node is visited after the subtrees
Algorithm: - It can be recursively defined as follows
 If the tree is not empty
Step 1: Traverse the left sub tree in postorder
Step 2: Traverse the right sub tree in postorder.
Step 3: visit the root node.
 Consider the following example:

 The postorder traversal of the above tree is 20, 30, 10


 The python code of postorder traversal is given below:
def postorder(self, rt):
if rt.left is not None:
self.postorder(rt.left)
if rt.right is not None:
self.postorder(rt.right)
print(rt.data, end=" ")
Inorder (LRN):
 In this node is visited between the subtrees
Algorithm: - It can be recursively defined as follows
 If the tree is not empty
Step 1: Traverse the left sub tree in inorder
Step 2: visit the root node.
Step 3: Traverse the right sub tree in inorder.
 Consider the following example:

 The inorder traversal of the above tree is 20, 10, 30


Dept. of Computer Science & Engg. Page 8 Govt. Polytechnic Koppal
Data Structures with Python-20CS41P Week 11

 The python code of inorder traversal is given below:


def inorder(self, rt):
if rt.left is not None:
self.inorder(rt.left)
print(rt.data, end=" ")
if rt.right is not None:
self.inorder(rt.right)
Implement Binary Search Tree:
class Node:
def __init__(self,value):
self.data = value
self.left = None
self.right =None
class BinarySearchTree:
def __init__(self):
self.root=None
def insert(self,value):
newNode=Node(value)
if self.root is None:
self.root = newNode
else:
curNode = self.root
while curNode is not None:
if value < curNode.data:
if curNode.left is None:
curNode.left=newNode
break
else:
curNode = curNode.left
else:
if curNode.right is None:
curNode.right=newNode
break
else:
curNode=curNode.right
def preorder(self, rt):
print(rt.data, end=" ")
if rt.left is not None:
self.preorder(rt.left)
if rt.right is not None:
self.preorder(rt.right)
def postorder(self, rt):
if rt.left is not None:

Dept. of Computer Science & Engg. Page 9 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

self.postorder(rt.left)
if rt.right is not None:
self.postorder(rt.right)
print(rt.data, end=" ")
def inorder(self, rt):
if rt.left is not None:
self.inorder(rt.left)
print(rt.data, end=" ")
if rt.right is not None:
self.inorder(rt.right)
bst = BinarySearchTree()
ls = [25,10,35,20,65,45,24]
for i in ls:
bst.insert(i)
print("\nPre-order")
bst.preorder(bst.root)
print("\nPost-order")
bst.postorder(bst.root)
print("\nIn-order")
bst.inorder(bst.root)

Assignment Questions:
1. Construct the binary search tree for the following values and traverse the tree in preorder, postorder,
inorder? 46, 76, 36, 26, 16, 56, 96.
2. Construct the binary search tree for the following values and traverse the tree in preorder, postorder,
inorder? 30 63 2 89 16 24 19 52 27 9 4 45
3. Construct the binary search tree for the following values and traverse the tree in preorder, postorder,
inorder? 25,10,35,20,65,45,24
4. Sort the following list using Binary Search Tree:
a) 10, 8, 11, 4, 9, 15, 7, 14
b) 10, 8, 9, 15, 7, 14
5. Consider the following binary tree:

Dept. of Computer Science & Engg. Page 10 Govt. Polytechnic Koppal


Data Structures with Python-20CS41P Week 11

(a) Show the order the nodes will be visited in a: i. preorder traversal ii. inorder traversal iii. postorder
traversal
(b) Identify all of the leaf nodes.
(c) Identify all of the interior nodes.
(d) List all of the nodes on level 4.
(e) List all of the nodes in the path to each of the following nodes: i) 83 ii) 39 iii) 4 iv) 9
(f) Consider node 52 and list the node's: i) Descendants ii) Ancestors iii) Siblings
(g) Identify the depth of each of the following nodes: i) 78 ii) 41 iii) 60 iv) 19

Dept. of Computer Science & Engg. Page 11 Govt. Polytechnic Koppal

You might also like