SlideShare a Scribd company logo
BMI2123 Data Structures
Asst
akaratas@bandirma.edu.tr
Week 8:Trees
So Far
Arrays
Now, familiar with algorithmic complexity analysis
Order of running time
Big-Oh function
Stacks
Queues
A midterm exam
2
Today ..
3
Linear vs Nonlinear Data Structures
Linear Data Structure: Data is organized in sequential (linear) order.
Data elements are traversed one after the other.
Only one element can be directly reached while traversing.
Examples: Array, stack, linked lists, queue
Nonlinear Data Structure: Data is not organized in sequential order.
Data can be organized hierarchically or random order.
Examples: Tree, graph, dictionaries, heaps
What is a Tree?
A tree is an abstract data type that stores
elements hierarchically.
A tree is a graph without cycles
A nonlinear structure
Recall: A data structure is said to be nonlinear if
its elements form a hierarchical classification
where, data items appear at various levels
In software systems, a tree is an abstract model
of a hierarchical structure
such as
lists, vectors, and sequences
A tree consists of nodes with a parent-child
relation
6
Recursive Nature of Trees
A tree is hierarchical collection of nodes. One of the nodes, known
as the root, has no parent. The links leaving a node (any number of
links are allowed) point to child nodes. Trees are recursive
structures. Each child node is itself the root of a subtree. At the
bottom of the tree are leaf nodes, which have no children.
root
Recursive definition: A tree is either:
empty (null), or
a root node that contains:
data,
a left subtree, and
a right subtree.
(The left and/or right subtree could be empty.)
Trees in Computer Science
folders/files on a computer
family genealogy; organizational charts
AI: decision trees
compilers: parse tree
a = (b + c) * d;
cell phone T9
d
+
*
a
=
c
b
Image source:
https://images.datacamp.com/image/upload/v1677504957/decision_tree_for_heart_attack_prevention_2140
bd762d.png
Trees in Computer Science(cont.)
A parse tree
Node: An object containing a data value and left/right children.
Root: the node with no parents. There can be at most one root node in a tree
Leaf: a node that has no children
Branch: any internal node; neither the root nor a leaf
parent: a node that refers to this one
child: a node that this node refers to
sibling: the nodes which are children of same parent
A node p is an ancestor of node q if there exists a path from root to q and p
appears on the path. The node q is called a descendant of p.
Edge: the link from parent to child
Subtree: the smaller tree of nodes on the left or right of the current node
Tree Terminology
Node: An object containing a data value and left/right children.
Root: the node with no parents. There can be at most one root node in a
tree A
Leaf: a node that has no children E,J,K,H,I
Branch: any internal node; neither the root nor a leaf B,C,D,F,G
parent: a node that refers to this one B,C,D,F,G
child: a node that this node refers to B,C,D,E,F,G,H,I,J,K
sibling: the nodes which are children of same parent {B,C,D}, {E,F}, {H,I}
Edge: the link from parent to child All links in the figure
Tree Terminology
Answers
Tree Terminology(cont.)
level or depth: length of the path from a
root to a given node. The root node is at
level zero. For example, (1 and 4) at the
same level and (2 and 5) at the same level.
The height of a tree is the number of links
in the longest path from the root to a leaf. Empty tree
Right-skewed tree
One node
Binary Trees
A tree is called binary tree if each node has zero child, one child or two children.
Empty tree is also a valid binary tree.
There are four major types of binary trees:
Full Binary Tree - Every parent node has either two or no children.
Perfect Binary Tree - Every parent node has exactly two child nodes and all the
external nodes (leaf nodes) are at the same level.
Complete Binary Tree - In this case, every level must be filled, and all the leaf
elements lean towards the left.
Balanced Binary Tree - A binary tree in which the height difference between the
left and the right child nodes is either 0 or 1.
Tree ADT
It is useful to store collections of positions. For example, the children of a node in
a tree can be presented to the user as such a list. We define position list, to be a
list whose elements are tree positions.
The real power of a tree position arises from its ability to access the neighboring
elements of the tree. Given a position p of tree T, we define the following:
14
Properties of Binary Trees
For the following properties, let us assume that the height of the tree is h. Also,
assume that root node is at height zero.
where n is the number of nodes in the tree,
h is the height of the tree
Representation of A Binary Tree
A binary tree data structure is represented using two methods. Those methods are
1-Array Representation and 2-Linked List Representation
Array Representation
In array representation of binary tree, we use a one dimensional array (1-D Array)
to represent a binary tree.
Array Representation(cont.)
Waste of space problem!
2-Linked List Representation
We use linked list to represent a binary tree. In a linked list, every
node consists of three fields. First field, for storing left child
address, second for storing actual data and third for storing right
child address.
Tree Traversal
traversal: An examination of the elements of a tree.
A pattern used in many tree algorithms and methods
Common orderings for traversals:
pre-order: process root node, then its left/right subtrees
in-order: process left subtree, then root node, then right
post-order: process left/right subtrees, then root node
level-order: Access nodes level by level starting from root. (also known as Breadth First Search)
40
81
9
41
17
6
29
Root
pre-order: ?
in-order: ?
post-order: ?
level-order: ?
Traversal trick
To quickly generate a traversal:
Trace a path around the tree.
As you pass a node on the
proper side, process it.
pre-order: left side
in-order: bottom
post-order: right side
pre-order: 17 41 29 6 9 81 40
in-order: 29 41 6 17 81 9 40
post-order: 29 6 41 81 40 9 17
40
81
9
41
17
6
29
overallRoot
Examples
3
86
9
15
42
27
48 12 39
5
Solution for the examples
Arithmetic Expression Tree
Binary tree associated with an arithmetic expression
internal nodes: operators
external nodes: operands
Example: arithmetic expression tree for the expression
(2 (a 1) (3 b))
23
2
a 1
3 b
Note: Properties of Proper Binary Trees
Notation
n number of nodes
e number of external nodes
i number of internal nodes
h height
24
Properties:
e = i + 1
n = 2e - 1
h i
h (n - 1)/2
e 2h
h log2 e
h log2 (n + 1) - 1
Acknowledgement
This material is prepared based on Data Structures
and Algorithms in C++ 2e book and the lecture
notes of Prof. Yung Yi from KAIST, South Korea.
We thank to Michael T. Goodrich, Roberto Tamassia,
David M. Mount who are the authors of the book
and Prof. Yung Yi.

