SlideShare a Scribd company logo
Lecture 4
Dynamic Data Structure Part 2
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 1
Content Lecture 4 Dynamic Data
Part 2
 Tree structures
 General
 Tree Representation
 Balanced Trees
 Binary Tree
 Sorted Binary Tree
 Binary Search Tree
 Insert an element in a Sorted Binary Tree
 Delete a node in a Sorted Binary Tree
 Other Tree Types
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 2
Tree structures
General
 Trees are one of the most important data structures
 There are several different variants
 We will have a closer look to some of them
 Trees are used in computer science for data storage,
searching and sorting
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 3
Tree structures
Definition
A Tree is a finite amount T of one or several nodes such that
 There is a significant node called root(T)
 The other nodes can be divided in disjunctive amount
T1, .. Tm
 Each of these amounts is again a Tree
 These Trees are called Sub Trees
 This is a recursive definition
 At the end a Tree with only one node remains
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 4
Tree Representation
 You can display Trees in different ways
 In this course outline Trees are illustrated with the root on
the top and the Sub Trees below
 This is a common illustration
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 5
FED
B C
A
Tree Representation
 These are some different representations
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 6
Tree Representation
Family Trees
 A common example for trees are Family Trees
 Two variants of Family Trees exist:
 Starting from an ancestor as root and illustrated all the
descendants
 Starting from a descendant as root and illustrated all the
ancestors
 In Family Trees you can have redundancies if some of the
ancestors have the same ancestors
 In this case the entries represent the role of the ancestor
(for example grandmother on the mother’s side)
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 7
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 8
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 9
Tree structures
Definitions for Trees
 Each node in a Tree is the root of one of the Sub Trees
 Each Tree has zero or more child nodes which are below in
the Tree
 A node with a child node is called parent node to the child
(also ancestor node or superior)
 A root node is a node with no parents
 Each Tree has at least one root node
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 10
Tree structures
Example
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 11
G
FED
B C
A root node
parent node of D, E, Fchild
node of A
Tree structures
 The depth of a node is the length of the path to its root
 A node p is an ancestor to node q if p exists on the path
from q to root
 The node q is called a descendant of p
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 12
Tree structures
Example
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 13
G
FED
B C
A
depth of F is 2
C is ancestor of D
D is a descendant of C
Tree structures
 The size of a node is the number of descendants a node has
including itself
 Siblings are nodes that share the same parent node
 Nodes without a child node are called leaf node or
terminal nodes
 Internal or inner node are nodes with one or more child
nodes therefore size > 0
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 14
Tree structures
Example
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 15
G
FED
B C
A size of A is 7
B and C are siblings
G is a leave node
F is an inner node
Tree structures
 In-degree of a node is the number of edges arriving at that
node
 The only node with In-degree = 0 is the root node
 Out-degree of a node is the number of edges leaving that
node
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 16
Tree structures
Example
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 17
G
FED
B C
A
In-degree of C 1
Out-degree of C is 3
In-degree of D 1
Out-degree
of D is 0
Tree structures
 The level of a node is defined
as:
 The level of the root(T) is 0
 The level of any other node
is increased by 1 to the
level of the node which is
the root of the superior sub
tree
level(node) = level(parent) + 1
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 18
G
FED
B C
A Level 0
Level 1
Level 2
Level 3
Balanced Trees
 If the relative order of the Sub Trees is important you call it
a Balanced Tree
 If the order is not important this is called an Oriented
Tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 19
G
FED
B C
A
G
EFD
B C
A
These Trees are
different if you look at
them as Balanced Tree
but they are consider
as the same if you look
at them as Oriented
Trees
Binary Trees
 A Binary Tree is a finite amount of nodes where the
amount is empty or contains a root and two disjunctive
Binary Trees
 These two Binary Trees are called left and right Sub Tree
of the root
 That means that every node of the tree has zero, one or two
child nodes
 Binary Trees are not a special case of Trees in general
 For example you can have an empty amount as a Binary
Tree but not as a General Tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 20
Binary Trees
 Any data in the Tree structure can be reached by starting at
the rood node and following either the left or the right
child
 Binary Trees are used for implementation of Binary Tree
Search and Binary Heaps
 Examples for Binary Trees are the elimination contest in
tennis or other sport competitions
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 21
Binary Trees
Example
 These two Binary Trees are not the same
 One has left and the other a right Sub Tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 22
