Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
90 views35 pages

CSC-114 Data Structure and Algorithms: Slides Credit: Ms. Saba Anwar

The document discusses trees as a non-linear data structure. It defines a tree as a connected graph without cycles and describes trees used in computer science as rooted trees that are directed graphs. The document also defines trees recursively and outlines some common applications of trees, such as for representing file systems, performing searches more efficiently than lists, parsing in compilers, and decision making in artificial intelligence. Key tree terminology introduced includes nodes, edges, the root node, leaf nodes, internal nodes, and ancestors of a node.

Uploaded by

Umair Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views35 pages

CSC-114 Data Structure and Algorithms: Slides Credit: Ms. Saba Anwar

The document discusses trees as a non-linear data structure. It defines a tree as a connected graph without cycles and describes trees used in computer science as rooted trees that are directed graphs. The document also defines trees recursively and outlines some common applications of trees, such as for representing file systems, performing searches more efficiently than lists, parsing in compilers, and decision making in artificial intelligence. Key tree terminology introduced includes nodes, edges, the root node, leaf nodes, internal nodes, and ancestors of a node.

Uploaded by

Umair Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Edited with the trial version of

Foxit Advanced PDF Editor


To remove this notice, visit:
www.foxitsoftware.com/shopping

Tree

CSC-114 Data Structure and Algorithms

Slides credit: Ms. Saba Anwar


Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Outline www.foxitsoftware.com/shopping

 Non-Linear Data Structures


 Tree
 Tree Terminologies
 Memory Representation
 Tree as ADT
 Binary Tree
 Traversal Strategies
 BFS
 DFS
 Pre order
 Post order
 In order

2
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Non-Linear Data Structure www.foxitsoftware.com/shopping

 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, Tables

3
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Graph www.foxitsoftware.com/shopping

 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

4
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree www.foxitsoftware.com/shopping

 Tree is a connected graph which does not contain cycle


 Cycle means a path which starts and ends at same node c
b a c d a
b h

d e h e

 In field of computer science, a specific form of trees is more common, which is called
rooted trees. a
 a is root vertex.
 These rooted trees are directed graphs b c d

e f g h

5
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree www.foxitsoftware.com/shopping

 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

Java Microsoft Google Guest Public

6
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree: a Data Structure www.foxitsoftware.com/shopping

 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.
 b, c, d are also root of their sub trees and so on. a

 So, a tree T can be defined recursively as:


 Tree T is a collection of nodes such that: b d
 T is empty/NULL (No node) OR
 There is a special node called root,
e f h
 which can have 0 or more children (T1, T2, T3 …Tn)
 which are also sub-trees themselves.
 T1, T2, T3 …Tn are disjoint sub trees ( no shared node)
i j k l

7
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Applications www.foxitsoftware.com/shopping

 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

8
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Applications www.foxitsoftware.com/shopping

 Parse Trees
 Used by compilers to produce machine code

 Decision Trees
 Used in artificial intelligence to build knowledge base

9
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Applications www.foxitsoftware.com/shopping

 Games
 are used in logic games

 Data Compression
 Huffman coding trees
 Priority Queue
 Heap Tree

 And many more other applications.

10
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Terminologies www.foxitsoftware.com/shopping

 Node/Vertex
Root a
 One data unit of tree
 Edge
 Arc/link from one node to other Internal b c d
 Root node
 The top node of tree. A node with no parent e f g h
 Leaf/External node
Edge
 Node with no child
 Internal Node: Leaf i j k l
 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.

11
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Terminologies www.foxitsoftware.com/shopping

 Descendants of Node
a
 Child , all grand children and great grand children of node.
 i, j, e and f are descendants of b.
 Sub Tree b c d
 A node within tree with descendants
 Degree of Node: e f g h
 Number of its children
 a’s degree is 3
 b, h and e’s degree is 2 i j k l
 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

12
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Terminologies www.foxitsoftware.com/shopping

 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

 Nodes with same parent and at same level Level


3
 i and j i j k l
 b and c and d
 Height of Tree
1. Maximum depth of any node3
2. Longest path from root to any leaf node  3
13
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree as ADT www.foxitsoftware.com/shopping

 A tree T provides following basic operations:


 Tree Methods:
 size(root): returns total number of nodes
 isEmpty(root): if tree is empty or not
 root(): returns root node of tree
 Node Methods:
 parent(node): returns parent of node
 children(node): returns list of all child’s of node
 isInternal(node): if node is non-leaf
 isExternal(node):if node is leaf
 isRoot(node): if node is root

