0% found this document useful (0 votes)
26 views21 pages

ECE391 Ch4 Trees and Search Trees

Uploaded by

Minh Trường
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views21 pages

ECE391 Ch4 Trees and Search Trees

Uploaded by

Minh Trường
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

4/9/2021

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

 We use positions to abstract Query methods:


nodes  boolean isInternal(p)
 Generic methods:  boolean isExternal(p)
 integer size()  boolean isRoot(p)
 boolean isEmpty() Update methods:
 objectIterator elements()  swapElements(p, q)
 positionIterator positions()  object replaceElement(p, o)
 Accessor methods: Additional update
 position root() methods may be defined
 position parent(p) by data structures
 positionIterator children(p) implementing the 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

Want a fast meal?

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

2. Binary Tree ADT: C++ Implementation


The complete structure for LinkedBinaryTree
typedef int Elem; Position removeAboveExternal(const
class LinkedBinaryTree { Position& p); // remove p and parent
protected: // housekeeping functions omitted. . .
// insert Node declaration here. . .
public: protected: // local utilities
// insert Position declaration here. . . void preorder(Node* v, PositionList& pl)
public: const; // preorder utility
LinkedBinaryTree(); // constructor private:
int size() const; // number of nodes Node* root; // pointer to the root
bool empty() const; // is tree empty? int n; // number of nodes
Position root() const; // get the root };
PositionList positions() const; //… page 291 Textbook
// list of nodes
void addRoot(); // add root to empty tree
void expandExternal(const Position& p);
// expand external node

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

3. Binary Search Tree


 A binary search tree is a  An inorder traversal of a
binary tree storing keys binary search trees visits
(or key-element pairs) at the keys in increasing
its internal nodes and order
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 6
the right subtree of v. We v
2 9
have
key(u)  key(v)  key(w) u w
1 4 8
 External nodes do not
store items

Semester I (2014) 22

11
4/9/2021

3. Binary Search Trees


Search
 To search for a key k, we Algorithm find (k, v)
trace a downward path if T.isExternal (v)
starting at the root return Position(null)
 The next node visited if k < key(v)
depends on the outcome return find (k, T.leftChild(v))
of the comparison of k else if k = key(v)
with the key of the return Position(v)
current node else { k > key(v) }
 If we reach a leaf, the key return find (k, T.rightChild(v))
is not found and we
< 6
return a null position
 Example: find(4) 2
>
9

1 4 = 8

Semester I (2014) 23

3. Binary Search Trees


Insertion
6
 To perform operation <
insertItem(k, o), we search 2 9
>
for key k
1 4 8
 Assume k is not already in >
the tree, and let let w be the
leaf reached by the search w
 We insert k at node w and
expand w into an internal 6
node 2 9
 Example: insert 5
1 4 8
w
5

Semester I (2014) 24

12
4/9/2021

3. Binary Search Trees


Deletion
6
 To perform operation <
removeElement(k), we search 2 9
for key k >
 Assume key k is in the tree, 1 4 v 8
w
and let let v be the node 5
storing k
 If node v has a leaf child w, we
remove v and w from the tree
with operation 6
removeAboveExternal(w)
2 9
 Example: remove 4
1 5 8

Semester I (2014) 25

3. Binary Search Trees


Deletion (cont.)
1
 We consider the case where the v
3
key k to be removed is stored at
a node v whose children are 2 8
both internal 6 9
 we find the internal node w w
that follows v in an inorder 5
traversal z
 we copy key(w) into node v
 we remove node w and its left 1
child z (which must be a leaf) v
by means of operation 5
removeAboveExternal(z) 2 8
 Example: remove 3 6 9

Semester I (2014) 26

13
4/9/2021

3. Binary Search Trees


Performance
 Consider a dictionary
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
O(h) time
 The height h is O(n) in
the worst case and O(log
n) in the best case

Semester I (2014) 27

3. Binary Search Tree


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
 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)
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l m h
0 1 3 4 5 7 8 9 11 14 16 18 19
l=m =h

Semester I (2014) 28

14
4/9/2021

3. Binary Search Tree


Class Assignment
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

4. AVL Tree
AVL Tree Definition

 AVL trees are 4


balanced. 44
2 3
 An AVL Tree is a 17 78
binary search tree 1 2 1
32 50 88
such that for every
1 1
internal node v of T, 48 62
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:

Semester I (2014) 30

15
4/9/2021

4. AVL Tree n(2) 3

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

before insertion after insertion

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

case 1: single rotation


(a left rotation about a) T0 T1 T2 T3 T0 T1 T2 T3

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:

a=z single rotation b=y


b=y a=z c=x
c=x

T0 T3
T1 T3 T0 T1 T2
T2

c=z single rotation b=y


b=y a=x c=z
a=x

T3
T0 T2
TT03
T1
TT21 T1
T2 TT30
Semester I (2014) 35

4. AVL Tree
Restructuring (as Double Rotations)
 double rotations:

a=z double rotation b=x


c=y a=z c=y
b=x

T0 T2
T2 T3 T0 T1 T3
T1

c=z double rotation b=x


a=y a=y c=z
b=x

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

before deletion of 32 after deletion

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

 Balance the binary search tree below


1
5
2 8

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

You might also like