You are on page 1of 61

Chapter 5

Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary trees
•Heaps

J.-J. Chen, EE NTUST 2023/5/3 Tree I-1


Genealogical Charts

Root

Dusty

Honey Bear Brandy

Brunhilde Terry Coyote Nugget

Gill Tansey Tweed Zoe Crocus Primrose Nous Belle

leaf

J.-J. Chen, EE NTUST 2023/5/3 Tree I-2


Definition of Tree
• A tree is a finite set of one or more nodes such
that:
– There is a specially designated node called the root.
– The remaining nodes are partitioned into n>=0 disjoint
sets T1, ..., Tn, where each of these sets is a tree.
– We call T1, ..., Tn the subtrees of the root.

J.-J. Chen, EE NTUST 2023/5/3 Tree I-3


Level and Depth
•Each node (13 nodes here)
- can have many children nodes
- but can only have one parent node
• Degree: the maximum number of children nodes
Level
leaf (terminal)
nonterminal 1
parent 3 A
children
sibling 2
degree of a tree (3) 2 B 1 C 3 D
ancestor
level of a node E F G H I J 3
height of a tree (4) 2 0 0 1 0 0

0 K 0 L
0
M 4
J.-J. Chen, EE NTUST 2023/5/3 Tree I-4
Terminology
• Degree
– The degree of a node is the number of subtrees of the node
– The degree of A is 3; the degree of C is 1.
• Leaf node
– The node with degree 0 is a leaf or terminal node.
• Parent
– A node that has subtrees is the parent of the
roots of the subtrees.
• Children
– The roots of these subtrees are the children of
the node.
• Siblings
– Children of the same parent are siblings.
• Ancestors
– The ancestors of a node are all the nodes along the path from
the root to the node.

J.-J. Chen, EE NTUST 2023/5/3 Tree I-5


Representation of Trees
A

B C D

E F G H I J

K L M

• List Representation
– ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
– The root comes first, followed by a list of sub-trees

DATA CHILD1 CHILD2 ……. CHILDK

Possible node structure for a tree of degree k

J.-J. Chen, EE NTUST 2023/5/3 Tree I-6


Lemma 5.1
• Lemma
–If T is a k-ary tree (i.e., a tree of degree k) with n nodes,
each having a fixed size as shown in the previous slide,
then n(k-1) +1 of the nk child fields are 0, n1

• Proof
– The number of non-zero child fields in an n-node tree is exactly
n-1
– The total number of child fields in a k-ary tree with n nodes is nk
– Hence, the number of 0 fields is nk-(n-1) = n(k-1) +1

J.-J. Chen, EE NTUST 2023/5/3 Tree I-7


Left Child - Right Sibling
A
data
B C D
left child right sibling

E F G H I J
A

K L M
B C D

E F G H I J

K L M

J.-J. Chen, EE NTUST 2023/5/3 Tree I-8


Represent as a Degree-2 Tree

A B

D E C
B C

G D
K F
E F G H I J

H
L
K L M
M I

J.-J. Chen, EE NTUST 2023/5/3 Tree I-9


Chapter 5
Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary trees
•Heaps

J.-J. Chen, EE NTUST 2023/5/3 Tree I-10


Binary Trees (BT)
• Definition
– A binary tree is a finite set of nodes that is either empty
or consists of a root and two disjoint binary trees called
the left subtree and the right subtree.

• Universal Representation
– Any tree can be transformed into binary tree by left
child-right sibling representation

• Distinguished Subtrees
– The left subtree and the right subtree are distinguished.

J.-J. Chen, EE NTUST 2023/5/3 Tree I-11


Left child-right child tree representation
A

B C D
A
E F G H I J B

K L M E C

G D
K F

H
L

M I

J
J.-J. Chen, EE NTUST 2023/5/3 Tree I-12
ADT of BT

J.-J. Chen, EE NTUST 2023/5/3 Tree I-13


Skewed & Complete BTs
Skewed Binary Tree Complete Binary Tree

1 A A A

2
B B B C