B
A
B
A
Binary tree with a
left sub tree
Binary tree with a
right sub tree
Binary Trees
Implementation
 Binary Trees are easily implemented as a data type like
 One pointer points to the root
 If this pointer is null than the Tree is empty
 A node in the Binary Tree contains a pointer to an object
or a record and the two pointers to the left and the right
Sub Tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 23
Binary Trees
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 24
root
A
B
C
E FD
G H J
ǁ
ǁ
ǁ ǁ ǁ
ǁ
ǁ
ǁ ǁ ǁ
Binary Trees
 There are three possibilities to traverse a Binary Tree:
preorder, inorder and postorder
 If the Tree is empty there is nothing to do
 Otherwise the traverse possibilities are defined as:
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 25
preorder visit the root
traverse the left sub tree
traverse the right sub tree
inorder traverse the left sub tree
visit the root
traverse the right sub tree
postorder traverse the left sub tree
traverse the right sub tree
visit the root
B C
A
A B C
B A C
B C A
Binary Trees
Examples
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 26
preorder: A B D C E G F H I
inorder: D B A E G C H F I
postorder: D B G E H I F C A
G
FED
B C
A
IH
Note
The preorder traverse is also called Polish
notation (after the Polish logician Jan
Lukasiewicz) and the postorder traverse is
therefore called a backwards Polish
notation
Binary Trees
 Mathematical formulas can be represented by Binary Trees:
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 27
preorder - * * + 4 2 7 + 9 3 / 8 – 4
inorder (4 + 2) * 7 * (9 + 3) – (8/(-4)) (adding parentheses)
postorder 4 2 + 7 * 9 3 + * 8 4 - / -
-8*
* /
-
4+ 7
4 2
+
9 3
Sorted Binary Tree
A tree T is a Sorted Binary Tree where
 v: T  V function to receive the value of a node
 T a Binary Tree
 T1 the left Sub Tree (also a Sorted Binary Trees)
 T2 the right Sub Tree (also a Sorted Binary Trees) such that:
v(root(T)) > v(t) for all nodes t in T1
v(root(T)) ≤ v(t) for all nodes t in T2
 If you traverse a Sorted Binary Tree inorder, than all values
are reached in a sorted way
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 28
Sorted Binary Tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 29
7
961
3 8
5
Example
Inorder: 1 3 5 6 7 8 9
Binary Tree
Definitions
The height of a Tree is the length of the path from the root to
the deepest node in the Tree:
h(T) = 0 if the Tree T is empty
h(T) = max(h(T1), h(T2)) + 1
where T1 is the left and T2 the right Sub Tree
The cardinality of a Tree T is the number n of elements in
the Tree that means:
n = card(T)
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 30
Binary Tree
Example
The height of this tree is 4
The cardinality of this tree is 7
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 31
7
961
3 8
5
Binary Tree
A Tree is called height-balancing if the Tree T is empty or if
| h(T1) – h (T2) | ≤ 1
where h is the height of a Tree and also the Sub Trees T1 and
T2 are height-balancing Trees
A Tree is called balanced if the Tree T is empty or if
| card(T1) – card(T2) | ≤ 1
and also the Sub Trees T1 and T2 are balanced
If a Tree T is balance he is also balanced in its height
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 32
Sorted Binary Tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 33
height-balancing Tree but not a balanced
height left tree = 2, height right tree = 3
 |h(T1) – h(T2) = |2 – 3| = 1
card left tree = 2, card right tree = 4
 |card(T1) – card(T2) = |2 – 4| = 2
Balanced Tree ( height-balancing)
height left tree = 2, height right tree = 2
 |h(T1) – h(T2) = |2 – 2| = 0
card left tree = 3, card right tree = 3
 |card(T1) – card(T2) = |3 – 3| = 0
7
961
3 8
5
751
2 6
4
3
Binary Search Tree
k a value and the
s search element
v(s) = k
T (Binary Tree) with t = root(T)
If T is empty than there is nothing to do
If v(t) = k than the search element is found
If not than
 Replace T with the left subtree T1 if k < v(t)  T = T1
 Replace T with the right subtree T2 if k ≥ v(t)  T = T2