More Related Content

PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
Tree Data Structure Tree Data Structure Details
PPTX
Data Structure of computer science and technology
PPTX
Tree.pptx
PDF
Dsc++ unit 3 notes
PPTX
Trees in data structures
PPTX
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
PPTX
D9-Tree and Graph-Data-Structures information.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Tree Data Structure Tree Data Structure Details
Data Structure of computer science and technology
Tree.pptx
Dsc++ unit 3 notes
Trees in data structures
TREE PRESENTATION COMPUTER SCIENCE/DATA STRUCTURE
D9-Tree and Graph-Data-Structures information.pptx

Similar to 8.haftajava notlarıiçeriyordökumanlar.pdf (20)

PPTX
Unit 5 Tree.pptx
PPT
Tree and Binary Search tree
PPTX
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
PPTX
Data Structures -Non Linear DS-Basics ofTrees
PPTX
PDF
tree traversals.pdf
PPTX
Basic Tree Data Structure BST Traversals .pptx
DOCX
Trees and Graphs in data structures and Algorithms
PPTX
Saikat techhnology of techtechhnology of techGhorai.pptx
DOCX
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
PPTX
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
PPT
Binary tree
PPT
Binary tree traversal ppt - 02.03.2020
PPTX
TREES basics simple introduction .pptx
PPTX
TREES 1.pptx
PPT
Lecture 5 trees
PPT
Lecture 5 tree.pptx
PPTX
trees in data structure
PPT
data structures and algorithms dsa engg.ppt
PPTX
Tree all information about tree concept are available .
Unit 5 Tree.pptx
Tree and Binary Search tree
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures -Non Linear DS-Basics ofTrees
tree traversals.pdf
Basic Tree Data Structure BST Traversals .pptx
Trees and Graphs in data structures and Algorithms
Saikat techhnology of techtechhnology of techGhorai.pptx
data structures Unit 3 notes.docxdata structures Unit 3 notes.docx
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
Binary tree
Binary tree traversal ppt - 02.03.2020
TREES basics simple introduction .pptx
TREES 1.pptx
Lecture 5 trees
Lecture 5 tree.pptx
trees in data structure
data structures and algorithms dsa engg.ppt
Tree all information about tree concept are available .
Ad

Recently uploaded (20)

