binary tree lect 1 and 2nd
binary tree lect 1 and 2nd
3
Tree Terminology
■ Edge
■ The connecting link between any two nodes is called an Edge.
In a tree with 'N' number of nodes there will be a maximum of
'N-1' number of edges.
4
Tree Terminology
■ Parent
■ The node which is predecessor of any node is called
as Parent Node. The node which has branch from it to any
other node is called as parent node. Parent node can also be
defined as "The node which has child / children".
Here,
A is Parent of B and C
B is Parent of D, E and F
C is the Parent of G and H
5
Tree Terminology
■ Child
■ The node which is descendant of any node is called as CHILD
Node. In a tree, any parent node can have any number of
child nodes. In a tree, all the nodes except root are child
nodes.
Here,
B and C are Children of A
G and H are Children of C
K is a Child of G
6
Tree Terminology
■ Siblings
■ In a tree data structure, nodes which belong to same Parent
are called as Siblings. In simple words, the nodes with same
parent are called as Sibling nodes.
Here,
B and C are siblings
D, E and F are siblings
G and H are siblings
7
Tree Terminology
■ Leaf
■ The node which does not have a child is called as Leaf Node.
■ leaf node is also called as 'Terminal' node.
Here,
D, I, J, F. K and H are leaf
nodes
8
Tree Terminology
■ Internal Nodes
■ In a tree data structure, the node which has at least one child
is called as Internal Node.
Here,
A, B, E, C, G are Internal
Nodes
9
Tree Terminology
■ Degree
■ the total number of children of a node is called as Degree of
that Node. The highest degree of a node among all the nodes
in a tree is called as 'Degree of Tree'
Here,
Degree of A is 2
Degree of B is 3
Degree of F is 0
10
Tree Terminology
■ Level
■ In a tree data structure, the root node is said to be at Level 0
and the children of root node are at Level 1 and the children
of the nodes which are at Level 1 will be at Level 2 and so on.
In simple words, in a tree each step from top to bottom is
called as a Level and the Level count starts with '0' and
incremented by one at each level (Step).
Here,
Height of the tree is 3
12
Tree Terminology
■ Depth
■ The total number of edges from root node to a particular node
is called as Depth of that Node. In a tree, the total number of
edges from root node to a leaf node in the longest path is said
to be Depth of the tree.
Here,
Depth of the tree is 3
13
Tree Terminology
■ Path
■ The sequence of Nodes and Edges from one node to another
node is called a Path between that two Nodes. Length of a
Path is total number of nodes in that path. In below
example the path A - B - E - J has length 4.
Here,
Path between A and J:
A-B-E-J
Path between C and K:
C-G-K
14
Tree Terminology
■ Sub-tree
■ Each child from a node forms a subtree recursively. Every
child node will form a subtree on its parent node.
15
Binary Tree
■ Binary tree is a special type of tree data structure in which
every node can have a maximum of 2 children.
– One is known as left child and
– the other is known as right child.
■ In a binary tree, every node can have either 0 children or 1
child or 2 children but not more than 2 children.
16
Types of Binary Tree
■ A full binary tree is a binary tree in which each node has
exactly zero or two children.
■ A complete binary tree is a binary tree, which is completely
filled, with the possible exception of the bottom level, which is
filled from left to right.
17
Binary Tree (tree.h)
18
Binary Tree Traversal
■ A traversal is a process that visits all the nodes in the tree.
Since a tree is a nonlinear data structure, there is no unique
traversal. We will consider several traversal algorithms with
we group in the following two kinds
– depth-first traversal
– breadth-first traversal
■ There are three different types of depth-first traversals:
– Pre Order traversal
– In Order traversal
– Post Order traversal
19
Binary Tree Traversal
■ Pre Order Traversal:
– Visit the root.
– Perform a preorder traversal of the left subtree.
– Perform a preorder traversal of the right subtree.
20
Binary Tree Traversal
■ Preorder traversal
21
Binary Tree Traversal
■ In Order Traversal:
– Perform an in order traversal of the left subtree.
– Visit the root.
– Perform an in order traversal of the right subtree.
In Order - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
22
Binary Tree Traversal
■ Inorder traversal
23
Binary Tree Traversal
■ Post Order Traversal:
– Perform a postorder traversal of the left subtree.
– Perform a postorder traversal of the right subtree.
– Visit the root.
24
Binary Tree Traversal
■ Postorder traversal
25
Binary Tree Traversal
■ There is only one kind of breadth-first traversal--the level order
traversal. This traversal visits nodes by levels from top to
bottom and from left to right.
26
Binary Tree Traversal
■ Level order traversal
28
Binary Tree Traversal
29
Binary Tree Traversal: Example
Pre Order
In Order
Post Order
30
Binary Tree Traversal: Example
Pre Order
ABDFGCEHI
In Order
BFDGAEIHC
Post Order
FGDBIHECA
31
Creating Binary Trees
32
Creating Binary Trees
■ We will use these routines as primitives to create a binary tree
from data values in an array.
– There is a very nice mapping from the indices of a linear
array into nodes of a binary tree. We do this by taking the
value a[i] and letting it have as child a[2*i+1] and a[2*i+2].
– Then we map a[0] into the unique root node of the resulting
binary tree. Its left child will be a [1], and its right child will be
a [2].
– The function create_tree() embodies this mapping.
– The formal parameter size is the number of nodes in the
binary tree.
33
Creating Binary Trees