3 C F G
D E

4 D
H I

5 E

J.-J. Chen, EE NTUST 2023/5/3 Tree I-14


Properties of BT
Maximum Number of Nodes
• Lemma 5.2
– The maximum number of nodes on level i of a binary
tree is 2i-1, i≧1.
– The maximum nubmer of nodes in a binary tree
of depth k is 2k-1, k≧1.
• Proof by Induction
– Induction base: root is the only node on level i=1, 2i-1 = 20 = 1
– Induction hypothesis: max. no of nodes on level i-1, is 2i-2
– induction step: max. no of nodes on level i is 2i-1
k
2 = 2 −1
i −1 k

i =1

J.-J. Chen, EE NTUST 2023/5/3 Tree I-15


Properties of BT
Relation between leaf & degree-2 nodes
• Lemma 5.3
– For any nonempty binary tree, T, if n0 is the number of
leaf nodes and n2 the number of degree-2 nodes, then
n0 = n2 + 1
• Proof
– Let n be the total number of nodes
– Let n1 be the number of nodes of degree 1
– We have n = n0 + n1 + n2
– If B is the number of branches, B+1=n, B=n1+2n2
– Hence we obtain n= B + 1 = n1 + 2n2 +1,
➔ n1+2n2+1= n0+n1+n2 ➔ n0=n2+1

J.-J. Chen, EE NTUST 2023/5/3 Tree I-16


Full BT VS Complete BT
• A full binary tree of depth k
–is a binary tree of depth k having 2k -1 nodes, k>=0.
• A binary tree with n nodes and depth k
–is complete iff its nodes correspond to the nodes
numbered from 1 to n in the full binary tree of depth k.
Numbered from top to bottom, left to right

A A

B C B C

D E F G D E F G

H H I J K L M N O
I
Complete binary tree Full binary tree of depth 4
J.-J. Chen, EE NTUST 2023/5/3 Tree I-17
Full BT with Sequential Node Number
1

Let n be the number of nodes.


The depth will be log2(n+1) 2 3

4 5 6 7

8 9 10 11 12 13 14 15
• Lemma 5.4
– If a complete binary tree with n nodes is represented
sequentially, then for any node with index, i, 1 i  n, we have
1. Parent(i) is at i/2 if i1
2. LeftChild(i) is at 2i if 2i  n.
If 2i  n, then i has no right child
3. RightChild(i) is at 2i if 2i+1  n.
If 2i +1  n, then i has no right child
J.-J. Chen, EE NTUST 2023/5/3 Tree I-18
Sequential Representation
(1) Good for complete BT but waste space for other BT
(2) insertion/deletion problem

[1] A
[2] B
A [3] -- [1] A
A
[4] C [2] B
B [5] -- [3] C
[6] -- B C [4] D
C [5] E
[7] --
[8] D [6] F
D D E F G
[9] --c [7] G
.. .. [8] H
E
H I [9] I
[16] E

J.-J. Chen, EE NTUST 2023/5/3 Tree I-19


Linked Representation

LeftChild data RightChild

data

left_child right_child

J.-J. Chen, EE NTUST 2023/5/3 Tree I-20


Linked Representation of a BT
A
A
B C
B
D E F G
C root
D A 0 H I
root
B 0
E A

C 0
B C

D 0 D 0 E 0 0 F 0 0 G 0

C 0 0 H 0 0 I 0

J.-J. Chen, EE NTUST 2023/5/3 Tree I-21


Chapter 5
Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary trees
•Heaps

J.-J. Chen, EE NTUST 2023/5/3 Tree I-22


Binary Tree Traversals
• Let L, V, and R stand for
– L: moving left
– V: visiting the node
– R: moving right
• There are six possible combinations of
traversal
– LVR, LRV, VLR, VRL, RVL, RLV
• Adopt convention that we traverse left before
right, only 3 traversals remain
– LVR, LRV, VLR
– inorder, postorder, preorder

J.-J. Chen, EE NTUST 2023/5/3 Tree I-23


