8.0 Tree Data Structure v2.2 - Notes
8.0 Tree Data Structure v2.2 - Notes
Node
Edge
Binary Tree
• Binary Tree is a special data structure used for data storage purposes.
• A binary tree has a special condition that each node can have a
maximum of two children.
• A binary tree has the benefits of both an ordered array and a linked
list as search is as quick as in a sorted array and insertion or deletion
operation are as fast as in linked list.
Tree Traversal
• Depth-first approach
identifies the root node and visits all the grandchildren of the left then
right sub-tree or the right sub-tree then the left sub-tree
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Tree Traversal
A→B→C→D→E→F→G
In-Order Traversal
• In this traversal method, the left subtree is visited
first, then the root and later the right sub-tree.
• We should always remember that every node may
represent a subtree itself.
• We start from A, and following in-order traversal, we
move to its left subtree B. B is also traversed in-
order.
• The process goes on until all the nodes are visited.
The output of inorder traversal of this tree will be −
D→B→E→A→F→C→G
In-Order Traversal
A→B→D→E→C→F→G
Pre-order Traversal
• D→E→B→F→G→C→A
Post-order Traversal
6 2 Breadth-f:
Pre-order:
In-order:
8 4 7 Post-order:
5 9
Exercises on Tree Traversal
j Determine the Breadth, In-order, Pre-order and Post-order
f e Pre-order:
In-order:
Post-order:
d a i Breadth:
g b
Types of Binary Tree
1. Full Binary Tree if every node has 0 or 2 children.
1 1
2 3 2 3
4 5 6 7 4 5
Types of Binary Tree
2. Complete Binary Tree if all the levels are completely filled except
possibly the last level and the last level has all keys as left as
possible.
1
2 3
4 5 6
Types of Binary Tree
3. Perfect Binary Tree if all the internal nodes have two children and
all leaf nodes are at the same level.
Types of Binary Tree
4. Balanced Binary Tree if the height of the left and right sub-tree of
any node differ by not more than 1.
diff = | height of left child – height of right child |
diff = 1 diff = 2
diff = 0 diff = 0
Types of Binary Tree
5. Degenerate Binary Tree if every internal node has one child.
Binary Tree Implementation
1. class Node:
2. def __init__(self,key):
3. self.left = None
4. self.right = None
5. self.val = key
Binary Tree Implementation
root
1. class Node: 1
2. def __init__(self,key):
3. self.left = None
4. self.right = None 2 3
5. self.val = key
6.
7. root = Node(1)
8. root.left = Node(2);
4 5
9. root.right = Node(3);
10. root.left.left = Node(4); Pre-order: 1 2 4 5 3
11. root.left.right = Node(5); In-order: 42513
Post-order: 4 5 2 3 1
Breadth: 12345
In-order Traversal
1. class Node:
2. def __init__(self, key):
3. self.left = None
4. self.right = None
5. self.val = key 12. root = Node(1)
6. 13. root.left = Node(2)
14. root.right = Node(3)
7. def printInorder(root):
15. root.left.left = Node(4)
8. if root: 16. root.left.right = Node(5)
9. printInorder(root.left) 17. print (“Inorder traversal of binary tree is")
10. print(str(root.val) + "->", end=‘') 18. printInorder(root)
11. printInorder(root.right)
Pre-order Traversal
1. class Node:
2. def __init__(self, key):
3. self.left = None
4. self.right = None
5. self.val = key 12. root = Node(1)
6. 13. root.left = Node(2)
14. root.right = Node(3)
7. def printPreorder(root):
15. root.left.left = Node(4)
8. if root: 16. root.left.right = Node(5)
9. print(str(root.val) + "->", end=‘') 17. print ("Preorder traversal of binary tree is")
10. printPreorder(root.left) 18. printPreorder(root)
11. printPreorder(root.right)
Post-order Traversal
1. class Node:
2. def __init__(self, key):
3. self.left = None
4. self.right = None
5. self.val = key 12. root = Node(1)
6. 13. root.left = Node(2)
14. root.right = Node(3)
7. def printPostorder(root):
15. root.left.left = Node(4)
8. if root: 16. root.left.right = Node(5)
9. printPostorder(root.left) 17. print ("Post-order traversal of binary tree is")
10. printPostorder(root.right) 18. printPostorder(root)
11. print(str(root.val) + "->", end=‘')
Binary Search Tree
• an advanced algorithm used for analyzing the
node, its left and right branches, which are
modeled in a tree structure and returning the
value.
• is devised on the architecture of a basic binary
search algorithm; hence it enables faster lookups,
insertions, and removals of nodes. This makes the
program really fast and accurate.
The properties that separate a binary search tree from a regular binary tree are
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
3. Both subtrees of each node are also BSTs i.e. they have the above two
properties
Binary Search Tree Operations
BST primarily offers the following three types of operations for your
usage:
• Search: searches the element from the binary tree
• Insert: adds an element to the binary tree
• Delete: delete the element from a binary tree
BST Operation: Search
The algorithm depends on the property of BST that if each left subtree
has values below root and each right subtree has values above the
root.
If root == NULL
return NULL;
If key == root.data
return root.data;
If key < root.data
return search(root.left)
If key > root.data
return search(root.right)
BST Operation: Insert
• Inserting a value in the correct position is similar to searching because
we try to maintain the rule that the left subtree is lesser than root
and the right subtree is larger than root.
• when we reach a point left or right subtree is null, we put the new
node there.
If node == NULL
return createNode(data)
if (newData < node.data)
node.left = insert(node.left, data);
else if (newData > node.data)
node.right = insert(node.right, data);
return node;
BST Operation: Delete
There are three cases for deleting a node from a binary search tree.
Case I
• In the first case, the node to be deleted is the leaf node.
Case II
• In the second case, the node to be deleted lies has a single child node.
Case III
• In the third case, the node to be deleted has two children.
BST Operation: Delete, Case 1
Case I
• In the first case, the node to be
deleted is the leaf node. In such a
case, simply delete the node from
the tree.
BST Operation: Delete, Case II
Case II
• In the second case, the node to
be deleted lies has a single child
node. In such a case follow the
steps below:
• Replace that node with its
child node.
• Remove the child node from
its original position.
BST Operation: Delete, Case III
Case III
• In the third case, the node to be deleted
has two children. In such a case follow
the steps below:
• Get the in-order successor of that
node.
• Replace the node with the inorder
successor.
• Remove the inorder successor from
its original position.
*means that the largest element on the left sub-tree