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

0% found this document useful (0 votes)
42 views43 pages

7.0 Tree

Data structure and algorithms

Uploaded by

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

7.0 Tree

Data structure and algorithms

Uploaded by

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

Data Structure and

Algorithm (IT-209)
Instructor: Abdullah Javed
([email protected])
Lecturer,
Govt. Postgraduate College, Jhelum

Lecture 7.0
Fall 2020
Agenda
• Tree Introduction
• Definition
• Representation
• Applications
• Terminologies
• Tree Traversal
• Binary Tree
• Implementation
 Array Based
 Linked List Based

• Expression Tree
2
Tree
• Nature view of tree
leave
s

branche
s roo
t
3
Tree
• Computer scientist’s view of tree

roo
t
leave
s

branche
s nodes
4
Tree (Definition)
• Tree represents the nodes connected by edges
• Each node can have 0 or more children
• A node can have at most one parent

5
Formal Definition
• A tree is a collection of nodes with the following properties:
 The collection can be empty.
 Otherwise, a tree consists of a distinguished node r, called root, and
zero or more nonempty sub-trees T1, T2, … , Tk, each of whose roots are
connected by a directed edge from r.
• The root of each sub-tree is said to be child of r, and r is the
parent of each sub-tree root.
• If a tree is a collection of N nodes, then it has N-1 edges.

root

...
T1 T2 Tk 6
Tree (Representation)

7
Tree Applications
• Table of contents of a book
• Organization hierarchy
• Unix file system
• Representing sorted lists of data
• Routing algorithms

Shortly, we will see that for applications that require


searching, linear data structures are not suitable.
8
9
Unix File System

10
Hierarchical Structure of a
University

11
Important Terminologies
• Path – Path refers to the sequence of nodes along the edges of a tree
 A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is parent of ni+1
(1 ≤i < k)
 The length of a path is the number of edges on that path.
 There is a path of length zero from every node to itself.
 There is exactly one path from the root to each node.

• Root – The node at the top of the tree is called root.


• Parent – Any node except the root node has one edge upward to a node is called parent
• Child – The node below a given node connected by its edge downward is called child node
• Leaf – The node which doesn’t have any child node is called the leaf node. Also called
External (all other nodes are internal)
• Siblings – A group of nodes with the same parent
• Descendant – A node reachable by repeated proceedings from parent to child

12
Important Terms
• Ancestor – A node reachable by repeated proceedings from child to parent
 If there is a path from n1 to n2, then n1 is ancestor of n2, and n2 is descendent of n1.
 If n1 ≠ n2 then n1 is proper ancestor of n2, and n2 is proper descendent of n1

• Degree – The number of sub trees of a node


• Edge – The connection between one node and another
• Traversing – Passing through nodes in a specific order
• Depth – Number of ancestors
 The depth of node ni is the length of the path from root to node n i

• Height – Height of a node is the length of the longest path from that node to any leaf
 The height of node ni is the length of longest path from node ni to a leaf.

• Binary Tree – A binary tree is a tree such that


 Every node has at most two children
 Each node is either a left child or a right child
13
Terminologies
A

B C D E F G

H I J K L M N

P Q
 Node A has 6 children: B, C, D, E, F, G.
 B, C, H, I, P, Q, K, L, M, N are leaves in the tree above.
 K, L, M are siblings since F is parent of all of them.

14
Terminologies
A tree, with height and depth information

15
Not a Tree
• Structures that are not trees.

B C

D E F

G H I

16
Not a Tree
• Structures that are not trees.

B C

D E F

G H I

17
Not a Tree
• Structures that are not trees.

B C

D E F

G H I

18
Tree Traversal
• Traversal
is a process to visit all the nodes of a
tree and print their values. We can’t access a
random node in a tree instead we start from root
node. We use the following three ways to traverse
tree nodes:

 Pre-Order Traversal

 post-Order Traversal

 In-Order Traversal
19
In-Order Traversal
• In
this method, the left subtree is visited first, then
the root and later the right subtree

• The output of the in-order traversal of this tree will


be
20
In-Order Traversal

21
Pre-Order Traversal
• Inthis method, the root node is visited first, then the
left subtree and finally the right subtree

• The output of the pre-order traversal of this tree will


be
22
Pre-Order Traversal

23
Post-Order Traversal
• In
this method, the left subtree is traversed first,
then the right subtree, and finally the root node

• The output of the pre-order traversal of this tree will


be
24
Post-Order Traversal

25
Exercise
• Write the in-order, pre-order, and post-order traversal of the following
tree:

26
Binary Tree
• A binary tree is a tree in which no node can have more than two
children
• A binary tree is a finite set of elements that is either empty or is
partitioned into three disjoint subsets.
• The first subset contains a single element called the root of the tree.
• The other two subsets are themselves binary trees called the left and
right subtrees.
• Each element of a binary tree is called a node of the tree.

27
Binary Tree
• Binary tree with 9 nodes.

B C

D E F

G H I

28
Binary Tree root

B C

D E F

G H I

Left subtree Right subtree

29
Binary Tree
• Recursive definition
A
root

B C

D E F

Left subtree G H I

Right subtree

30
Binary Tree
• Recursive definition
A

B C
root

D E F

G H I

Left subtree

31
Binary Tree
• Recursive definition
A

B C

D E F

root
G H I

32
Binary Tree
• Recursive definition
A
root

B C

D E F

G H I

Right subtree

33
Binary Tree
• Recursive definition
A

B C
root

D E F

G H I

Left subtree Right subtree

34
A Pointer-Based Implementation
of Binary Trees
struct BinaryNode {
Object element;
struct BinaryNode *left;
struct BinaryNode *right;
};

35
Linked Structure for Binary Trees
• A node is represented by an object storing

 Element

 Parent node

 Left child node

 Right child node

36
Linked Structure for Binary Trees

37
An Array-Based Representation
• Nodes are stored in an array A
• Node v is stored at A [ rank ( v ) ]
 Rank ( root ) = 1
 Left in even: if node is the left child of parent ( node ),
Rank ( node ) = 2 * rank ( parent ( node ) )
 Right in odd: if node is the right child of parent ( node ),
Rank ( node ) = 2 * rank ( parent ( node ) ) + 1

• A [ 0 ] is always empty
• A [ i ] is empty if there is no node in the i th position
• The array size N is 2 (h+1)

38
An Array-Based Representation

39
Tree ADT
• Data: any type of objects/data can be stored in a
tree
• Operations / Methods:
 root() – return the root of the tree
 children(p) – returns the children of a node
 size() – returns the number of nodes in the tree
 isEmpty() - returns true if the tree is empty
 isRoot(p), isInternal(p), isExternal(p)
 Insert(data)
 Delete()
 Traverse()
 Search(data)
40
Expression Tree
• Expression tree is a special kind of binary tree used to represent expressions
• Each internal node correspond to operator and each external node corresponds to
operand. Example: (2x(a–1)+(3xb))

41
Example

42
Easy to Generate Infix, Prefix Postfix Expressions

• Infix: ((8–5)*((4+2)/3)
• Prefix: *-85/+423
• Postfix: 85–42+3/*
43

You might also like