Arithmetic Expression Using BT
•Inorder traversal
A/B*C*D+E
+ infix expression

* E •Preorder traversal
+**/ABCDE
* D prefix expression

/ C •Postorder traversal
AB/C*D*E+
A B postfix expression

•Level order traversal


+*E*D/CAB

J.-J. Chen, EE NTUST 2023/5/3 Tree I-24


Inorder Traversal of a BT

current

Left Right
subtree subtree

J.-J. Chen, EE NTUST 2023/5/3 Tree I-25


Trace of Inorder Traversal
Call of Value Action Call of Value Action
inorder in root inorder in root
Driver + 10 C
+ 1 * 11 0
1 2 * 10 C cout<<’C’
3 / 12 0
2 * E
4 A 1 * cout<<’*’
5 0 13 D
3 * D 4 A cout<<’A’ 14 0
6 0 13 D cout<<’D’
4 / 7 C 3 / cout<<’/’ 15 0
7 B Driver + cout<<’+’
A B 8 0 16 E
8 7 B cout<<’B’ 17 0
9 0 16 E cout<<’E’
2 * cout<<’*’ 18 0

J.-J. Chen, EE NTUST 2023/5/3 Tree I-26


Preorder traversal of a BT

current

Left Right
subtree subtree

J.-J. Chen, EE NTUST 2023/5/3 Tree I-27


Postorder Traversal of a B-Tree
void Tree::postorder()
// Drier calls workhorse for traversal of the entire tree
// The driver is declared as a public member function of Tree
{
preorder(root);
}
void Tree:postorder(TreeNode *CurrentNode)
// workshorse traverses the subtree rooted at the CurrentNode
// (which is a pointer to a node in a binary tree). The
// workhorse is declared as a private member function of a tree
{
if(CurrentNode) {
postorder(CurrentNode->LeftChild);
postorder(CurrentNode->RightChild); current
cout << CurrentNode->data;
}
}

Left Right
subtree subtree

J.-J. Chen, EE NTUST 2023/5/3 Tree I-28


Level-Order Traversal (1)
• Stack is required
– for the inorder, pre-order & post-order traversal
• Queue is required
– for the level-order Traversal
– The level-order traversal visits the root first, then the
root’s left-child, followed by the root’s right-child
+
Breadth-First Traversal (BFS)
* E

* D Level-order Traversal:

/ C +* E *D /CAB

A B

J.-J. Chen, EE NTUST 2023/5/3 Tree I-29


Level-Order Traversal (2)

+
Breadth-First Traversal (BFS)
* E

* D Level-order Traversal:

/ C +* E *D /CAB

A B

J.-J. Chen, EE NTUST 2023/5/3 Tree I-30


Chapter 5
Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary trees
•Heaps

J.-J. Chen, EE NTUST 2023/5/3 Tree I-31


Copying BT

J.-J. Chen, EE NTUST 2023/5/3 Tree I-32


BT equivalence

J.-J. Chen, EE NTUST 2023/5/3 Tree I-33


Propositional Calculus Expression
• Boolean Formula is constructed by
– A set of variables { x1, x2, x3,….}
– Operators such as (AND), (OR) , (NOT)
• Construction Rules
– A variable is an expression
– If x and y are expressions, then x, xy, xy are
expressions
– Parentheses can be used to alter the normal order of
evaluation ( >  >  )
– Example: E = x1  ( x2   x3)
• Evaluation
– When x1 =FALSE, x2 =TRUE, x3=FALSE → E = TRUE

J.-J. Chen, EE NTUST 2023/5/3 Tree I-34


Satisfiability Problem
• Satisfiability problem:
– For formulas of propositional calculus, is there an
assignment to make an expression true ?

(t,t,t) (x1  ¬x2)  (¬ x1  x3)  ¬x3


(t,t,f)
(t,f,t) 
(t,f,f)
 
(f,t,t)
(f,t,f)  X3

(f,f,t)
(f,f,f) X1   X3

