ECE391 Ch8 Trees and Search Trees
ECE391 Ch8 Trees and Search Trees
1. Tree ADT
What is a Tree
Computers”R”U
⚫In computer science, a s
tree is an abstract model
of a hierarchical structure
⚫A tree consists of nodes Sale
Manufacturing R&D
with a parent-child s
relation
⚫Applications:
⚫Organization charts US International Laptops Desktops
⚫File systems
⚫Programming
environments
Europe Asia Canada
2
9/4/
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
1. Tree ADT
4
9/4/
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. Reference
2. Methods
Motivations s
3 4 6 7 8
2.1
1.2 2.2 Ponzi 2.3 Bank
1.1 Greed Stock
Avidity Scheme Robbery
Fraud
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.
6
9/4/
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
1. Tree ADT
Post-order Traversal
⚫Example: Postorder traversal
8
9/4/
Class Assignment
⚫Give a tree as the figure Computers”R”Us
below:
⚫Answer the questions Manufacturin R&
Sales
1. What is Root? g D
2. What are internal nodes?
3. What are external nodes? U Internationa
Laptops Desktops
4. What are ancestors of S l
the node US?
5. What is the depth of the
node Asia? Asi
Europe Canada
a
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?
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: a binary tree is B C
either
⚫a tree consisting of a single node,
or D E F G
10
9/4/
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
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 N
o
How about coffee? On expense account?
Yes N Yes N
o o
Starbucks Spike’s Al Forno Café Paragon
12
9/4/
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 ≤ 2h
h height ■ h ≥ log2 e
■ h ≥ log2 (n + 1) − 1
13
2. Binary Trees
BinaryTree ADT
⚫The BinaryTree ADT ⚫Update methods
extends the Tree ADT, may be defined by
i.e., it inherits all the data structures
methods of the Tree ADT implementing the
⚫Additional methods: BinaryTree ADT
⚫ position leftChild(p)
⚫ position rightChild(p)
⚫ position sibling(p)
14
9/4/
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
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
16
9/4/
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;
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;
};
18
9/4/
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
19
20
9/4/
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.
21
22
9/4/
⚫Example: find(4) 2 9
>
1 4 = 8
23
24
9/4/
25
26
9/4/
27
29
4. AVL Tree
AVL Tree Definition
30
9/4/
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)
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: 4 4
4 4
1 7 1 7 c=z
7 8 7 8
a=y
3 5 8 3 5 8
2 0 8 2 0 8
4 6 4 6
8 2 8 2 b=x
5
w 4
32
9/4/
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=
a= case 2: double rotation
z are symmetrical) z
(a right rotation about c,
c= then a left rotation about a)
b=
y
y T
T 0
0 c= b=
x x
T
T b= 3
b=
1 y x
T T
T T 1 2
2 3 a= c= a= c=
z x z y
33
4. AVL Tree
Insertion Example, continued
unbalanced...
4
T1 44
2 3 x
17 62
y z
1 2 2
32 50 78
1 1 1
...balanced 48 54 88
T2
T0 T1 T3
34
9/4/
4. AVL Tree
Restructuring (as Single Rotations)
⚫Single Rotations:
T0
T1 T2 T3
35
4. AVL Tree
Restructuring (as Double Rotations)
⚫double rotations:
36
9/4/
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:
4 4
4 4
1 6 1 6
7 2 7 2
3 5 7 5 7
2 0 8 0 8
4 5 8 4 5 8
8 4 8 8 4 8
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
6
a=z 4 2
4
4 7
1 6 4 8
w b=y
7 2
1 5 8
5 7 c=x 7 0 8
0 8
4 5
4 5 8 8 4
8 4 8
38
9/4/
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)
39
4. AVL Tree
Class Assignment
6 9
1
0
1
2
40
9/4/
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 6 1 2
0
1 2
1 1
0 9
2
1 1
2
41