PPTX
VERB TO BE_SERPA YORDY.pptxvhyjjkjjjjjjuuj
PPTX
sample 1mathssscpreprationfor basics.PPTX
PPT
Welcome-to-Information-Technology.pptx.ppt
PPTX
title _yeOPC_Poisoning_Presentation.pptx
PDF
Lifting Equipment Inspection Checklist with eAuditor Audits & Inspections
PPTX
IOT piching HEALTH MONITORING SYSTEM USING ARDUINO123.pptx
PDF
Chapter -24-By Dr Sajid Ali Ansari 2021.pdf
PDF
DOC-20250802-WA0013._20250802_161719_0000.pdf
PDF
Colorful Illustrative Digital Education For Children Presentation.pdf
PPTX
dhcp concept.pptxfeegrvewfegrgerhtrhtrhredew
PPTX
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
PPTX
Countable and uncountable nouns_SERPA YORDY.pptx
PDF
Core Components of IoT, The elements need for IOT
PPT
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
PDF
Cableado de Controladores Logicos Programables
PPTX
kvjhvhjvhjhjhjghjghjgjhgjhgjhgjhgjhgjhgjhgjh
PPTX
executive branch_no record.pptxsvvsgsggs
PPTX
了解新西兰毕业证(Wintec毕业证书)怀卡托理工学院毕业证存档可查的
PPTX
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
PPTX
code of ethics.pptxdvhwbssssSAssscasascc
VERB TO BE_SERPA YORDY.pptxvhyjjkjjjjjjuuj
sample 1mathssscpreprationfor basics.PPTX
Welcome-to-Information-Technology.pptx.ppt
title _yeOPC_Poisoning_Presentation.pptx
Lifting Equipment Inspection Checklist with eAuditor Audits & Inspections
IOT piching HEALTH MONITORING SYSTEM USING ARDUINO123.pptx
Chapter -24-By Dr Sajid Ali Ansari 2021.pdf
DOC-20250802-WA0013._20250802_161719_0000.pdf
Colorful Illustrative Digital Education For Children Presentation.pdf
dhcp concept.pptxfeegrvewfegrgerhtrhtrhredew
PROGRAMMING-QUARTER-2-PYTHON.pptxnsnsndn
Countable and uncountable nouns_SERPA YORDY.pptx
Core Components of IoT, The elements need for IOT
FABRICATION OF MOS FET BJT DEVICES IN NANOMETER
Cableado de Controladores Logicos Programables
kvjhvhjvhjhjhjghjghjgjhgjhgjhgjhgjhgjhgjhgjh
executive branch_no record.pptxsvvsgsggs
了解新西兰毕业证(Wintec毕业证书)怀卡托理工学院毕业证存档可查的
Presentacion compuuuuuuuuuuuuuuuuuuuuuuu
code of ethics.pptxdvhwbssssSAssscasascc
Ad

