Binary Trees: ECE 250 Algorithms and Data Structures
Binary Trees: ECE 250 Algorithms and Data Structures
Binary trees
ece.uwaterloo.ca
[email protected]
Outline
Definition
A full node is a node where both the left and right sub-trees are non-
empty trees
Legend:
full nodes neither leaf nodes
Binary trees
9
5.1.1 Definition
public:
Binary_node( Type const & );
The recursive size function runs in Q(n) time and Q(h) memory
– These can be implemented to run in Q(1)
template <typename Type>
int Binary_node<Type>::size() const {
if ( left() == nullptr ) {
return ( right() == nullptr ) ? 1 : 1 + right()->size();
} else {
return ( right() == nullptr ) ?
1 + left()->size() :
1 + left()->size() + right()->size();
}
Binary trees
16
5.1.2 Height
The recursive height function also runs in Q(n) time and Q(h) memory
– Later we will implement this in Q(1) time
int Binary_node<Type>::height() const {
if ( left() == nullptr ) {
return ( right() == nullptr ) ? 0 : 1 + right()->height();
} else {
return ( right() == nullptr ) ?
1 + left()->height() :
1 + left()->height() + right()->height();
}
Binary trees
17
5.1.2 Clear
if ( right() != nullptr ) {
right()->clear( p_right_tree );
}
delete this;
p_to_this = nullptr;
}
Binary trees
18
5.1.3 Run Times
Recall that with linked lists and arrays, some operations would run in
Q(n) time
lg( 1000 ) ≈ 10 kB
lg( 1 000 000 ) ≈ 20 MB
lg( 1 000 000 000 ) ≈ 30 GB
lg( 1 000 000 000 000 ) ≈ 40 TB
lg( 1000n ) ≈ 10 n
http://xkcd.com/394/
Binary trees
20
5.1.4.1 Application: Ropes
For example,
‘A’ 65 010000012
‘B’ 66 010000102
‘a’ 97 011000012
‘b’ 98 011000102
‘ ’ 32 001000002
J.R.R. Tolkien
Binary trees
22
5.1.4.1 Application: Ropes
The string
References: http://en.wikipedia.org/wiki/Rope_(computer_science)
J.R.R. Tolkien, The Hobbit
Binary trees
26
5.1.4.1 Application: Ropes
References: http://en.wikipedia.org/wiki/Rope_(computer_science)
J.R.R. Tolkien, The Hobbit
Binary trees
27
5.1.4.2 Application: Expression Trees
Observations:
– Internal nodes store operators
– Leaf nodes store literals or variables
– No nodes have just one sub tree
– The order is not relevant for
• Addition and multiplication (commutative)
– Order is relevant for
• Subtraction and division (non-commutative)
– It is possible to replace non-commutative operators using the unary
negation and inversion:
(a/b) = a b-1 (a – b) = a + (–b)
Binary trees
29
5.1.4.2 Application: Expression Trees
3 4 a × b c + + × d 5 ÷ 6 e – + +
Binary trees
30
5.1.4.2 Application: Expression Trees
Summary
We looked at:
– Properties
– Applications
Binary trees
32
Usage Notes
• These slides are made publicly available on the web for anyone to
use
• If you choose to use them, or a part thereof, for a course at another
institution, I ask only three things:
– that you inform me that you are using the slides,
– that you acknowledge my work, and
– that you alert me of any mistakes which I made or changes which you
make, and allow me the option of incorporating such changes (with an
acknowledgment) in my set of slides
Sincerely,
Douglas Wilhelm Harder, MMath
[email protected]