The effort for the search is O(h(T))
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 34
Binary Search Tree
Example
Search element k
k = 1
t = 5  1 < 5
t = 3  1 < 3
t = 1  1 = 1 element found!
k = 7
t = 5  7 > 5
t = 8  7 < 8
t = 6  7 > 6
t = 7  7 = 7 element found!
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 35
7
961
3 8
5
Insert a element in a Sorted
Binary Tree
u a values with k = v(u) to be insert
T a tree with t the root of Tree T
 Create a new binary sorted tree U containing only the root
u
 If T is empty replace T by U
 Otherwise
 replace T with the left sub tree T1 if k < v(t)  T = T1
 replace T with the right sub tree T2 if k ≥ v(t)  T = T2
 The effort is also O(h(T))
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 36
Insert a element in a Sorted
Binary Tree
Example
Insert in an empty Tree the elements 5, 1, 3, 2, 8, 4, 6, 7 and 9:
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 37
5
1
3
2 4
9
8
7
6
Delete a node in a Sorted Binary
Tree
Be u ϵ T the node to be removed from Tree T
Be U the sub tree from T with root(U) = u
 If the left Sub Tree U1 of U is empty than replace U by U1
 If the right Sub Tree U2 of U is empty than replace U by U2
 If both Sub Trees are not empty than:
 Chose an u2 ϵ U2 such that v(u2) ≤ v(t) for all t ϵ U2
 u2 has to be removed from U2
 u2 replace the node u
The effort to delete a node in a Tree is O(h(T)) due to the
search for the minimal element u2
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 38
Delete a node in a Sorted Binary
Tree
Example
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 39
Other Tree Types
 2-3 tree
 2-3-4 tree
 AA tree
 AVL tree
 B-tree
 Elastic binary tree
 Random binary tree
 Red-black tree
 Self-balancing binary search tree
 Unrooted binary tree
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 40
Any
questions?
23/10/2018 Lecture 4 Dynamic Data Structure Part 2 41

More Related Content

PPTX
Lecture4a dynamic data_structure
PPTX
Introduction to data structure ppt
PDF
Data and File Structure Lecture Notes
PDF
Introduction to Data Structure
PPT
Indexing Data Structure
PPT
Data structure
DOCX
Chapter 1
PDF
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR
Lecture4a dynamic data_structure
Introduction to data structure ppt
Data and File Structure Lecture Notes
Introduction to Data Structure
Indexing Data Structure
Data structure
Chapter 1
Introduction of Data Structures and Algorithms by GOWRU BHARATH KUMAR

What's hot (20)

PPT
Chapter 1( intro &amp; overview)
PPTX
Dynamic multi level indexing Using B-Trees And B+ Trees
PPT
Introductiont To Aray,Tree,Stack, Queue
PPT
Introduction to data structure
PPT
Lecture 1 data structures and algorithms
PPTX
Data Structure & Algorithms | Computer Science
PDF
Furnish an Index Using the Works of Tree Structures
PPTX
Introduction to data structure
PPTX
Introduction of Data Structure
PDF
Modern Database Systems - Lecture 01
DOCX
Data Structure Question Bank(2 marks)
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
DOC
DATA STRUCTURES - SHORT NOTES
PPSX
Data Structure # vpmp polytechnic
PPTX
Dsa module 1 ppt
PDF
104333 sri vidhya eng notes
PDF
Programming & Data Structure Lecture Notes
PDF
final_presentation
PDF
Introduction to data structure
PDF
[Queue , linked list , tree]
Chapter 1( intro &amp; overview)
Dynamic multi level indexing Using B-Trees And B+ Trees
Introductiont To Aray,Tree,Stack, Queue
Introduction to data structure
Lecture 1 data structures and algorithms
Data Structure & Algorithms | Computer Science
Furnish an Index Using the Works of Tree Structures
Introduction to data structure
Introduction of Data Structure
Modern Database Systems - Lecture 01
Data Structure Question Bank(2 marks)
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
DATA STRUCTURES - SHORT NOTES
Data Structure # vpmp polytechnic
Dsa module 1 ppt
104333 sri vidhya eng notes
Programming & Data Structure Lecture Notes
final_presentation
Introduction to data structure
[Queue , linked list , tree]
Ad

Similar to Lecture4b dynamic data_structure (20)

