0% found this document useful (0 votes)
9 views29 pages

Trees PART 1

Slides to study

Uploaded by

jimishag04
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)
9 views29 pages

Trees PART 1

Slides to study

Uploaded by

jimishag04
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/ 29

Non-Linear Data Structures

TOPIC 1: TREES

Mahesh Maddumala, Ph.D.


Department of Computing and Information Science
Mercyhurst University

Non-Linear Data Structures Trees


Python Data Structures

Data
Structures

Non-
Primitive
Primitive

Diction
Integer Float String Boolean Arrays Lists Tuples Sets Files
aries

Non-
Linear
Linear

Stack Queue Trees Graphs

Non-Linear Data Structures Trees


Trees
 A Tree is a non-linear data structure that stores elements hierarchically
 Example1:

Non-Linear Data Structures Trees


Trees
 Example2:

Non-Linear Data Structures Trees


Basic Terminology
 A tree consists of nodes and the connections between the nodes are called as
edges
 The top node is called a root node
 A node is called as internal node if it has one or more children
 A node is called as external node or leaf if it has no children
 Other than root, every node has a unique parent node
 If two nodes have the same parent, they are siblings

Non-Linear Data Structures Trees


Basic Terminology
A

B C D

E F G

 Root node: A
 Internal nodes: A, B & C
 External nodes (or Leaves): D, E, F & G
 Siblings: ?

Non-Linear Data Structures Trees


Basic Terminology

Level 1

Level 2
height=4
Level 3

Level 4

Non-Linear Data Structures Trees


Binary Trees
 A binary tree is a tree data structure in which each node has at most two
children, which are referred to as the left child and the right child
Examples:

A A

B C B C

D E F G
D E F G
H I

Non-Linear Data Structures Trees


Representation of Binary Trees
 Reference based representation left element right
 Each node can be represented with a structure of a data
element, left-child reference, and right child reference
 Example:

d d

b f b f

a c
a c

Non-Linear Data Structures Trees


Types of Binary Tree
 Full Binary Tree
 Complete Binary Tree
 Perfect Binary Tree
 Balanced Binary Tree

Non-Linear Data Structures Trees


Full Binary Tree
 A full binary tree is a binary tree in which every node has either 0 or 2 children
o Also known as proper binary tree or 2-tree
Example1:
A

B C

D E F G

H I J K

Non-Linear Data Structures Trees


Full Binary Tree
Exercise1: Is this a full binary tree?

B C

D E F G

H I

Non-Linear Data Structures Trees


Full Binary Tree
Exercise2: Is this a full binary tree?

B C

D E F

Non-Linear Data Structures Trees


Full Binary Tree
Exercise3: Is this a full binary tree?

B C D

E F G H I J

Non-Linear Data Structures Trees


Full Binary Tree
Exercise4: Is this a full binary tree?

B C

D E

F G

Non-Linear Data Structures Trees


Properties of Full Binary Tree
 Number of leaves = Number of internal nodes + 1
 Total number of nodes = 2 x (Total number of leaves) – 1
 Total number of nodes = 2 x (Total number of internal nodes) + 1

Non-Linear Data Structures Trees


Complete Binary Tree
 A Complete Binary Tree is a binary tree in which at every level, except
possibly the last, must be completely filled and in the last level, all the
nodes must be as left side as possible.
Examples:
A A
A
B C B C
B C
D E F G D E F G
D E F G
H H I
Figure A Figure B Figure C

Non-Linear Data Structures Trees


Complete Binary Tree
The below binary trees are not complete binary trees:

A A
A
B C B C
B C

D E D E F G D E
F G I J

Figure F
Figure D Figure E

Non-Linear Data Structures Trees


Perfect Binary Tree
 A Perfect Binary Tree is a binary tree in which all internal nodes have 2
children and all the leaf nodes are at the same level.

Examples:
A

A
B C

B C
D E F G

Figure A Figure B

Non-Linear Data Structures Trees


Perfect Binary Tree
The below binary trees are not perfect binary trees

A A

B C B C

D E F D E

Figure C Figure D

Non-Linear Data Structures Trees


Properties of Perfect Binary Tree
 Total number of nodes = 2h – 1, where h is the height of a tree
 Total number of leaves = 2(h-1)
 Total number of internal nodes = 2(h-1) – 1
 Total number of leaves = Total number of internal nodes + 1

Non-Linear Data Structures Trees


Balanced Binary Tree
 A 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.

Examples:
A
A
A
B C C
B
B C
D E F D E F G
D
G
Figure A Figure B Figure C

Non-Linear Data Structures Trees


Balanced Binary Tree
The below binary trees are not balanced binary trees

A A

B C B C

D E D E F

F G G

Figure D Figure E

Non-Linear Data Structures Trees


Tree Traversal Algorithms
 A traversal of a tree is a systematic way of accessing (or visiting) all the
nodes of a tree.

Tree Traversals

Breadth-First Search
Depth-First Search (DFS)
(BFS) or level-order

Pre-order In-order Post-order

Non-Linear Data Structures Trees


Tree Traversal Algorithms
 Pre-order
 Algorithm:

preorder(node):
if (node == null)
return
visit(node) #print node
preorder(node.left)
preorder(node.right)

Prefix expression: + * A - B C + D E

Non-Linear Data Structures Trees


Tree Traversal Algorithms
 In-order
 Algorithm:

inorder(node):
if (node == null)
return
inorder(node.left)
visit(node) #print node
inorder(node.right)

Infix expression: A * B - C + D + E

Non-Linear Data Structures Trees


Tree Traversal Algorithms
 Post-order
 Algorithm:

postorder(node):
if (node == null)
return
postorder(node.left)
postorder(node.right)
visit(node) #print node

Postfix expression: A B C - * D E + +

Non-Linear Data Structures Trees


Tree Traversal Algorithms
 Level-order (or Breadth-First Search)
 visit every node on each level starting from root before going to a lower level:

B C Expression: A B C D E F G H I

D E F G

H I

Non-Linear Data Structures Trees


References
 Goodrich, Michael T., Roberto Tamassia, and Michael H. Goldwasser. Data structures and algorithms in
Python. Hoboken: Wiley, 2013.

 https://en.wikipedia.org/wiki/Tree_traversal

Non-Linear Data Structures Trees

You might also like