14
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree www.foxitsoftware.com/shopping

 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

15
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree www.foxitsoftware.com/shopping

 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

16
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Full Binary Tree www.foxitsoftware.com/shopping

 Degree of each node is either 0 or 2.


 Full Tree is also referred as Proper Tree

Full Full Not-Full

 A tree that is not Full/Proper , is called improper or not-full


17
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Perfect www.foxitsoftware.com/shopping

 A Full/Proper binary tree in which each leaf node has same depth/level.

Perfect & Full Not-Perfect But Full Not Perfect Nor Full

18
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Complete Binary Tree www.foxitsoftware.com/shopping

 A tree that is completely filled at all levels, except the last level which is filled
from left to right

Complete But Not Full Not-Complete Not-Complete But Full

19
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree www.foxitsoftware.com/shopping

 Maximum nodes at level i of binary tree?


 2i
 Maximum nodes in a binary tree?
 2h+1-1

20
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree ADT www.foxitsoftware.com/shopping

 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

21
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree Implementation www.foxitsoftware.com/shopping

 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

left data right left data right


class Node{
data;
Node left; left data right left data right
Node right;
}
left data right left data right left data right

22
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Binary Tree Implementation www.foxitsoftware.com/shopping

 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

 Root of tree  array[0] 1 b g 2


 Left child of root array[1]
 Right child of root array[2] 3 c f 4 5 h 6
 -----
 -----
 Left child of node at index k array[2k+1]
 Right child of node at index k array[2k+2] 0 1 2 3 4 5 6
a b g c f NULL h

23
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Traversal www.foxitsoftware.com/shopping

 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

24
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Breadth First Search (BFS) www.foxitsoftware.com/shopping

 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

25
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Breadth First Search (BFS) www.foxitsoftware.com/shopping

a a

b c b d

d e f e f h

g h i i j k l

abcdefghij abdefhijkl

26
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Breadth First Search (BFS) www.foxitsoftware.com/shopping

 Algorithm: Iterative_BFS(Tree root) a


 Input: root node of Tree.
 Steps:
b g
1. If root is not NULL
2. Let Q =new Queue ()
3. Set node = root c f h
4. Q.enqueue(node)
5. While( Q is not empty) d e
6. node=Q.dequeue()
7. print(node)//print node’s data
8. If hasLeft(node) a h d e
9. Q.enqueue(node.left) b g d e
10. If hasRight(node)
11. Q.enqueue(node.right) g c f e
12. End While
c f h
13. End If
f h d e
27
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Recursive BFS www.foxitsoftware.com/shopping

 Recursive_BFS(Tree node, Queue Q)


If(node is not NULL)
print(node)
If hasLeft(node)
Q.enqueue(node.left)
If hasRight(node)
Q.enqueue(node.right)
If Q is not Empty
Recursive_BFS(Q.dequeue(),Q)
End if

28
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Search (DFS) www.foxitsoftware.com/shopping

 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

29
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Search (DFS) www.foxitsoftware.com/shopping

a a

b c b d

d e f e f h

g h i i j k l

abdeghjcfi abeijfdhkl

30
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Search (DFS) www.foxitsoftware.com/shopping

 Algorithm: Iterative_DFS(Tree root) a


 Input: root node of Tree.
 Steps:
1. If root is not NULL b g
2. S=new Stack()
3. set node = root
4. S.push(node) c f h
5. While( S is not empty)
6. node=S.pop() d e
7. print(node)
8. If hasRight(node)
9. S.push(node.right)
10. If hasLeft(node)
11. S.push(node.left) d
12. End While c e e
13. End If
b f f f f
A g g g g g g h
31
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Depth First Variations www.foxitsoftware.com/shopping

 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)
a
1. Visit node
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

32
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Pre-Order vs. Post-Order vs. In-Order www.foxitsoftware.com/shopping

 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

33
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Pre-Order vs. Post-Order vs. In-Order www.foxitsoftware.com/shopping

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
34
Edited with the trial version of
Foxit Advanced PDF Editor
To remove this notice, visit:
Tree Traversal-Recursive Algorithms www.foxitsoftware.com/shopping

 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

35

You might also like