0% found this document useful (0 votes)
31 views

Lecture 09.1

The document provides information about trees as a data structure. It defines a tree as consisting of nodes where each node has a value and references to other child nodes. A tree must have a designated root node. It then discusses tree terminology like parent/child nodes, leaf nodes, root node, subtree, level, height, and binary trees where each node has at most two children. Binary tree traversal methods like inorder, preorder and postorder are also introduced, which traverse the left subtree before the right subtree.
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)
31 views

Lecture 09.1

The document provides information about trees as a data structure. It defines a tree as consisting of nodes where each node has a value and references to other child nodes. A tree must have a designated root node. It then discusses tree terminology like parent/child nodes, leaf nodes, root node, subtree, level, height, and binary trees where each node has at most two children. Binary tree traversal methods like inorder, preorder and postorder are also introduced, which traverse the left subtree before the right subtree.
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/ 26

Tree

Course Code: CSC 2106 Course Title: Data Structure (Theory)

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 9.1 Week No: 9 Semester: Fall 2020-2021


Lecturer: MAHFUJUR RAHMAN, mahfuj@aiub.edu
Lecture Outline

1. Tree
2. Basic Terminology
3. Binary Tree
• Complete Binary Tree
• Full Binary Tree
• Balanced Tree
• Tree Traversal
Tree
Introduction

1. Many applications are hierarchical in nature.


2. Linear data structures are not appropriate for these type of applications.

President

Vice President Vice President Vice President

Executive Executive Executive Executive Executive


Tree
Some Applications

Representing hierarchical data


Storing data in a way that makes it easily searchable
Representing sorted lists of data
Routing algorithms
Tree
Definition

As a data structure, a tree consists of one or more nodes, where


each node has a value and a list of references to other (its children)
nodes.

A tree must have a node designated as root. If a tree has only one
node, that node is the root node. Root is never referenced by any other
node.

Having more than one node indicates that the root have some (at
least one) references to its children nodes and the children nodes
(might) have references to their children nodes and so on.
Tree
An example

VALUE ROOT
A

CHILD CHILD
B C D

E F G H I J

K L M N O
Tree
Terminologies

As a data structure, a tree consists of one or more nodes, where


each node has a value and a list of references to other (its children)
nodes.

A tree must have a node designated as root. If a tree has only one
node, that node is the root node. Root is never referenced by any other
node.

Having more than one node indicates that the root have some (at
least one) references to its children nodes and the children nodes
(might) have references to their children nodes and so on.
Tree
Definition

Generally it is considered that in a A


tree, there is only one path going from
one node to another node.
B C D
There cannot be any cycle or loop.
The link from a node to other node
is called an edge.
An arrowed edge indicates flow E F G H I J
from P to Q. P Q
An straight line edge indicates flow
from P to Q and Q to P. P Q K L M N O

If
AIfnode
link from
F isline
straight reached
node L to
through
node
is generally Cnode
usedisto
considered,
B, than thethan
represent linklinks
the there
from
node
will be
betweenK to
a the
cycle
node among
F willofnot
nodes anodes
be considered.
tree. C, G, and L.
Tree
Terminologies

Nodes LEVEL
0 A
Parent Nodes & Child Nodes
Leaf Nodes: nodes with no child
Root Node: node with no parent
Sub Tree: the tree rooted by a 1 B C D
child
Level of a tree:
 Root at level 0;
 Each children have 2 E H I J
Height of root A the
is 4;level F G
one more
Height than its
of nodes parent.
B, C, D is 3;
Height/depth
Height of E,of
F, the
G, H,tree:
I, J isTotal
2;
number
Height of
of Levels
nodes K, L , M, N, O is 1.
Height of a node: Total number
of levels from bottom
[Tree height – node level]. 3 K L M N O
Height of this tree is 4, as there are four levels (0…3).
Tree
m-ary tree

A Tree is an m-ary Tree when each of its node has no more than m children.
A
A 3-ary tree
B D

C D
E F H I B

H I J
K G N O E F G

L M 2-ary tree K L M N O
Binary Tree
Definition and example

A Each node of a binary


Tree has at most 2
B D children.

E F H I

K G N O

L M Binary tree
Complete and Full Binary Tree
• A complete/full binary tree is a binary
tree, which is completely filled with A
nodes from top to bottom and left to
right.
• The tree starts from the root (top),
B D
goes to the next level with first the
left child and then the right child (left
to right). The process repeats for each
next level with each node till the last E F H I
(bottom) level.
• In complete binary tree some of the
nodes only for the bottom level might
be absent (here nodes after N). K L G M N C J P
• In Full binary tree the last/bottom
level must also be filled up.
Full Binary Tree
#Nodes at each level L = 2L
Level=0, #Nodes in a full binary tree with L labels is
A
# of nodes=20=1
If total number of nodes is n,
Level=1, Height of the tree is Log2n
B D
# of nodes=21=2

Level=2,
E F H I
# of nodes=22=4

Level=3,
K L G M N C J P
# of nodes=23=8
Complete Binary Tree
Exercise

A full binary tree is always a complete binary tree but vice versa may not be true.
Why?

A complete binary tree has L labels. How many nodes it may have in both
minimum and maximum cases?

A complete binary tree has L labels. At most, how many more nodes are required
to make it a full binary tree?
Tree Traversal
Introduction

Systematic way of visiting all the nodes.


Methods:
Inorder
Preorder
Postorder
They all traverse the left subtree before the right subtree.
Tree Traversal
Inorder

B A D
A

B H D
E F

M E F C H
K L

K L M C
Tree Traversal
Inorder

E B F A D

K L E F M H

K L M C
Tree Traversal
Inorder

K E L B F M A H D

K L M CH

C
Tree Traversal
Inorder

K E L B F M A H C D

C
Tree Traversal
Inorder

Traversing order A
left subtree
parent
B D
right subtree

E F H

K L M C

K E L B F M A H C D
Tree Traversal
Preorder

Traversing order A
parent
left subtree
B D
right subtree

E F H

K L M C

A B E K L F M D H C
Tree Traversal
Postorder

Traversing order A
left subtree
right subtree
parent B D

E F H

K L M C

K L E M F B C H D A
Tree Traversal
Exercise

Visit the tree


Inorder
Preorder
Postorder
Tree Traversal
Exercise

Visit the tree


Inorder
Preorder
Postorder
References

1. https://en.wikipedia.org/wiki/Binary_Search_Tree
Books
 “Schaum's Outline of Data Structures with C++”. By John R. Hubbard
 “Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
 “Data structures, algorithms and performance”, D. Wood, Addison-Wesley, 1993
 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008
 “Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
 “C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,
Bruno R. Preiss,

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy