cs225 ch4 trees and search trees -...
TRANSCRIPT
8/19/2014
1
1. Tree ADT
2. Binary Trees
3. Binary Search Trees3 y
4. AVL Trees
Semester I (2014) 1
What is a Tree
In computer science, a tree is an abstract model
Computers”R”Us
1. Tree ADT
of a hierarchical structure
A tree consists of nodes with a parent‐child relation
Applications:
Organization charts
File systems
Sales R&DManufacturing
Laptops DesktopsUS International
Semester I (2014) 2
File systems
Programming environments
Europe Asia Canada
8/19/2014
2
Tree Terminology Root: node without parent (A)
Internal node: node with at least one child (A, B, C, F)
Subtree: tree consisting of a node and its descendants
1. Tree ADT
one child (A, B, C, F)
External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D)
Ancestors of a node: parent, grandparent, grand‐grandparent, etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of d ( )
A
B DC
Semester I (2014) 3
any node (3)
Descendant of a node: child, grandchild, grand‐grandchild, etc.
G HE F
I J K
sub‐tree
We use positions to abstract nodes
h d
Query methods: boolean isInternal(p)
b l i E l( )
1. Tree ADT
Generic methods: integer size()
boolean isEmpty()
objectIterator elements()
positionIterator positions()
Accessor methods:
boolean isExternal(p)
boolean isRoot(p)
Update methods: swapElements(p, q)
object replaceElement(p, o)
Additional update h d b d fi d
Semester I (2014) 4
position root()
position parent(p)
positionIterator children(p)
methods may be defined by data structures implementing the Tree ADT
8/19/2014
3
Pre‐order Traversal
A traversal visits the nodes of a tree in a systematic manner
Algorithm preOrder(v)visit(v)
1. Tree ADT
In a preorder traversal, a node is visited before its descendants
Application: print a structured document
Make Money Fast!1
9
for each child w of vpreorder (w)
Semester I (2014) 5
1. Motivations References2. Methods
2.1 StockFraud
2.2 PonziScheme
1.1 Greed 1.2 Avidity2.3 BankRobbery
2
3
5
4 6 7 8
9
Pre‐order Traversal
Example: Preorder traversal of an ordered tree, where the children of each node are ordered from left to right.
1. Tree ADT
Semester I (2014) 6
8/19/2014
4
Post‐order Traversal
In a postorder traversal, a node is visited after its descendants
Algorithm postOrder(v)for each child w of v
1. Tree ADT
Application: compute space used by files in a directory and its subdirectories
postOrder (w)visit(v)
cs16/9
3 78
Semester I (2014) 7
homeworks/todo.txt1K
programs/
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
3
1
7
2 4 5 6
Post‐order Traversal
Example: Postorder traversal
1. Tree ADT
Semester I (2014) 8
8/19/2014
5
Class Assignment Give a tree as the figure
below:
Answer the questions1. What is Root?
Computers”R”Us
Sales R&DManufacturing
2. What are internal nodes?
3. What are external nodes?
4. What are ancestors of the node US?
5. What is the depth of the node Asia?
6. What is height of the tree
7. What descendant of the
Laptops DesktopsUS International
Europe Asia Canada
7
node Sales?
8. What is pre‐order traversal of the tree?
9. What is post‐order traversal of the tree?
Semester I (2014) 9
A binary tree is a tree with the following properties: Each internal node has two children
The children of a node are an
Applications: arithmetic
expressions
decision processes
searching
2. Binary Trees
The children of a node are an ordered pair
We call the children of an internal node left child and right child
Alternative recursive definition: a binary tree is either
i i f i l d
searching
A
B C
Semester I (2014) 10
a tree consisting of a single node, or
a tree whose root has an ordered pair of children, each of which is a binary tree
F GD E
H I
8/19/2014
6
Arithmetic Expression Tree
Binary tree associated with an arithmetic expression internal nodes: operators
2. Binary Trees
internal nodes: operators
external nodes: operands
Example: arithmetic expression tree for the expression (2 (a 1) (3 b))
Semester I (2014) 11
2
a 1
3 b
Decision Tree
Binary tree associated with a decision process internal nodes: questions with yes/no answer
2. Binary Trees
internal nodes: questions with yes/no answer
external nodes: decisions
Example: dining decision
Want a fast meal?
Yes No
Semester I (2014) 12
How about coffee? On expense account?
Starbucks Spike’s Al Forno Café Paragon
Yes No Yes No
8/19/2014
7
Properties of Binary Trees
Notation
n number of nodes
Properties:
e i 1
2. Binary Trees
e number of external nodes
i number of internal nodes
h height
n 2e 1
h i
h (n 1)2 e 2h
h log2 e
Semester I (2014) 13
h log2 (n 1) 1
BinaryTree ADT
The BinaryTree ADT extends the Tree ADT
Update methods may b d fi d b d t
2. Binary Trees
extends the Tree ADT, i.e., it inherits all the methods of the Tree ADT
Additional methods:
position leftChild(p)
position rightChild(p)
be defined by data structures implementing the BinaryTree ADT
Semester I (2014) 14
position rightChild(p)
position sibling(p)
8/19/2014
8
Data Structure for Trees A node is represented by an object storing Element
1. Tree ADT
Element
Parent node
Sequence of children nodes
Node objects implement the Position ADT
B
B
A D F
Semester I (2014) 15
DA
C E
F
C
E
Data Structure for Binary Trees A node is represented by
an object storing
Element
2. Binary Trees
Parent node
Left child node
Right child node
Node objects implement the Position ADT
B
B
A D
Semester I (2014) 16
DA
C E
C E
8/19/2014
9
Auxiliary Structure Node
typedef int Object;struct Node {
Data Structure for Binary Trees
2. Binary Trees
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
Position Class
class Position {
Data Structure for Binary Trees
2. Binary Trees
private:NodePtr node;
public:Position(NodePtr n = NULL){ node = n;}
Object& element() const{ t d l t }{ return node‐>element;}
bool isNull() const{return node == NULL;}
friend LinkedBinaryTree;};
Semester I (2014) 18
8/19/2014
10
C++ Implementation
Tree interface
BinaryTree interface expandExternal(v)
2. Binary Trees
yextending Tree
Classes implementing Treeand BinaryTree and providing
Constructors
Update methods
Print methods
A
removeAboveExternal(w)
A
v v
Semester I (2014) 19
Examples of updates for binary trees
expandExternal(v)
removeAboveExternal(w)
A
CB
Bw
The complete structure for LinkedBinaryTree
typedef int Elem; class LinkedBinaryTree { protected:// insert Node declaration here. . .public:
Position removeAboveExternal(const Position& p); // remove p and parent// housekeeping functions omitted. . .
protected: // local utilities
2. Binary Tree ADT: C++ Implementation
public:// insert Position declaration here. . .public:LinkedBinaryTree(); // constructorint size() const; // number of nodesbool empty() const; // is tree empty?Position root() const; // get the rootPositionList positions() const; // list of nodes
protected: // local utilitiesvoid preorder(Node* v, PositionList& pl) const; // preorder utilityprivate:Node* root; // pointer to the rootint n; // number of nodes};//… page 291 Textbook
//void addRoot(); // add root to empty treevoid expandExternal(const Position& p); // expand external node
Semester I (2014) 20
8/19/2014
11
Class Assignment1. 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
3. Binary Search Tree
A binary search tree is a binary tree storing keys (or key‐element pairs) at its internal nodes and
An inorder traversal of a binary search trees visits the keys in increasing dits internal nodes and
satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have
order
6
92v
Semester I (2014) 22
key(u) key(v) key(w)
External nodes do not store items
41 8u w
8/19/2014
12
Search To search for a key k, we
trace a downward path starting at the root
Algorithm find (k, v)if T.isExternal (v)
return Position(null)
3. Binary Search Trees
g
The next node visited depends on the outcome of the comparison of kwith the key of the current node
If we reach a leaf, the key is not found and we
if k key(v)return find (k, T.leftChild(v))
else if k key(v)return Position(v)
else { k key(v) }return find (k, T.rightChild(v))
6
Semester I (2014) 23
return a null position
Example: find(4)
6
92
41 8
Insertion To perform operation
insertItem(k, o), we search
6
92
3. Binary Search Trees
( , ),for key k
Assume k is not already in the tree, and let let w be the leaf reached by the search
We insert k at node w and expand w into an internal node
6
92
41 8
w
Semester I (2014) 24
Example: insert 592
41 8
5w
8/19/2014
13
Deletion
To perform operation removeElement(k), we search
6
92
3. Binary Search Trees
( ),for key k
Assume key k is in the tree, and let let v be the node storing k
If node v has a leaf child w, we remove v and w from the tree with operation
92
41 8
5
vw
6
Semester I (2014) 25
premoveAboveExternal(w)
Example: remove 492
51 8
Deletion (cont.)
We consider the case where the key k to be removed is stored at
3
1v
3. Binary Search Trees
key k to be removed is stored at a node v whose children are both internal
we find the internal node w that follows v in an inorder traversal
we copy key(w) into node v
we remove node w and its left
8
6 9
5w
z
2
1
Semester I (2014) 26
child z (which must be a leaf) by means of operation removeAboveExternal(z)
Example: remove 3
5
8
6 9
v
2
8/19/2014
14
Performance
Consider a dictionary with n items
3. Binary Search Trees
with n items implemented by means of a binary search tree of height h the space used is O(n) methods find(), insertItem() and removeElement() take
Semester I (2014) 27
O(h) time
The height h is O(n) in the worst case and O(log n) in the best case
Binary Search in Ordered Dictionary
Binary search performs operation find(k) on a dictionary implemented by means of an array‐based sequence, sorted by key
3. Binary Search Tree
similar to the high‐low game
at each step, the number of candidate items is halved
terminates after O(log n) steps
Example: find(7)
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
0
0
ml h
Semester I (2014) 28
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
1 3 4 5 7 8 9 11 14 16 18 19
0
0
0
ml h
ml h
lm h
8/19/2014
15
Class Assignment
1. Insert into an initially empty binary search tree,
3. Binary Search Tree
1. Insert into an initially empty binary search tree, items with the following key (in this order): 30, 40, 24, 58, 48, 26, 11, 13. Draw the tree after each insertion.
Semester I (2014) 29
AVL Tree Definition
AVL trees are 4
4. AVL Tree
t ees a ebalanced.
An AVL Tree is a binary search treesuch that for every internal node v of T,
88
44
17 78
32 50
48 62
2
4
1
1
2
3
1
1
Semester I (2014) 30
the heights of the children of v can differ by at most 1. An example of an AVL tree where the
heights are shown next to the nodes:
8/19/2014
16
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
3
4 n(1)
n(2)4. AVL Tree
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
Semester I (2014) 31
g ( ) ( ) g ( ) ( )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)
Insertion in an AVL Tree
Insertion is as in a binary search tree Always done by expanding an external node.
4. AVL Tree
Example:44
17 78
32 50 88
48 62
a=y
c=z
44
17 78
32 50 88
48 62
Semester I (2014) 32
48 62
54w
b=x48 62
before insertion after insertion
8/19/2014
17
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
4. AVL Tree
three
b=y
a=z
c=x
T0
T1 b=y
c=y
b=x
a=z
T0
T3b=x
case 2: double rotation(a right rotation about c, then a left rotation about a)
(other two cases are symmetrical)
Semester I (2014) 33
T1
T2 T3
b=y
a=z c=x
T0 T1 T2 T3
T1 T2
b=x
c=ya=z
T0 T1 T2 T3
case 1: single rotation(a left rotation about a)
Insertion Example, continued44
17 782
5
1 3
4
1y
z
2
67
4. AVL Tree
8832 50
48 621 2
54
1
T0T2
T3
x3
4
5
1
44
17 622
4
3 x
y z
unbalanced...
2
4
6
T1
Semester I (2014) 34
88
7832 50
48
1
1
2 2
1
54
1
T0 T1
T2
T3
y z
...balanced
1
2
3 5 7
8/19/2014
18
Restructuring (as Single Rotations)
Single Rotations:
4. AVL Tree
T0T1
T2
T3
c = xb = y
a = z
T0 T1 T2
T3
c = xb = y
a = zsingle rotation
Semester I (2014) 35
T3T2
T1
T0
a = xb = y
c = z
T0T1T2
T3
a = xb = y
c = zsingle rotation
T0T1 T2 T3
Restructuring (as Double Rotations) double rotations:
4. AVL Tree
double rotationa = z
b = xc = y
T0T2
T1
T3 T0
T2T3T1
a = zb = x
c = y
Semester I (2014) 36
double rotationc = z
b = xa = y
T0T2
T1
T3 T0
T2T3 T1
c = zb = x
a = y
8/19/2014
19
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
4. AVL Tree
w, may cause an imbalance. Example:
44
17
7832 50
62
44
17
7850
62
Semester I (2014) 37
8848 54 8848 54
before deletion of 32 after deletion
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,
4. AVL Tree
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
44
17 62 b
a=z
44 78
62
Semester I (2014) 38
17
7850
8848
62
54
w
c=x
b=y
17 50 88
48 54
8/19/2014
20
Running Times for AVL Trees a single restructure is O(1)
using a linked‐structure binary tree
4. AVL 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)
Semester I (2014) 39
( g ) initial find is O(log n)
Restructuring up the tree, maintaining heights is O(log n)
Class Assignment
Balance the binary search tree below
4. AVL Tree
Balance the binary search tree below
5
1
8
6 9
2
10
Semester I (2014) 40
10
12
8/19/2014
21
15
Class Assignment Balance the binary search tree below
14
5
8
6 9
2
10
121
2
3
4
5
15
8
6
9
2
10
121
2
3
4
1
Semester I (2014) 41