You are on page 1of 90

Binary Tree

Froilan De Guzman
Learning outcomes:
- To discuss the basic concepts of binary tree data
structure.
- To discuss the how to insert data on binary tree
- To simulate how perform tree traversal.

2
What is binary tree?
A tree whose elements have at most 2 children is called a binary tree. Since
each element in a binary tree can have only 2 children, we typically name
them the left and right child.

A Binary Tree node contains following parts: Data, Pointer to left child,
Pointer to right child
3
Data insertion on binary tree

Example #1 : you will


insert the following data
items (in order) on
binary tree.
• 7
• 9
• 3
• 6
• 1
• 11
• 8 4
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree.
• 7
• 9
• 3
• 6
• 1
• 11
• 8 5
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree. 9
• 7
• 9
• 3
• 6
• 1
• 11
• 8 6
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree. 3 9
• 7
• 9
• 3
• 6
• 1
• 11
• 8 7
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree. 3 9
• 7
• 9
• 3 6

• 6
• 1
• 11
• 8 8
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree. 3 9
• 7
• 9
• 3 1 6

• 6
• 1
• 11
• 8 9
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree. 3 9
• 7
• 9
• 3 1 6 11

• 6
• 1
• 11
• 8 10
Data insertion on binary tree

Example #1 : you will 7


insert the following data
items (in order) on
binary tree. 3 9
• 7
• 9
• 3 1 6 8 11

• 6
• 1
• 11
• 8 11
Data insertion on binary tree

Example #2 : you will


insert the following data
items (in order) on
binary tree.
• 8
• 11
• 3
• 7
• 1
• 9
• 6 12
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on
binary tree.
• 8
• 11
• 3
• 7
• 1
• 9
• 6 13
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on 11
binary tree.
• 8
• 11
• 3
• 7
• 1
• 9
• 6 14
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on 3 11
binary tree.
• 8
• 11
• 3
• 7
• 1
• 9
• 6 15
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on 3 11
binary tree.
• 8
• 11 7

• 3
• 7
• 1
• 9
• 6 16
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on 3 11
binary tree.
• 8
• 11 1 7

• 3
• 7
• 1
• 9
• 6 17
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on 3 11
binary tree.
• 8
• 11 1 7 9

• 3
• 7
• 1
• 9
• 6 18
Data insertion on binary tree
8

Example #2 : you will


insert the following data
items (in order) on 3 11
binary tree.
• 8
• 11 1 7 9

• 3
• 7
• 1 6

• 9
• 6 19
Data insertion on binary tree

Example #3 : you will


insert the following data
items (in order) on
binary tree.
• 3
• 11
• 8
• 7
• 1
• 9
• 6 20
Data insertion on binary tree 3

Example #3 : you will


insert the following data
items (in order) on
binary tree.
• 3
• 11
• 8
• 7
• 1
• 9
• 6 21
Data insertion on binary tree 3

Example #3 : you will


insert the following data 11
items (in order) on
binary tree.
• 3
• 11
• 8
• 7
• 1
• 9
• 6 22
Data insertion on binary tree 3

Example #3 : you will


insert the following data 11
items (in order) on
binary tree.
• 3
8
• 11
• 8
• 7
• 1
• 9
• 6 23
Data insertion on binary tree 3

Example #3 : you will


insert the following data 11
items (in order) on
binary tree.
• 3
8
• 11
• 8
• 7 7
• 1
• 9
• 6 24
Data insertion on binary tree 3

Example #3 : you will


insert the following data 1 11
items (in order) on
binary tree.
• 3
8
• 11
• 8
• 7 7
• 1
• 9
• 6 25
Data insertion on binary tree 3

Example #3 : you will


insert the following data 1 11
items (in order) on
binary tree.
• 3
8
• 11
• 8
• 7 7 9
• 1
• 9
• 6 26
Data insertion on binary tree 3

Example #3 : you will


