Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
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
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
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
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
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
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
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
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
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