8.haftajava notlarıiçeriyordökumanlar.pdf

  • 1. BMI2123 Data Structures Asst [email protected] Week 8:Trees So Far Arrays Now, familiar with algorithmic complexity analysis Order of running time Big-Oh function Stacks Queues A midterm exam 2 Today .. 3
  • 2. Linear vs Nonlinear Data Structures Linear Data Structure: Data is organized in sequential (linear) order. Data elements are traversed one after the other. Only one element can be directly reached while traversing. Examples: Array, stack, linked lists, queue Nonlinear Data Structure: Data is not organized in sequential order. Data can be organized hierarchically or random order. Examples: Tree, graph, dictionaries, heaps What is a Tree? A tree is an abstract data type that stores elements hierarchically. A tree is a graph without cycles A nonlinear structure Recall: A data structure is said to be nonlinear if its elements form a hierarchical classification where, data items appear at various levels In software systems, a tree is an abstract model of a hierarchical structure such as lists, vectors, and sequences A tree consists of nodes with a parent-child relation 6 Recursive Nature of Trees A tree is hierarchical collection of nodes. One of the nodes, known as the root, has no parent. The links leaving a node (any number of links are allowed) point to child nodes. Trees are recursive structures. Each child node is itself the root of a subtree. At the bottom of the tree are leaf nodes, which have no children. root Recursive definition: A tree is either: empty (null), or a root node that contains: data, a left subtree, and a right subtree. (The left and/or right subtree could be empty.) Trees in Computer Science folders/files on a computer family genealogy; organizational charts
  • 3. AI: decision trees compilers: parse tree a = (b + c) * d; cell phone T9 d + * a = c b Image source: https://images.datacamp.com/image/upload/v1677504957/decision_tree_for_heart_attack_prevention_2140 bd762d.png Trees in Computer Science(cont.) A parse tree Node: An object containing a data value and left/right children. Root: the node with no parents. There can be at most one root node in a tree Leaf: a node that has no children Branch: any internal node; neither the root nor a leaf parent: a node that refers to this one child: a node that this node refers to sibling: the nodes which are children of same parent A node p is an ancestor of node q if there exists a path from root to q and p appears on the path. The node q is called a descendant of p. Edge: the link from parent to child Subtree: the smaller tree of nodes on the left or right of the current node Tree Terminology Node: An object containing a data value and left/right children. Root: the node with no parents. There can be at most one root node in a tree A Leaf: a node that has no children E,J,K,H,I Branch: any internal node; neither the root nor a leaf B,C,D,F,G parent: a node that refers to this one B,C,D,F,G child: a node that this node refers to B,C,D,E,F,G,H,I,J,K sibling: the nodes which are children of same parent {B,C,D}, {E,F}, {H,I} Edge: the link from parent to child All links in the figure Tree Terminology Answers Tree Terminology(cont.) level or depth: length of the path from a root to a given node. The root node is at level zero. For example, (1 and 4) at the same level and (2 and 5) at the same level. The height of a tree is the number of links in the longest path from the root to a leaf. Empty tree Right-skewed tree One node
  • 4. Binary Trees A tree is called binary tree if each node has zero child, one child or two children. Empty tree is also a valid binary tree. There are four major types of binary trees: Full Binary Tree - Every parent node has either two or no children. Perfect Binary Tree - Every parent node has exactly two child nodes and all the external nodes (leaf nodes) are at the same level. Complete Binary Tree - In this case, every level must be filled, and all the leaf elements lean towards the left. Balanced Binary Tree - A binary tree in which the height difference between the left and the right child nodes is either 0 or 1. Tree ADT It is useful to store collections of positions. For example, the children of a node in a tree can be presented to the user as such a list. We define position list, to be a list whose elements are tree positions. The real power of a tree position arises from its ability to access the neighboring elements of the tree. Given a position p of tree T, we define the following: 14 Properties of Binary Trees For the following properties, let us assume that the height of the tree is h. Also, assume that root node is at height zero. where n is the number of nodes in the tree, h is the height of the tree Representation of A Binary Tree A binary tree data structure is represented using two methods. Those methods are 1-Array Representation and 2-Linked List Representation Array Representation In array representation of binary tree, we use a one dimensional array (1-D Array) to represent a binary tree.
  • 5. Array Representation(cont.) Waste of space problem! 2-Linked List Representation We use linked list to represent a binary tree. In a linked list, every node consists of three fields. First field, for storing left child address, second for storing actual data and third for storing right child address. Tree Traversal traversal: An examination of the elements of a tree. A pattern used in many tree algorithms and methods Common orderings for traversals: pre-order: process root node, then its left/right subtrees in-order: process left subtree, then root node, then right post-order: process left/right subtrees, then root node level-order: Access nodes level by level starting from root. (also known as Breadth First Search) 40 81 9 41 17 6 29 Root pre-order: ? in-order: ? post-order: ? level-order: ? Traversal trick To quickly generate a traversal: Trace a path around the tree. As you pass a node on the proper side, process it. pre-order: left side in-order: bottom post-order: right side pre-order: 17 41 29 6 9 81 40 in-order: 29 41 6 17 81 9 40 post-order: 29 6 41 81 40 9 17 40 81 9 41 17 6 29 overallRoot
  • 6. Examples 3 86 9 15 42 27 48 12 39 5 Solution for the examples Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression (2 (a 1) (3 b)) 23 2 a 1 3 b Note: Properties of Proper Binary Trees Notation n number of nodes e number of external nodes i number of internal nodes h height 24 Properties: e = i + 1 n = 2e - 1 h i h (n - 1)/2 e 2h h log2 e h log2 (n + 1) - 1
  • 7. Acknowledgement This material is prepared based on Data Structures and Algorithms in C++ 2e book and the lecture notes of Prof. Yung Yi from KAIST, South Korea. We thank to Michael T. Goodrich, Roberto Tamassia, David M. Mount who are the authors of the book and Prof. Yung Yi.