2n possible combinations X2 X1
for n variables
J.-J. Chen, EE NTUST 2023/5/3 Tree I-35
1st version of Satisfiability Problem
enum Boolean{ FALSE,TRUE };
enum TypeOfData { Not, And, Or, True, False};
class SatTree; // forward declaration
class SatNode { class SatTree{
friend class SatTree; public:
private: void PostOrderEval();
SatNode *LeftChild; void rootvalue() { cout << root->value;};
TypesOfData data; private:
Boolean value; SatNode *root;
SatNode *RightChild; void PostOrderEval(SatNode *);
}; };
for all 2n possible truth value combinations for the n variables
{ LeftChild data value RightChild
generate the next combination; 0/1
replace the variables by their values;
evaluate the formula by traversing the tree it points to in postorder
if(formula.rootvalue()) { cout << combination; return; }
}
cout << " no satisfiable combination";
J.-J. Chen, EE NTUST 2023/5/3 Tree I-36
Evaluating a Formula

J.-J. Chen, EE NTUST 2023/5/3 Tree I-37


Chapter 5
Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary Trees
•Heaps

J.-J. Chen, EE NTUST 2023/5/3 Tree I-38


Threaded Binary Tree
• Too many null pointers in current BT
representation
– n: number of nodes
– Number of non-null links: n-1
– Total links: 2n
– Null links: 2n-(n-1) = n +1
• Replace these null pointers with some useful
threads
– Left thread: inorder predecessor
– Right thread: inorder successor

J.-J. Chen, EE NTUST 2023/5/3 Tree I-39


A Threaded Binary Tree
left_thread left_child data right_child right_thread

TRUE ⚫ ⚫ FALSE

TRUE: thread FALSE: child


inorder
root A predecessor
dangling
B C

dangling D E F G

inorder traversal: inorder


H I H, D, I, B, E, A, F, C, G
successor
J.-J. Chen, EE NTUST 2023/5/3 Tree I-40
Memory Representation of Threaded Tree
A We must be able to distinguish between
threads and normal pointers
B C
root
D E F G
f -- f
H I

f A f

f B f f C f

f D f t E t t F t t G t

t H t t I t

J.-J. Chen, EE NTUST 2023/5/3 Tree I-41


The Class for a
Threaded BT

LeftThread LeftChild Data RightChild RightThread


TRUE • • FALSE

J.-J. Chen, EE NTUST 2023/5/3 Tree I-42


Finding the Inorder Successor

inorder traversal:
H, D, I, B, E, A, F, C, G

J.-J. Chen, EE NTUST 2023/5/3 Tree I-43


inorder traversal:
H, D, I, B, E, A, F, C, G
root

f f
TEMP 21 CURRENT1

TEMP 11

TEMP 12 CURRENT 2

J.-J. Chen, EE NTUST 2023/5/3 Tree I-44


Inserting a Node as the RightChild
in a Threaded Tree

1
3
2

3
2
1

J.-J. Chen, EE NTUST 2023/5/3 Tree I-45


Inserting Operation

1
2
3

3
2
1

J.-J. Chen, EE NTUST 2023/5/3 Tree I-46


Chapter 5
Tree(Part I)
•Introduction
•Binary Tree
•Binary Tree Traversal
•Additional Binary Tree operations
•Threaded Binary trees
•Heaps

J.-J. Chen, EE NTUST 2023/5/3 Tree I-47


Max Priority Queue
• Example1: For a service machine, users pay
1.Fixed amount per use but need different service time→
Construct a minimum priority queue with service time,
i.e., the user with smallest time is selected
2.Different amount per use but need the same service
time→ Construct a maximum priority queue with
payment, i.e., the user with maximum payment is
selected

Server $ $ $ $ $
machine
Required service time $ $ $ $ $
$ payment

J.-J. Chen, EE NTUST 2023/5/3 Tree I-48


• Example2: A large factory
– Event is said to occur when a machine completes the
job,
– The job is moved to the queue of the next machine
– A priority queue contains the finish time of all jobs that
are presently worked on
– To determine the occurrence of events, a min priority
queue can be used