insert the following data 1 11
items (in order) on
binary tree.
• 3
8
• 11
• 8
• 7 7 9
• 1
• 9
6
• 6 27
Types of binary tree
Full Binary Tree is a Binary Tree in which every node has 0 or 2 children.

28
Types of binary tree
Complete Binary Tree has all levels completely filled with nodes except
the last level and in the last level, all the nodes are as left side as possible.

29
Types of binary tree

Perfect Binary Tree is a Binary Tree in which all internal nodes have 2
children and all the leaf nodes are at the same depth or same level.

30
Types of binary tree

Balanced Binary Tree is a Binary tree in which height of the left and the
right sub-trees of every node may differ by at most 1.

31
Types of binary tree

Degenerate Binary Tree is a Binary Tree where every parent node has only
one child node.

32
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their values
too. Because, all nodes are connected via edges (links) we always start from
the root (head) node. That is, we cannot randomly access a node in a tree.
There are three ways which we use to traverse a tree −
- In-order Traversal
- Pre-order Traversal
- Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree
or to print all the values it contains.

33
Tree Traversal
Traversal is a process to visit all the nodes of a tree and may print their values
too. Because, all nodes are connected via edges (links) we always start from
the root (head) node. That is, we cannot randomly access a node in a tree.
There are three ways which we use to traverse a tree −
- In-order Traversal
- Pre-order Traversal
- Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree
or to print all the values it contains.

34
In-order Traversal
Left>>Root>>Right
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.

If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.
• 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 in-order traversal of this tree will
be −
35
• D→B→E→A→F→C→G
Pre-order Traversal
Root>>Left>>Right
In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.

• We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The output of
pre-order traversal of this tree will be −
• A→B→D→E→C→F→G 36
Post-order Traversal
Left>>Right>>Root
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then
the right subtree and finally the root node.

• We start from A, and following Post-order traversal, we first visit the left subtree B. B is also traversed
post-order. The process goes on until all the nodes are visited. The output of post-order traversal of this
tree will be −
• D→E→B→F→G→C→A
37
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

38
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

Left>>Root>>Right 39
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→3

Left>>Root>>Right 40
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→3→6

Left>>Root>>Right 41
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→3→6→7

Left>>Root>>Right 42
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→3→6→7→8

Left>>Root>>Right 43
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→3→6→7→8→
9
6

Left>>Root>>Right 44
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→3→6→7→8→9→
11
6

Left>>Root>>Right 45
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8
6

Root>>Left>>Right
46
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8→3
6

Root>>Left>>Right
47
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8→3→1
6

Root>>Left>>Right
48
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8→3→1→7
6

Root>>Left>>Right
49
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8→3→1→7→6
6

Root>>Left>>Right
50
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8 → 3 → 1 → 7 → 6 → 11
6

Root>>Left>>Right
51
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

8 → 3 → 1 → 7 → 6 → 11→ 9
6

Root>>Left>>Right
52
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1
6

Left>>Right>>Root
53
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→6
6

Left>>Right>>Root
54
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→6→7
6

Left>>Right>>Root
55
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→6→7→3
6

Left>>Right>>Root
56
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1→6→7→3→9
6

Left>>Right>>Root
57
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1 → 6 → 7 → 3 → 9 → 11
6

Left>>Right>>Root
58
Example # 1
8
Visit all nodes using
- In-order traversal
- Pre-order traversal 3 11
- Post-order traversal

1 7 9

1 → 6 → 7 → 3 → 9 → 11→
8 6

Left>>Right>>Root
59
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

7 9

6
60
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1
7 9

Left>>Root>>Right 6
61
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→3
7 9

Left>>Root>>Right 6
62
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→3→6
7 9

Left>>Root>>Right 6
63
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→3→6→7
7 9

Left>>Root>>Right 6
64
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→3→6→7→8
7 9

Left>>Root>>Right 6
65
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→3→6→7→8→9
7 9

Left>>Root>>Right 6
66
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1 → 3 → 6 → 7 → 8 → 9 → 11
7 9

Left>>Root>>Right 6
67
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3
7 9

Root>>Left>>Right 6
68
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3→1
7 9

