ECE391 Ch4 Trees and Search Trees
ECE391 Ch4 Trees and Search Trees
1. Tree ADT
2. Binary Trees
3. Binary Search Trees
4. AVL Trees
Semester I (2014) 1
1. Tree ADT
What is a Tree
In computer science, a Computers”R”Us
tree is an abstract model
of a hierarchical structure
A tree consists of nodes
Sales Manufacturing R&D
with a parent-child
relation
Applications:
Organization charts US International Laptops Desktops
File systems
Programming
environments
Europe Asia Canada
Semester I (2014) 2
1
4/9/2021
1. Tree ADT
Tree Terminology
Root: node without parent (A)
Internal node: node with at least Subtree: tree consisting of
one child (A, B, C, F) a node and its descendants
External node (a.k.a. leaf ): node
without children (E, I, J, K, G, H, D) A
Ancestors of a node: parent,
grandparent, grand-grandparent, etc.
Depth of a node: number of B C D
ancestors
Height of a tree: maximum depth of
any node (3) E F G H
Descendant of a node: child,
grandchild, grand-grandchild, etc. sub-tree
I J K
Semester I (2014) 3
1. Tree ADT
Semester I (2014) 4
2
4/9/2021
1. Tree ADT
Pre-order Traversal
A traversal visits the nodes of a Algorithm preOrder(v)
tree in a systematic manner visit(v)
In a preorder traversal, a node is for each child w of v
visited before its descendants preorder (w)
Application: print a structured
document
1
Make Money Fast!
2 5 9
1. Motivations 2. Methods References
3 4 6 7 8
2.1 Stock 2.2 Ponzi 2.3 Bank
1.1 Greed 1.2 Avidity
Fraud Scheme Robbery
Semester I (2014) 5
1. Tree ADT
Pre-order Traversal
Example: Preorder traversal of an ordered tree, where the children of
each node are ordered from left to right.
Semester I (2014) 6
3
4/9/2021
1. Tree ADT
Post-order Traversal
In a postorder traversal, a node Algorithm postOrder(v)
is visited after its descendants for each child w of v
Application: compute space postOrder (w)
used by files in a directory and visit(v)
its subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K
1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Semester I (2014) 7
1. Tree ADT
Post-order Traversal
Example: Postorder traversal
Semester I (2014) 8
4
4/9/2021
Class Assignment
Give a tree as the figure Computers”R”Us
below:
Answer the questions
Sales Manufacturing R&D
1. What is Root?
2. What are internal nodes?
3. What are external nodes?
US International Laptops Desktops
4. What are ancestors of the
node US?
5. What is the depth of the
node Asia? Europe Asia Canada
6. What is height of the tree
7. What descendant of the
node Sales?
8. What is pre-order
traversal of the tree?
9. What is post-order
traversal of the tree?
Semester I (2014) 9
2. Binary Trees
A binary tree is a tree with the Applications:
following properties: arithmetic
Each internal node has two expressions
children decision processes
The children of a node are an searching
ordered pair
We call the children of an
internal node left child and A
right child
Alternative recursive definition:
B C
a binary tree is either
a tree consisting of a single node,
or
D E F G
a tree whose root has an ordered
pair of children, each of which is a
binary tree
H I
Semester I (2014) 10
5
4/9/2021
2. Binary Trees
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))
2 - 3 b
a 1
Semester I (2014) 11
2. Binary Trees
Decision Tree
Binary tree associated with a decision process
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision
Yes No
How about coffee? On expense account?
Yes No Yes No
Starbucks Spike’s Al Forno Café Paragon
Semester I (2014) 12
6
4/9/2021
2. Binary Trees
Properties of Binary Trees
Notation Properties:
n number of nodes e = i + 1
e number of external n = 2e - 1
nodes h i
i number of internal h (n - 1)/2
nodes
e 2
h
h height
h log2 e
h log2 (n + 1) - 1
Semester I (2014) 13
2. Binary Trees
BinaryTree ADT
The BinaryTree ADT Update methods may
extends the Tree ADT, be defined by data
i.e., it inherits all the structures
methods of the Tree ADT implementing the
Additional methods: BinaryTree ADT
position leftChild(p)
position rightChild(p)
position sibling(p)
Semester I (2014) 14
7
4/9/2021
1. Tree ADT
Data Structure for Trees
A node is represented by
an object storing
Element
Parent node
B
Sequence of children
nodes
Node objects implement
the Position ADT A D F
B
A D F
C E
C E
Semester I (2014) 15
2. Binary Trees
Data Structure for Binary Trees
A node is represented by
an object storing
Element
Parent node
Left child node
B
Right child node
Node objects implement
the Position ADT
B
A D
A D
C E C E
Semester I (2014) 16
8
4/9/2021
2. Binary Trees
Data Structure for Binary Trees
Auxiliary Structure Node
typedef int Object;
struct Node {
Object element;
Node* parent;
Node* left;
Node* right;
Node() : element(Object())
{ parent = left = right = NULL; }
Node* sibling() const {
return (this == parent -> left ? parent->right : parent-> left);
}
};
typedef Node* NodePtr;
Semester I (2014) 17
2. Binary Trees
Data Structure for Binary Trees
Position Class
class Position {
private:
NodePtr node;
public:
Position(NodePtr n = NULL)
{ node = n;}
Object& element() const
{ return node->element;}
bool isNull() const
{return node == NULL;}
friend LinkedBinaryTree;
};
Semester I (2014) 18
9
4/9/2021
2. Binary Trees
C++ Implementation
Tree interface expandExternal(v)
BinaryTree interface
v v
extending Tree
A
Classes implementing Tree A
and BinaryTree and
providing
Constructors
Update methods
removeAboveExternal(w)
Print methods
Examples of updates for
binary trees A B
expandExternal(v) w
removeAboveExternal(w) B C
Semester I (2014) 19
Semester I (2014) 20
10
4/9/2021
Class Assignment
1. Write a C++ function that print post-order
traversal of a binary tree to output screen.
2. Write a C++ function copy(T1 ,T2, v1, v2) that copy
the node v2 of the tree T2 to the position of the
node v1 of the tree T1.
Semester I (2014) 21
Semester I (2014) 22
11
4/9/2021
1 4 = 8
Semester I (2014) 23
Semester I (2014) 24
12
4/9/2021
Semester I (2014) 25
Semester I (2014) 26
13
4/9/2021
Semester I (2014) 27
Semester I (2014) 28
14
4/9/2021
Semester I (2014) 29
4. AVL Tree
AVL Tree Definition
Semester I (2014) 30
15
4/9/2021
4 n(1)
Height of an AVL Tree
Fact: The height of an AVL tree storing n keys is O(log n).
Proof: Let us bound n(h): the minimum number of internal
nodes of an AVL tree of height h.
We easily see that n(1) = 1 and n(2) = 2
For n > 2, an AVL tree of height h contains the root node, one
AVL subtree of height n-1 and another of height n-2.
That is, n(h) = 1 + n(h-1) + n(h-2)
Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). So
n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), … (by induction),
n(h) > 2in(h-2i)
Solving the base case we get: n(h) > 2 h/2-1
Taking logarithms: h < 2log n(h) +2
Thus the height of an AVL tree is O(log n)
Semester I (2014) 31
4. AVL Tree
Insertion in an AVL Tree
Insertion is as in a binary search tree
Always done by expanding an external node.
Example: 44 44
c=z
17 78 17 78
a=y
32 50 88 32 50 88
48 62 48 62
b=x
54
w
Semester I (2014) 32
16
4/9/2021
4. AVL Tree
Trinode Restructuring
let (a,b,c) be an inorder listing of x, y, z
perform the rotations needed to make b the topmost node of the
three
(other two cases
a=z case 2: double rotation
a=z are symmetrical)
(a right rotation about c,
c=y then a left rotation about a)
b=y
T0
T0
c=x b=x
T3
T1 b=y b=x
T1 T2
T2 T3
a=z c=x a=z c=y
Semester I (2014) 33
4. AVL Tree
Insertion Example, continued
5
44
2
z 64
17 78 7
3
2 y 1
1
32 1 50 4 88
1
2 x
48
1
3 62
5
54 T3
unbalanced...
T0 T2
4
T1 44 4 x
2 3
17
2y 62
z 6
1 2 2
32
1
1 50 3 5
78 7
1 1
...balanced 48 54 88
T2
T0 T1 T3
Semester I (2014) 34
17
4/9/2021
4. AVL Tree
Restructuring (as Single Rotations)
Single Rotations:
T0 T3
T1 T3 T0 T1 T2
T2
T3
T0 T2
TT03
T1
TT21 T1
T2 TT30
Semester I (2014) 35
4. AVL Tree
Restructuring (as Double Rotations)
double rotations:
T0 T2
T2 T3 T0 T1 T3
T1
T0 T2
T3 T2 T3 T1 T0
T1
Semester I (2014) 36
18
4/9/2021
4. AVL Tree
Removal in an AVL Tree
Removal begins as in a binary search tree, which means the
node removed will become an empty external node. Its parent,
w, may cause an imbalance.
Example:
44 44
17 62 17 62
32 50 78 50 78
48 54 88 48 54 88
Semester I (2014) 37
4. AVL Tree
Rebalancing after a Removal
Let z be the first unbalanced node encountered while travelling up
the tree from w. Also, let y be the child of z with the larger height,
and let x be the child of y with the larger height.
We perform restructure(x) to restore balance at z.
As this restructuring may upset the balance of another node
higher in the tree, we must continue checking for balance until
the root of T is reached
62
a=z 44
44 78
w 17 62 b=y
17 50 88
50 78 c=x
48 54
48 54 88
Semester I (2014) 38
19
4/9/2021
4. AVL Tree
Running Times for AVL Trees
a single restructure is O(1)
using a linked-structure binary tree
find is O(log n)
height of tree is O(log n), no restructures needed
insert is O(log n)
initial find is O(log n)
Restructuring up the tree, maintaining heights is O(log n)
remove is O(log n)
initial find is O(log n)
Restructuring up the tree, maintaining heights is O(log n)
Semester I (2014) 39
4. AVL Tree
Class Assignment
6 9
10
12
Semester I (2014) 40
20
4/9/2021
Class Assignment
Balance the binary search tree below
1 1
5 4
5 5
4 3
1 2 8 1 2 8
3
6 9 2
6 10
2
10 1
9 12
1
12
Semester I (2014) 41
21