Machine Machine

Job o Job p Job l Job m


Job n Job k

J.-J. Chen, EE NTUST 2023/5/3 Tree I-49


Heap
• Heap
– is frequently used to implement a priority queue
• Definition
– A max(min) tree is a tree in which the key value in each node
is no smaller (larger) than the key values in its children(if any)
– A max heap is a complete binary tree that is also a max tree
– A min heap is a complete binary tree that is also a min tree
[1] [1] [1]
14 9 30

[2] [3] [2] [3] [2]


12 7 6 3 25

[4] [5] [6] [4]


10 8 6 5

J.-J. Chen, EE NTUST 2023/5/3 Tree I-50


The Max Heap Class

J.-J. Chen, EE NTUST 2023/5/3 Tree I-51


Insertion into a Max Heap (1)
• Example
20 20 21

15 2 15 5 15 20

14 10 14 10 2 14 10 2

initial location of new node insert 5 into heap insert 21 into heap

J.-J. Chen, EE NTUST 2023/5/3 Tree I-52


Insertion into a Max Heap (2)
Original heap
20

15 2

14 10 x.key

insert 5 into heap insert 21 into heap

20 21

15 5 15 20

14 10 2 14 10 2

J.-J. Chen, EE NTUST 2023/5/3 Tree I-53


Deletion from a Max Heap (1)
remove
20 null
Delete the root Move 19 up
element
15 19 15 19

14 10 14 10

19 19

Move 10 to
vacancy
null 15 10
15

14 14 null
10

J.-J. Chen, EE NTUST 2023/5/3 Tree I-54


Deletion from a Max Heap (2)
20 20
remove Delete the 15 Move 14 up
element
15 19 null 19

14 10 14 10

20 20

Move 10 to
vacancy
19 14 19
14

null 10 10

J.-J. Chen, EE NTUST 2023/5/3 Tree I-55


22

Deletion from a Max Heap 19 21

18 17 14 10

15

i: vacant position
j: larger child

Height of a heap =  log2(n+1) 


Complexity = O(log n)

J.-J. Chen, EE NTUST 2023/5/3 Tree I-56


Min-Heap Delete Examples

Delete 15 Delete 50

J.-J. Chen, EE NTUST 2023/5/3 Tree I-57


Priority Queue
• Priority Queue
– Elements deleted is the one with the highest priority
– An element with arbitrary priority may be inserted
– This is called max priority queue
– Min priority queue is defined similarly

template <class Type>


class MaxPQ{
public:
virtual void insert(const Element<Type>&)=0;
virtual Element<Type>*DeleteMax(Element<Type>&)=0
};

J.-J. Chen, EE NTUST 2023/5/3 Tree I-58


Priority Queue Representation

Representation Insertion Deletion

Unordered O(1) O(n)


array
Unordered O(1) O(n)
linked list
Sorted O(n) O(1)
array
Sorted O(n) O(1)
linked list
Max heap O(log2n) O(log2n)

J.-J. Chen, EE NTUST 2023/5/3 Tree I-59


Homework
• Write an algorithm to insert into a min heap. Use the
notation of program 5.18. The complexity of your
algorithm should be O(log n). Show that this is the case.

J.-J. Chen, EE NTUST 2023/5/3 Tree I-60


Homework Chapter 5
part I
• P. 276. ex1
Write a C++ function to count the number of leaf nodes in a binary
tree, T. What is its computing time?
• P. 276. ex5
write a recursive algorithm for erasing all nodes in a binary
tree[Destructor]
• Page 281, Ex1
Write an algorithm that inserts a new node l as the left child of node
s in a threaded binary tree. The left subtree of s becomes the left
subtree of l .
• Page 291, Ex3
Write an algorithm to delete the smallest item from a min heap. Use
the notation of program 5.19. The complexity of your algorithm
should be O(log n). Show that this is the case.
• DeadLine: to be determined

J.-J. Chen, EE NTUST 2023/5/3 Tree I-61

You might also like