Root>>Left>>Right 6
69
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3 → 1 → 11
7 9

Root>>Left>>Right 6
70
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3 → 1 → 11 → 8
7 9

Root>>Left>>Right 6
71
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3 → 1 → 11 → 8 → 7
7 9

Root>>Left>>Right 6
72
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3 → 1 → 11 → 8 → 7 → 6
7 9

Root>>Left>>Right 6
73
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

3 → 1 → 11 → 8 → 7 → 6 → 9
7 9

Root>>Left>>Right 6
74
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1
7 9

Left>>Right>>Root 6
75
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→6
7 9

Left>>Right>>Root 6
76
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→6→7
7 9

Left>>Right>>Root 6
77
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→6→7→9
7 9

Left>>Right>>Root 6
78
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1→6→7→9→8
7 9

Left>>Right>>Root 6
79
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1 → 6 → 7 → 9 → 8 → 11
7 9

Left>>Right>>Root 6
80
Example # 2 3

Visit all nodes using


- In-order traversal
1 11
- Pre-order traversal
- Post-order traversal
8

1 → 6 → 7 → 9 → 8 → 11 → 3
7 9

Left>>Right>>Root 6
81
Coding the binary tree: node class
class node:
def __init__(self, value):
self.value = value
self.leftChild = None
self.rightChild = None

82
Coding the binary tree: node class - insert
class node:
def __init__(self, value):
self.value = value
self.leftChild = None
self.rightChild = None
def insert (self, data):
if self.value ==data:
return False
elif self.value >data:
if self.leftChild:
return self.leftChild.insert(data)
else:
self.leftChild = node(data)
return True
else:
if self.rightChild:
return self.rightChild.insert(data)
else:
self.rightChild = node(data)
83
return True
Coding the binary tree: node class - find
def find (self, data):
if (self.value ==data):
return True
elif (self.value >data):
if self.leftChild:
return self.leftChild.find(data)
else:
return False
else:
if self.rightChild:
return self.rightChild.find(data)
else:
return False

84
Coding the binary tree: node class – tree traversal
def preorder(self):
if self:
print(str(self.value))
if self.leftChild:
self.leftChild.preorder()
if self.rightChild:
self.rightChild.preorder()
def postorder(self):
if self:
if self.leftChild:
self.leftChild.postorder()
if self.rightChild:
self.rightChild.postorder()
print(str(self.value))
def inorder(self):
if self:
if self.leftChild:
self.leftChild.inorder()
print(str(self.value))
if self.rightChild: 85
self.rightChild.inorder()
Coding the binary tree: tree class – insert and find
class binaryTree:
def __init__(self):
self.root = None

def insert(self, data):


if self.root:
return self.root.insert (data)
else:
self.root = node(data)
return True

def find (self, data):


if self.root:
return self.root.find(data)
else:
return False

86
Coding the binary tree: tree class – tree traversal

def preorder (self):


print("Preorder: ")
self.root.preorder()

def postorder (self):


print("Post order: ")
self.root.postorder()

def inorder (self):


print("Inorder: ")
self.root.inorder()

87
Testing the code:
bst =binaryTree()

bst.insert(8)
bst.insert(11)
bst.insert(3)
bst.insert(7)
bst.insert(1)
bst.insert(9)
bst.insert(6)

bst.inorder()
bst.preorder()
bst.postorder()
88
Testing the code:
bst =binaryTree()

bst.insert(3)
bst.insert(11)
bst.insert(8)
bst.insert(7)
bst.insert(1)
bst.insert(9)
bst.insert(6)

bst.inorder()
bst.preorder()
bst.postorder()
89
References:
- https://www.geeksforgeeks.org/binary-tree-data-structure/
- https://www.tutorialspoint.com/data_structures_algorithms/tree_data_structure.htm
- https://towardsdatascience.com/5-types-of-binary-tree-with-cool-illustrations-
9b335c430254
- https://www.tutorialspoint.com/data_structures_algorithms/tree_traversal.htm

90

You might also like