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

ECE391 Ch8 Trees and Search Trees

1. The document discusses trees and binary trees. It defines trees as hierarchical structures with nodes connected by parent-child relationships. 2. Binary trees are defined as trees where each internal node has two children. Binary trees can be used to represent arithmetic expressions and decision processes. 3. The properties of binary trees are discussed, including the relationships between the number of nodes, external nodes, internal nodes, height, and other metrics. Binary search trees are a type of binary tree that allows for efficient search operations.

Uploaded by

Thành Công Thi
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)
46 views21 pages

ECE391 Ch8 Trees and Search Trees

1. The document discusses trees and binary trees. It defines trees as hierarchical structures with nodes connected by parent-child relationships. 2. Binary trees are defined as trees where each internal node has two children. Binary trees can be used to represent arithmetic expressions and decision processes. 3. The properties of binary trees are discussed, including the relationships between the number of nodes, external nodes, internal nodes, height, and other metrics. Binary search trees are a type of binary tree that allows for efficient search operations.

Uploaded by

Thành Công Thi
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

9/4/

Computer System Engineering


Chapter 8:
Trees and Search Trees
1. Tree ADT
2. Binary Trees
3. Binary Search Trees
4. AVL 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

⚫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

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

⚫a tree whose root has an ordered


pair of children, each of which is a
binary tree H I

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

Want a fast meal?

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

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

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

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

22
9/4/

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
return a null position < 6

⚫Example: find(4) 2 9
>
1 4 = 8

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

24
9/4/

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
and let let v be the node w
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

25

3. Binary Search Trees


Deletion (cont.)
1
⚫We consider the case where v
3
the key k to be removed is
stored at a node v whose 2 8
children are 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

26
9/4/

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

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)
1 1 1 1 1
0 1 3 4 5 7 8 9
1 4 6 8 9
l m h
1 1 1 1 1
0 1 3 4 5 7 8 9
1 4 6 8 9
l m h
1 1 1 1 1
0 1 3 4 5 7 8 9
1 4 6 8 9
l m h
1 1 1 1 1
0 1 3 4 5 7 8 9
1 4 6 8 9
l=m
=h
28
9/4/

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.
2. Write C++ codes to perform the insertion function
to the binary search tree.

29

4. AVL Tree
AVL Tree Definition

⚫AVL trees are


balanced.
⚫ An AVL Tree is a
binary search tree
such that for every
internal node v of T,
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:

30
9/4/

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)
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

before insertion after insertion

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

case 1: single rotation


(a left rotation about a) T T T T T T T T
0 1 2 3 0 1 2 3

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

before deletion of 32 after deletion

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

⚫Balance the binary search tree below


1
5
2 8

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy