6 Trees an Intro
6 Trees an Intro
07/04/2017
Non-Linear Data Structure
Linear vs non-linear classification of data structures is dependent upon how
individual elements are connected to each other.
All linear data structures have one thing in common that they are sequential
Lists, Stack, Queue
In Non-Linear data structures, data elements are not sequential, an element can refer to
more than one elements
Tree, Graphs
07/04/2017
Graph
Graph is a non-linear mathematical structure that is defined as G= (v, e), where
v is a set of vertices{v1, v2, …vn} and e is a set of edges {e1,e2,e3,…em}
Where edge e is a pair of two vertices, means a connection between two vertices
07/04/2017
Tree
Tree is a connected graph which does not contain cycle. Every vertex has only
one parent.
Cycle means a path which starts and ends at same node c
b a c d a
b h
d e h e
07/04/2017
Tree
So, Tree is defined as data structure which presents hierarchical relationship
between data elements. Hierarchical means some elements are below and some
are above from others. Like family tree, folder structure, table of contents
C:/
Program
Windows Users
Files
07/04/2017
Tree: a Data Structure
Tree is a recursive data structure, it contains patterns that are themselves are
trees.
A data structure is recursive if it is composed of smaller pieces of it’s own data type.
Such as list and trees.
a is root of all nodes like b, d etc. a
b, c, d are also root of their sub trees and so on.
So, a tree T can be defined recursively as: b d
Tree T is a collection of nodes such that:
T is empty/NULL (No node) OR
e f h
There is a special node called root,
which can have 0 or more children (T1, T2, T3 …Tn)
which are also sub-trees themselves.
i j k l
T1, T2, T3 …Tn are disjoint sub trees ( no shared node)
07/04/2017
Tree Applications
Tree is an extremely useful data structure, it provides natural organization of
data which exhibits hierarchy, due to their non-linear structure they provide
efficient operations with compare to linear data structures. Few uses are as
follows:
Disk File System
Used by operating system to stored folder hierarchy
Search trees
More efficient than sorted list
07/04/2017
Tree Applications
Parse Trees
Used by compilers to produce machine code
Decision Trees
Used in artificial intelligence to build knowledge base
07/04/2017
Tree Applications
Games
are used in logic games
Data Compression
Huffman coding trees
Priority Queue
Heap Tree
07/04/2017
Tree Terminologies
Node/Vertex
Root a
One data unit of tree
Edge
Internal b c d
Arc/link from one node to other
Root node
The top node of tree. A node with no parent e f g h
Leaf/External node Edge
Node with no child
Leaf i j k l
Internal Node:
Node with child
Ancestors of Node
Parent, all grand parents and all great grand parents of node.
a, b and e are ancestors of i.
07/04/2017
Tree Terminologies
Descendants of Node
a
Child , all grand children and great grand children of node.
i, j, e and f are descendants of b.
b c d
Sub Tree
A node within tree with descendants
Degree of Node: e f g h
Number of its children
a’s degree is 3
i j k l
b, h and e’s degree is 2
c’s degree is 1
Degree of Tree
Maximum degree of any node Sub-tree
Since a has degree 3 that is maximum so degree of tree is 3
07/04/2017
Tree Terminologies
Depth/Level of Node Level
a 0
Number of ancestors or length of path from node to root
j has depth 3 Level
b c d 1
c has depth 1
Length of Path means # of edges on the path from one node to other
Level
e f g h
Siblings 2
07/04/2017
Binary Tree
Binary Tree is a special tree where each node can have maximum two children.
In other words maximum degree of any node is 2.
Each node has a left child and a right child. Even if a node has only one child, other
child is still mentioned with NULL.
a General Tree
Binary Tree a
b d b d
h
e f h g i g j
07/04/2017
Binary Tree
Recursive Definition:
T is a binary tree if
T is empty (NULL) OR
T’s root node has maximum two children's, where each child is itself a binary tree.
Left child is called left subtree and right child is called right subtree
General Tree
Binary Tree Binary Tree
07/04/2017
Full Binary Tree
Degree of each node is either 0 or 2.
Full Tree is also referred as Proper Tree
Perfect & Full Not-Perfect But Full Not Perfect Nor Full
07/04/2017
Complete Binary Tree
A tree that is completely filled at all levels, except the last level which is filled
from left to right
07/04/2017
Binary Tree
Maximum nodes at level i of binary tree?
2i
Maximum nodes in a binary tree?
2H+1-1
If n is total nodes in a binary tree
H ≈ log(n)
Number of leaves in a perfect tree?
2H
07/04/2017
Binary Tree ADT
In addition to previous function Binary Tree provides additional functions:
left(node): returns left child of node
right(node): returns right child of node
hasLeft(node): tells if a node has left child or not
hasRight(node): tells if a node has right child or not
sibling(node): returns sibling of given node
First find parent, then see it node itself is left or right child
07/04/2017
Binary Tree Implementation
Linked representation
Each node has two links left and right
If root node is null, means tree is empty
If node’s left, right links are NULL, it means its leaf node
Optionally, a parent field with a reference to the parent node left data right
class Node{
data; left data right left data right
Node left;
Node right;
left data right left data right left data right
}
07/04/2017
Binary Tree Implementation
Array representation
A fixed size tree can be represented using 1-D array.
If we know the height of tree, we can define size of array to hold maximum possible
number of nodes 2h+1-1 a 0
07/04/2017
Tree Traversal
A tree traversal means visiting each node of tree once.
Due to non-linear structure of tree there is not a single way to traverse node:
1. Breadth First Search
a
2. Depth First Search
Pre-order
In-order b g
Post-order
c f h
d e
07/04/2017
Breadth First Search (BFS)
Starting from root node, visit all of its children, all of its grand children and all
of its great grand children
Order of nodes: a b g c f h d e
a
Nodes at same level must be visited first before nodes of next level
Also known as level order traversal b g
Implementation?
We should store nodes to keep track of them. c f h
The sequence in which we store them effects the
the sequence in which we retrieve them back d e
Which data structure can be used to store nodes?
array, stack or queue
07/04/2017
Breadth First Search (BFS)
a a
b c b d
d e f e f h
g h i i j k l
abcdefghij abdefhijkl
07/04/2017
Breadth First Search (BFS)
Algorithm: Iterative_BFS(Tree root) a
Input: root node of Tree.
Steps:
b g
1. If root is not NULL
2. Set Q =new Queue () c f h
3. Set node = root
4. Q.enqueue(node)
5. While Q is not empty d e
6. node=Q.dequeue()
7. print(node)//print node’s data a h d e
8. If hasLeft(node)
9. Q.enqueue(node.left) b g d e
10. If hasRight(node) g c f e
11. Q.enqueue(node.right)
12. End While c f h
13. End If f h d e
07/04/2017
Recursive BFS
Recursive_BFS(node, queue)
Input: root node of Tree, an empty queue
Steps:
If node is not NULL
Print node
If hasLeft(node)
queue.enqueue(node.left)
If hasRight(node)
queue.enqueue(node.right)
If queue is not Empty
Recursive_BFS(queue.dequeue(), queue)
End if
07/04/2017
Depth First Search (DFS)
Using the top-down view of the tree, starting from root, go to each sub tree as
far as possible, then back track
Possible Orders:
a
Left sub tree and then right sub tree
abcdefgh
right sub tree and then left sub tree b g
aghbfced
Implementation: c f h
Can we use a stack instead of queue
d e
07/04/2017
Depth First Search (DFS)
a a
b c b d
d e f e f h
g h i i j k l
abdeghjcfi abeijfdhkl
07/04/2017
Depth First Search (DFS)
Algorithm: Iterative_DFS(root) a
Input: root node of Tree.
Steps:
b g
1. If root is not NULL
2. Set S=new Stack()
3. Set node = root c f h
4. S.push(node)
5. While( S is not empty) d e
6. node=S.pop()
7. Print node
8. If hasRight(node)
9. S.push(node.right) d
10. If hasLeft(node) c e e
11. S.push(node.left)
b f f f f
12. End While
13. End If
A g g g g g g h
07/04/2017
Depth First Variations
Depth First Search can also be implemented with recursive approach. And depending upon the
order in which we go in depth can bring different variations in order of node traversal. Which are:
Pre-order (simple DFS)
1. Visit node a
2. Visit left child of node
3. Visit right child of node b g
Post-order
1. Visit left child of node a
2. Visit right child of node
3. Visit node b g
In-order
1. Visit left child of node a
2. Visit node
3. Visit right child of node b g
07/04/2017
Pre-order vs. Post-order vs. In-order
Pre-order (node-left-right)
abcdefgh
Post-order (left-right-node) a
decfbhga
In-order (left-node-right) b g
dcebfagh
c f h
d e
07/04/2017
Pre-order vs. Post-order vs. In-order
a a
b d
b c
d e f e f h
g h i i j k l
j
Pre-order: a b d e g h j c f i Pre-order: a b e i j f d h k l
Post-order: d g j h e b i f c a Post-order: i j e f b k l h d a
In-order: dbgejhacif In-order: i e j b f a k h l d
07/04/2017
Tree Traversal-Recursive Algorithms
Recursive_Preorder(Tree node) Recursive_Postorder(Tree node)
If node is not NULL If node is not NULL
Print node Recursive_Postorder(node.left)
Recursive_Preorder(node.left) Recursive_Postorder(node.right)
Recursive_Preorder(node.right) Print node
End If End If
Recursive_Inorder(Tree node)
If node is not NULL
Recursive_Inorder(node.left)
Print node
This is traditional DFS
Recursive_Inorder(node.right)
End If
07/04/2017
Tree Building with Traversal Orders
Can we build a binary tree if we are given a traversal order?
Pre-order: 5, 15, 10, 8, 7, 9, 11, 19
5
Post-order: 10, 8, 15, 11, 19, 9, 7, 5
In-order: 10, 15, 8, 5, 7, 11, 9, 19 15 7
What is special about each traversal order?
How you will decide that which node is root node? 10 8 9
Which node is left?
Which node is right? 11 19
10 10
5 50