Ds With Python Week11-1
Ds With Python Week11-1
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).
Tree Terminologies:
Root node: The topmost (first) node in a tree is called as root node.
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.
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:
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).
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:
(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