PPT
PPT
mitochondria moment and super computer integration.ppt
PPT
bst.ppt
PPTX
data structures module III & IV.pptx
PPTX
non linear data structure -introduction of tree
PPTX
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
PPTX
Tree Data Structure Tree Data Structure Details
PPTX
PPTX
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
PPTX
Lecture 8 data structures and algorithms
PDF
CS-102 BST_27_3_14v2.pdf
PPTX
Weak 13 Trees, BST update.pptxhjjujjjhhhy
PPTX
Trees in Data Structure
PPT
computer notes - Data Structures - 11
PPTX
data structure and algorithm chapter 8 computer
PPTX
Tree
PPTX
Unit 6 tree
PDF
Binary Search Tree
PDF
The graph theoretic definition of tree is : it is a finite set of one or more...
PDF
The graph theoretic definition of tree is : it is a finite set of one or more...
mitochondria moment and super computer integration.ppt
bst.ppt
data structures module III & IV.pptx
non linear data structure -introduction of tree
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Tree Data Structure Tree Data Structure Details
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
Lecture 8 data structures and algorithms
CS-102 BST_27_3_14v2.pdf
Weak 13 Trees, BST update.pptxhjjujjjhhhy
Trees in Data Structure
computer notes - Data Structures - 11
data structure and algorithm chapter 8 computer
Tree
Unit 6 tree
Binary Search Tree
The graph theoretic definition of tree is : it is a finite set of one or more...
The graph theoretic definition of tree is : it is a finite set of one or more...
Ad

More from mbadhi barnabas (8)

PPTX
Lecture3b searching
PPTX
Lecture3a sorting
PPTX
Lecture2b algorithm
PPTX
Lecture2a algorithm
PPTX
Lecture1b data types
PPTX
Lecture1a data types
PDF
Data struture and aligorism
PDF
Data structures and algorithm
Lecture3b searching
Lecture3a sorting
Lecture2b algorithm
Lecture2a algorithm
Lecture1b data types
Lecture1a data types
Data struture and aligorism
Data structures and algorithm

Recently uploaded (20)

PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
Global journeys: estimating international migration
PDF
Foundation of Data Science unit number two notes
PDF
Lecture1 pattern recognition............
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
A Quantitative-WPS Office.pptx research study
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
Mega Projects Data Mega Projects Data
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Global journeys: estimating international migration
Foundation of Data Science unit number two notes
Lecture1 pattern recognition............
Reliability_Chapter_ presentation 1221.5784
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
A Quantitative-WPS Office.pptx research study
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Business Acumen Training GuidePresentation.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
Mega Projects Data Mega Projects Data
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Business Ppt On Nestle.pptx huunnnhhgfvu
05. PRACTICAL GUIDE TO MICROSOFT EXCEL.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx

Lecture4b dynamic data_structure

  • 1. Lecture 4 Dynamic Data Structure Part 2 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 1
  • 2. Content Lecture 4 Dynamic Data Part 2  Tree structures  General  Tree Representation  Balanced Trees  Binary Tree  Sorted Binary Tree  Binary Search Tree  Insert an element in a Sorted Binary Tree  Delete a node in a Sorted Binary Tree  Other Tree Types 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 2
  • 3. Tree structures General  Trees are one of the most important data structures  There are several different variants  We will have a closer look to some of them  Trees are used in computer science for data storage, searching and sorting 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 3
  • 4. Tree structures Definition A Tree is a finite amount T of one or several nodes such that  There is a significant node called root(T)  The other nodes can be divided in disjunctive amount T1, .. Tm  Each of these amounts is again a Tree  These Trees are called Sub Trees  This is a recursive definition  At the end a Tree with only one node remains 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 4
  • 5. Tree Representation  You can display Trees in different ways  In this course outline Trees are illustrated with the root on the top and the Sub Trees below  This is a common illustration 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 5 FED B C A
  • 6. Tree Representation  These are some different representations 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 6
  • 7. Tree Representation Family Trees  A common example for trees are Family Trees  Two variants of Family Trees exist:  Starting from an ancestor as root and illustrated all the descendants  Starting from a descendant as root and illustrated all the ancestors  In Family Trees you can have redundancies if some of the ancestors have the same ancestors  In this case the entries represent the role of the ancestor (for example grandmother on the mother’s side) 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 7
  • 8. 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 8
  • 9. 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 9
  • 10. Tree structures Definitions for Trees  Each node in a Tree is the root of one of the Sub Trees  Each Tree has zero or more child nodes which are below in the Tree  A node with a child node is called parent node to the child (also ancestor node or superior)  A root node is a node with no parents  Each Tree has at least one root node 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 10
  • 11. Tree structures Example 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 11 G FED B C A root node parent node of D, E, Fchild node of A
  • 12. Tree structures  The depth of a node is the length of the path to its root  A node p is an ancestor to node q if p exists on the path from q to root  The node q is called a descendant of p 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 12
  • 13. Tree structures Example 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 13 G FED B C A depth of F is 2 C is ancestor of D D is a descendant of C
  • 14. Tree structures  The size of a node is the number of descendants a node has including itself  Siblings are nodes that share the same parent node  Nodes without a child node are called leaf node or terminal nodes  Internal or inner node are nodes with one or more child nodes therefore size > 0 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 14
  • 15. Tree structures Example 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 15 G FED B C A size of A is 7 B and C are siblings G is a leave node F is an inner node
  • 16. Tree structures  In-degree of a node is the number of edges arriving at that node  The only node with In-degree = 0 is the root node  Out-degree of a node is the number of edges leaving that node 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 16
  • 17. Tree structures Example 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 17 G FED B C A In-degree of C 1 Out-degree of C is 3 In-degree of D 1 Out-degree of D is 0
  • 18. Tree structures  The level of a node is defined as:  The level of the root(T) is 0  The level of any other node is increased by 1 to the level of the node which is the root of the superior sub tree level(node) = level(parent) + 1 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 18 G FED B C A Level 0 Level 1 Level 2 Level 3
  • 19. Balanced Trees  If the relative order of the Sub Trees is important you call it a Balanced Tree  If the order is not important this is called an Oriented Tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 19 G FED B C A G EFD B C A These Trees are different if you look at them as Balanced Tree but they are consider as the same if you look at them as Oriented Trees
  • 20. Binary Trees  A Binary Tree is a finite amount of nodes where the amount is empty or contains a root and two disjunctive Binary Trees  These two Binary Trees are called left and right Sub Tree of the root  That means that every node of the tree has zero, one or two child nodes  Binary Trees are not a special case of Trees in general  For example you can have an empty amount as a Binary Tree but not as a General Tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 20
  • 21. Binary Trees  Any data in the Tree structure can be reached by starting at the rood node and following either the left or the right child  Binary Trees are used for implementation of Binary Tree Search and Binary Heaps  Examples for Binary Trees are the elimination contest in tennis or other sport competitions 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 21
  • 22. Binary Trees Example  These two Binary Trees are not the same  One has left and the other a right Sub Tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 22 B A B A Binary tree with a left sub tree Binary tree with a right sub tree
  • 23. Binary Trees Implementation  Binary Trees are easily implemented as a data type like  One pointer points to the root  If this pointer is null than the Tree is empty  A node in the Binary Tree contains a pointer to an object or a record and the two pointers to the left and the right Sub Tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 23
  • 24. Binary Trees 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 24 root A B C E FD G H J ǁ ǁ ǁ ǁ ǁ ǁ ǁ ǁ ǁ ǁ
  • 25. Binary Trees  There are three possibilities to traverse a Binary Tree: preorder, inorder and postorder  If the Tree is empty there is nothing to do  Otherwise the traverse possibilities are defined as: 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 25 preorder visit the root traverse the left sub tree traverse the right sub tree inorder traverse the left sub tree visit the root traverse the right sub tree postorder traverse the left sub tree traverse the right sub tree visit the root B C A A B C B A C B C A
  • 26. Binary Trees Examples 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 26 preorder: A B D C E G F H I inorder: D B A E G C H F I postorder: D B G E H I F C A G FED B C A IH Note The preorder traverse is also called Polish notation (after the Polish logician Jan Lukasiewicz) and the postorder traverse is therefore called a backwards Polish notation
  • 27. Binary Trees  Mathematical formulas can be represented by Binary Trees: 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 27 preorder - * * + 4 2 7 + 9 3 / 8 – 4 inorder (4 + 2) * 7 * (9 + 3) – (8/(-4)) (adding parentheses) postorder 4 2 + 7 * 9 3 + * 8 4 - / - -8* * / - 4+ 7 4 2 + 9 3
  • 28. Sorted Binary Tree A tree T is a Sorted Binary Tree where  v: T  V function to receive the value of a node  T a Binary Tree  T1 the left Sub Tree (also a Sorted Binary Trees)  T2 the right Sub Tree (also a Sorted Binary Trees) such that: v(root(T)) > v(t) for all nodes t in T1 v(root(T)) ≤ v(t) for all nodes t in T2  If you traverse a Sorted Binary Tree inorder, than all values are reached in a sorted way 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 28
  • 29. Sorted Binary Tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 29 7 961 3 8 5 Example Inorder: 1 3 5 6 7 8 9
  • 30. Binary Tree Definitions The height of a Tree is the length of the path from the root to the deepest node in the Tree: h(T) = 0 if the Tree T is empty h(T) = max(h(T1), h(T2)) + 1 where T1 is the left and T2 the right Sub Tree The cardinality of a Tree T is the number n of elements in the Tree that means: n = card(T) 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 30
  • 31. Binary Tree Example The height of this tree is 4 The cardinality of this tree is 7 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 31 7 961 3 8 5
  • 32. Binary Tree A Tree is called height-balancing if the Tree T is empty or if | h(T1) – h (T2) | ≤ 1 where h is the height of a Tree and also the Sub Trees T1 and T2 are height-balancing Trees A Tree is called balanced if the Tree T is empty or if | card(T1) – card(T2) | ≤ 1 and also the Sub Trees T1 and T2 are balanced If a Tree T is balance he is also balanced in its height 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 32
  • 33. Sorted Binary Tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 33 height-balancing Tree but not a balanced height left tree = 2, height right tree = 3  |h(T1) – h(T2) = |2 – 3| = 1 card left tree = 2, card right tree = 4  |card(T1) – card(T2) = |2 – 4| = 2 Balanced Tree ( height-balancing) height left tree = 2, height right tree = 2  |h(T1) – h(T2) = |2 – 2| = 0 card left tree = 3, card right tree = 3  |card(T1) – card(T2) = |3 – 3| = 0 7 961 3 8 5 751 2 6 4 3
  • 34. Binary Search Tree k a value and the s search element v(s) = k T (Binary Tree) with t = root(T) If T is empty than there is nothing to do If v(t) = k than the search element is found If not than  Replace T with the left subtree T1 if k < v(t)  T = T1  Replace T with the right subtree T2 if k ≥ v(t)  T = T2 The effort for the search is O(h(T)) 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 34
  • 35. Binary Search Tree Example Search element k k = 1 t = 5  1 < 5 t = 3  1 < 3 t = 1  1 = 1 element found! k = 7 t = 5  7 > 5 t = 8  7 < 8 t = 6  7 > 6 t = 7  7 = 7 element found! 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 35 7 961 3 8 5
  • 36. Insert a element in a Sorted Binary Tree u a values with k = v(u) to be insert T a tree with t the root of Tree T  Create a new binary sorted tree U containing only the root u  If T is empty replace T by U  Otherwise  replace T with the left sub tree T1 if k < v(t)  T = T1  replace T with the right sub tree T2 if k ≥ v(t)  T = T2  The effort is also O(h(T)) 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 36
  • 37. Insert a element in a Sorted Binary Tree Example Insert in an empty Tree the elements 5, 1, 3, 2, 8, 4, 6, 7 and 9: 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 37 5 1 3 2 4 9 8 7 6
  • 38. Delete a node in a Sorted Binary Tree Be u ϵ T the node to be removed from Tree T Be U the sub tree from T with root(U) = u  If the left Sub Tree U1 of U is empty than replace U by U1  If the right Sub Tree U2 of U is empty than replace U by U2  If both Sub Trees are not empty than:  Chose an u2 ϵ U2 such that v(u2) ≤ v(t) for all t ϵ U2  u2 has to be removed from U2  u2 replace the node u The effort to delete a node in a Tree is O(h(T)) due to the search for the minimal element u2 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 38
  • 39. Delete a node in a Sorted Binary Tree Example 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 39
  • 40. Other Tree Types  2-3 tree  2-3-4 tree  AA tree  AVL tree  B-tree  Elastic binary tree  Random binary tree  Red-black tree  Self-balancing binary search tree  Unrooted binary tree 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 40
  • 41. Any questions? 23/10/2018 Lecture 4 Dynamic Data Structure Part 2 41