Вы находитесь на странице: 1из 89

Binary Trees

TAN TIEN PING


Tree
We have already look at structure such as list,
stack, array, which arrange object in linear
relation.
In computer science, a tree is an abstract
model of a hierarchical structure.
The term ”tree” comes from the family tree.
A tree consists of nodes with a parent-child
relation.
Each node can only have one parent node,
except root.
Tree
Trees are important ADT for storing
certain type of data. Computers”R”Us

◦ Organization charts
◦ File systems
Sales Manufacturing R&D
◦ Programming environments
◦ Decision tree
◦ Merge sort US International Laptops Desktops
◦ Binary search
◦ Parse tree
◦ Europe Asia Canada
Weighted tree
◦ …
a:35 c:22

b:15 d:12 e:10 f:6

Prefix tree (Huffman coding)

Decision tree (classification algorithms)

E
E +E
id E * E
id id
Probabilistic decision tree Parse tree by a compiler
Tree
Trees can be group based on the number of children a node has
◦ Binary tree (2 children)
◦ Binary search tree/ sorted binary tree
◦ AVL tree
◦ Heap
◦ Multiway tree (any number of children)
◦ Multiway search tree
◦ m-way tree
◦ B-tree
◦ 2-4 tree
Terminologies of a Tree
Three types of node in a tree
◦ Root: node without parent (A) A
◦ Non-terminal/Internal node: node with at least one child (A, B,
C, F)
◦ Leaf/Terminal/External node (a.k.a. leaf ): node without B C D
children (E, I, J, K, G, H, D)
Edge: connect between one node to another E F G H
Ancestors of a node:
◦ parent, grandparent, grand-grandparent. subtree
I J K
Depth of a node: number of edges from the node to the root.
Level of a node: depth + 1
Terminologies of a Tree
Height of a tree: maximum depth of any node (3)
Descendant of a node:
◦ child, grandchild, grand-grandchild, etc.

Subtree: part of a tree consisting of a node and its


descendants
Ordered tree: a tree is ordered if the children nodes
are arranged in a meaningful order.
Tree Abstract Data Type
Common functions for a tree
◦ getElement(): returns the value store in the node
◦ root(): returns the root of a node
◦ setParent(n): set the parent node for a node
◦ setChild(n): add a node as the child node
◦ parent(): returns the parent of a node
◦ children(): returns the children nodes
3

5 7
Tree Abstract Data Type
The basic building block for a tree is a “tree node”.
There are 3 elementary/necessary objects required to build a tree node.
◦ An element/value.
◦ A link to the parent. So that we can get the parent of a node.
◦ A link to the children. So that we can get the children of a node.

A node normally stores values. However, sometimes some of the nodes can be empty depending
on the problem. E.g. the non-terminal nodes of the prefix tree is empty.
A link to the parent/child is often just a reference to a node. But sometimes, a link is represented
using an object. E.g. weighted tree.
Our following discussion assume the link to the parent/node is just a simple reference/pointer to
the next node.
Tree Abstract Data Type
A node is represented by an object storing 
◦ Element/ value
◦ A link to Parent node B
◦ A link to a sequence of children nodes
 
Node objects implement the Position ADT
A D F
B
a tree
node
A D F

 
C E
C E
Tree
Important operations for a tree
◦ Traversing a tree: visit all the content of a tree in a particular order. e.g. left to
right/top to bottom for an unsorted tree.
◦ E.g. If you want to list out the all files in a folder.
◦ Searching a node in a tree.
◦ Search in a particular order. Different from traversal, here you stop when
you found what you looking for.
◦ Search by matching.
◦ e.g. binary search tree, decision tree (see right figure)
◦ Building a tree
◦ Insert a node
◦ Delete a node

Ref: http://visual.ly/should-i-take-nap-decision-tree-0
Traversal
A traversal visits the nodes of a tree in a systematic manner
To traverse a tree, we need to give the reference of the root of the tree to the traversing
algorithm.
The algorithm will then traverse from to root to
Preorder Traversal
In a preorder traversal, a node is visited before its descendants Algorithm preOrder(v)
visit(v)
Application: print a structured document
for each child w of v
Order: VLR. Consider V as a node, L and R as links preOrder (w)

1
Make Money Fast!

2 5 9
Reference
1. Motivations 2. Methods
s
6 7 8
3 4 2.1 2.2 2.3
1.1 1.2 Note: the number shows the
Stock Ponzi Bank
Greed Avidity
Robbery
order the node is visited.
Fraud Scheme
Postorder Traversal
In a postorder traversal, a node is visited after its descendants
Application: compute space used by files in a directory and its subdirectories
Order: LRV Algorithm postOrder(v)
for each child w of v
postOrder (w)
9 visit()
cs16/

8
3 7 todo.tx
programs
homeworks/ t
/
1K
1 2 4 5 6
h1c.do h1nc.do DDR.jav Stocks.jav Robot.jav
Note: the number shows the
c c a a a
3K 2K 10K 25K 20K
order the node is visited.
Inorder Traversal
In an inorder traversal a node is visited after its left subtree and before its right subtree
Applicable to binary tree only. Algorithm inOrder(v)
Application: draw a binary tree if hasLeft (v)
◦ x(v) = inorder rank of v inOrder (left (v))
visit(v)
◦ y(v) = depth of v
if hasRight (v)
6 inOrder (right (v))
Order: LVR
2 8

1 4 7 9

3 5 Note: the number shows the


order the node is visited.
Breadth-First Traversal
Breadth-first traversal is a tree traversal algorithm that normally starts at the tree root (or some
arbitrary node) and explores the neighbor nodes first, before moving to the next level neighbors.

public void breadthFirst() {


Node<T> p = root;
1 Queue<Node<T>> queue
= new Queue<Node<T>>();
if (p != null) {
2 3 queue.enqueue(p);
while (!queue.isEmpty()) {
p = queue.dequeue();
4 5 6 7 visit(p);
while(p.hasChild()){
if (p.left != null)
8 9 }
queue.enqueue(p.getChild());
}
}
Tree Search: Depth-First Search
An unordered tree is a tree where the elements are not arranged in a specific order. e.g.
directory of a file.
2 ways of searching for a node in a unordered tree
◦ Depth-first search
◦ Breath-first search

Depth-first search (DFS) starts at the root and explores as far as possible along each branch
before backtracking.
Depth-first search can be implemented using preorder, postorder or inorder traversal. It can also
be implemented either from left to right or right to left.
The only difference between search and traversal is the searching halt after the element is found
in search, while traversal will go through all the nodes.
Tree Search: Depth-First Search
Three tasks:
V ‑ visiting a node
L ‑ traversing the left subtree
R ‑ traversing the right subtree

The three tasks can be executed in 3! = 6 different orders:


left to right to
right left
preorder VLR VRL
inorder LVR RVL
postorder LRV RLV
Tree Search: Breadth-First
Search
Breadth-first search is a tree search algorithm that normally starts at the tree root (or some
arbitrary node) and explores the neighbor nodes first, before moving to the next level neighbors.

public void breadthFirst() {


Node<T> p = root;
1 Queue<Node<T>> queue
= new Queue<Node<T>>();
if (p != null) {
2 3 queue.enqueue(p);
while (!queue.isEmpty()) {
p = queue.dequeue();
4 5 6 7 visit(p);
while(p.hasChild()){
if (p.left != null)
8 9 }
queue.enqueue(p.getChild());
}
}
Binary Tree

Ref: http://personal.denison.edu/~bressoud/cs110-f12/firstlabs/lab08_f12.html
Binary Tree
A binary tree is a tree with the following properties:
◦ Every node has at most 2 children.
A
◦ A child node is either a left child or right child.
◦ Each internal node has at most two children.
◦ A left child precede a right child in the order of children of a node. B C

Alternative recursive definition: a binary tree is either


◦ a tree can be empty. D E F G
◦ a non-empty tree consists of a root node, which stores an element and 2
binary trees, which are the left subtree and right subtree.
H I
Binary Tree
A binary tree is composed of zero or more nodes
Each node contains:
◦ An element/value
◦ A reference or pointer to a left child (may be null), and
◦ A reference or pointer to a right child (may be null)

A binary tree may be empty (contain no nodes)


If not empty, a binary tree has a root node
◦ Every node in the binary tree is reachable from the root node by a unique path

A node without a child is called a leaf.


Binary tree can be implemented by referenced/pointer or array, just like Queue/Stack can
be implemented either by referenced/array.
Binary Tree
A binary tree with nodes that has zero/2 child nodes is
called a full/proper binary tree.

Applications:
◦ arithmetic expressions
◦ decision tree for classification.
◦ searching
Binary Tree
A complete binary tree is a binary tree where every level, except possibly the last, is completely
filled, and all nodes are as far left as possible.

Ref: https://stackoverflow.com/questions/12359660/difference-between-complete-binary-tree-strict-binary-
tree-full-binary-tre
root

nonterminal
node parent

right
child
leaf left child
(terminal node)
Is Binary Tree as Powerful as a
Multiway/branch tree?
In another word, is limiting a tree to only two branches, limit what it can do?
◦ No.

Any information that can be represented using multibranch tree can be represented using binary tree and vice versa.
Sunny
?

Y N
High? Overca
st?

Y N
Play? Rain?

Is this the only possible


binary tree?
Properties of a Binary Tree
Data Structure of a Binary Tree:
Reference-based
A node is represented by an object storing 
◦ Element
◦ Parent node
B
◦ Left child node
◦ Right child node  
Node objects implement the Position ADT
B
A D

A D    

C E C E
Data Structure of a Linked
Binary Tree
public class BTNode<T> {
protected T el; //element or value
protected BTNode<T> left, right; //node left and right

public BTNode(T element){


el = element;
}

public void setLeft(BTNode<T> node){


left = node;
}

public void setRight(BTNode<T> node){


right = node;
}

public BTNode<T> getRight(){


return right;
}
...
}
Binary Tree Abstract Data Type
Common functions for a tree
◦ getElement(): returns the value store in the node
◦ root(): returns the root of a node
◦ setParent(n): set the parent node for a node
◦ setLeft(n): set a node as the left child node
◦ setRight(n): set a node as the right child node
◦ getParent(): returns the parent of a node
◦ getLeft(): returns the left child node
◦ getRight(): returns the right child node
Binary Tree: More
A binary tree can be defined in a few ways. The edge is predefined with a particular value, and
this value may not be ”mention” explicitly. Each node can be viewed as a question.
General convention used for binary tree:
◦ Left edge encodes: <, true, 0
◦ Right edge encodes: >, false, 1
100
0 1

0
84 16
1 0

6 6 ? 50 34 16
< >= Less than
1 0 1 0 1
0
2 9 Yes No
< >= < Less than 2 ? Less than 9 ? a:35 b:15 c:22 d:12 e:10 f:6
1 4 8 1 4 8 Binary prefix tree in Huffman encoding
Performance of a Linked-
Structure Implementation of
Binary Tree
Data Structure of an Array
Unordered Binary Tree
Nodes of a tree can also be stored in an array. 1
A
Node v is stored at A[rank(v)]
rank(root) = 1
if node is the left child of parent(node): 2 3
B D
◦ rank(node) = 2 * rank(parent(node))
◦ e.g. “B” = 2 * 1 = 2
4 5 6 7
if node is the right child of parent(node)
E F C J
◦ rank(node) = 2* rank(parent(node)) + 1
◦ e.g. “D” = 2 . 1 + 1 = 3

A B D … G H … 10 11
G H
0 1 2 3 10 11
Data Structure of an Array
Unordered Binary Tree
Things for you to think about:

How to find the parent of a node? What is the time complexity?

What is the benefit of array implementation of tree?

What is the disadvantages of array implementation of tree?


Balance Binary Tree
A binary tree is balanced if every level above the lowest is “full” (contains 2 n nodes)
In many applications, a reasonably balanced binary tree is desirable. Why? Complexity of the
operations such as insertion, deletion, search will be O(n) if the tree is skew, instead of O(log n).
27
20
6
12 24 2 trees contains the same
values, but different structure. 4 26

4 13 21 27 1 21
If you try searching for a value,
1 6 26 you will notice the average 12 24
operation used to find an item is
A balanced binary tree less on the tree to the left. 13 20

An unbalanced binary tree


Creating Balance Linked List
Binary Tree from a Sorted Array
Given a list of values, there are many ways to create different structure of linked list binary trees.
However, balanced binary tree is the structure preferred.
Top down approach to create a balance binary tree. Steps (in general):
i. Determine the middle element (median), if n is even then middle= n/2, else middle=n/2+1. Set the
element as root
ii. Recursively do the same for left half and right half
i. Determine the middle left subarray if the subarray is not empty, insert it as the left child,.
ii. Determine the middle right subarray if the subarray is not empty, insert it as the right child.

Notice that the order of insertion is just like “preorder traversal”.


Creating Balance Linked List
Binary Tree from Sorted Array
Stream of data : 5 1 9 8 7 0 2 3 4 6
Array of sorted data : 0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9 4

0 1 2 3 4 5 6 7 8 9
1 7

0 1 2 3 4 5 6 7 8 9
0 2 5 8

3 6 9
Binary Search Tree
Binary Search Tree
Binary search tree is a type of binary tree where the node is sorted, thus it is also known as ordered/ sorted
binary tree.
It allows the fast search/lookup of items.
A binary search tree is a binary tree storing keys (or key-value entries) at its 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; w is in the right subtree of v. We
have
key(u)  key(v)  key(w)
◦ In another word, node at the left edge is less than the current node; node at the right edge more than the
current node.
◦ The key is used for sorting, which is normally is of less interest for users. But each key is normally link to
some values that of importance. Analogy: you tag your drawers with different id, the id help you to know
what thing store at what place. You use the id to access to the things that you want! The things store in the
drawers are important!
Binary Search Tree
An inorder traversal of a binary search trees visits the keys in increasing order.

2 9

1 4 8
Binary Search Tree (Array-based):
Search Example
Look at array location (lo + hi)/2
Searching for 5:
(0+6)/2 = 3
Using a binary
hi = 2;
(0 + 2)/2 = 1 lo = 2; search tree
(2+2)/2=2
7

3 13
0 1 2 3 4 5 6
2 3 5 7 11 13 17 2 5 11 17
Binary Search Tree (Linked List):
Search Algorithm

/*
/ Searching for the node that contains the el value
*/
search(BTNode parent, T el){
BTNode p = parent;
while (p != null){
if (p.getElement() == el)
return element in node p;
else if (p.getElement() < el)
p = p.getRight();
else p = p.getLeft(); }
return null; // el was not found;
}
Binary Search Tree (Reference-
based): Search Example
25 28

20 20

10 30 10 30

15 25 success 15 25

23 27 23 27
failure

26 26
Binary Search Tree: Insertion 6
<
To perform operation put(k, o), we search for key k
(using TreeSearch) 2 9
>
Assume k is not already in the tree, and let w be the 1 4 8
leaf reached by the search >

We insert k at node w and expand w into an internal w


node
6
Example: insert 5
2 9

1 4 8
w
5
Binary Search Tree: Inserting a
Node
bstInsert(BTNode root, BTNode newNode)
BTNode p = root;
BTNode prev = null;
while p != null // find a place for inserting new node;
prev = p;
if (p.getElement() < newNode.getElement())
p = p.getRight();
else p = p.getLeft();
if root == null // tree is empty;
root becomes a new node with el;
else if (prev.getElement() < newNode.getElement())
prev.setRight(newNode);
else
prev.setLeft(newNode);
Binary Search Tree: Inserting a
Node
20 30

null 20

25 10 15

20 20 20

30 30 10 30

25 25
Binary Search Tree: Inserting a
Node
27 23 26

20 20 20

10 30 10 30 10 30

15 25 15 25 15 25

27 23 27
Binary Search Tree: Deleting a
Node
Deleting a node in a tree, there are 3 possible scenarios.
First scenario, the node is a leaf.
◦ Simply delete the node.
◦ Example: Node 4 (leaf)

6 6

2 9 2 9

1 4 8 1 8

7 7
Binary Search Tree: Deleting a
Node
Second scenario, the node has only one subtree.
◦ Simply delete the node, relink the other nodes.
◦ Example: Node 9

6 6

2 9 2 8

1 4 8 1 4 7

7
Binary Search Tree: Deleting a
Node
Third scenario, the node has two subtrees.
◦ Example: Node 6

How to do it?
◦ Idea: Just delete the element/value. Keep the structure. Copy other element from other node (e.g.
from the leaf or with one subtree) to replace it. Then delete the other node.
◦ So, if we were going to delete “6”, what value from other node we should replace with?
6

2 9

1 4 8

7
Binary Search Tree: Deleting a
Node
Third scenario, the node has two subtrees.
◦ Example: Node 6

How to do it?
◦ Idea: Just delete the element/value. Keep the structure. Copy other element from other node (e.g.
from the leaf or with one subtree) to replace it. Then delete the other node.
◦ So, if we were going to delete “6”, what value from other node we should replace with x?
◦ The value x that we choose, must be more than all the values in the left subtree.
x
◦ The value x that we choose, must be less than all the values in the right subtree.
2 9

1 4 8

7
Binary Search Tree: Deleting a
Node
Third scenario, the node has two subtrees.
◦ Example: Node 6

How to do it?
◦ Idea: Just delete the element/value. Keep the structure. Copy other element from other node (e.g.
from the leaf or with one subtree) to replace it. Then delete the other node.
◦ So, if we were going to delete “6”, what value from other node we should replace with x?
◦ The value x that we choose, must be more than all the values in the left subtree.
x
◦ The value x that we choose, must be less than all the values in the right subtree.
◦ So, the value we should choose in this case is “4” or “7” 2 9

1 4 8

7
Binary Search Tree: Deleting a
Node
Deleting node “6”
6 -

2 9 2 9

1 4 8 1 4 8

7 7

4 4

2 9 2 9

1 - 8 1 8

7 7
Binary Search Tree: Deleting a
Node
What if we need to delete node “6” for this tree?

4 9

2 8

1 3 7
Binary Search Tree: Time
Complexity
Insertion, deletion and search has an average time complexity of O(log n), where the binary tree
is nearly balance.
Insertion, deletion and search has an worst case time complexity of O(n). This happens when the
binary tree is nearly skew.

skew binary tree


AVL Tree
AVL Tree
AVL tree is a type of balance binary search tree proposed by Georgy Adelson-Velsky and Evgenii
Landis in 1962, where the heights of the subtrees differ at most one.
AVL tree is self-balancing to ensure the condition that the difference in heights of subtrees of
one is respected.
The advantage of an AVL tree is that it has average case and worst case complexity of O(log n)
compared to a binary search tree.

value = height right subtree –


height of left subtree

Examples of AVL tree.


AVL Tree: Balancing through
Rotation
Insertion and deletion is the same like binary search tree, but if after insertion/deletion the AVL
condition is not respected, self balancing is done.
Insertion and deletion is the same like binary
search tree, but if after insertion/deletion the AVL
condition is not respected, self balancing is done.

Moving bottom
internal node up
the ”ladder”

Ref: https://en.wikipedia.org/wiki/AVL_tree
Example 1: Inserting “54” to a tree.

44 44

17 78 17 78

So, which node to rotate so that it


32 50 88 32 50 88
maintain as an AVL tree?

48 62 48 62

54

Before insertion After insertion


Example 1: Inserting “54” to a tree.

44 44

17 78 17 78

Trim the right subtree


32 50 88 32 50 88

48 62 48 62

+2
54

Before insertion After insertion


Example 1: Inserting “54” to a tree.

44 44 44

17 78 17 78 17 78
L
32 50 88 32 50 88 32 50 88
R
48 62 48 62 48 62

54 54

Before insertion After insertion Change LR case to LL case


Example 1: Inserting “54” to a tree.

44 44 44

17 78 17 78 17 78
L
32 50 88 32 50 88 32 62 88
L
48 62 48 62 50

54 48 54

before insertion After insertion Change LR case to LL case. Balance it


subsequently.
Example 1: Inserting “54” to a tree.

44 44 44

17 78 17 78 17 62

32 50 88 32 50 88 32 50 78

48 54 88
48 62 48 62

54

before insertion After insertion Change LR case to LL case. Balance it


subsequently.
Example 2: Deleting “32” from a tree.

44 44

17 78 17 78

So, which node to rotate so that it


32 50 88 50 88
maintain as an AVL tree?

82 90 82 90

Before insertion After deletion


Example 2: Deleting “32” from a tree.

44 44

17 78 17 78

So, which node to rotate so that it


32 50 88 50 88
maintain as an AVL tree?
+2
82 90 82 90

Before insertion After deletion


Example 2: Deleting “32” from a tree.

44 44 44
R

17 78 17 78 17 78
R

32 50 88 50 88 50 88

82 90 82 90 82 90

So, which node to rotate so that it


maintain as an AVL tree?
Before insertion After deletion
Example 2: Deleting “32” from a tree.

44 44

78
17 78 17 78

44 88
32 50 88 50 88

17
50 82 90
82 90 82 90

So, which node to rotate so that it


maintain as an AVL tree?
Before insertion After deletion
Heap
Heap
Heap is a binary tree that has the following
property
◦ The key of the child node is greater
than/equal to the key of its parent node.
◦ This mean that the root has the smallest
key
◦ The tree must be complete.
◦ Each level from 0 to h-1 has the maximum
number of nodes possible.
◦ Level h, contains the rest of the nodes.
Occupying the left most first.
Heap can be used to implement priority
queue.
Heap
Recall the complexity of the following operations of priority queue using different data structure.

Method Unsorted List Sorted List Heap


size O(1) O(1) O(1)
isEmpty O(1) O(1) O(1)
insert O(1) O(n) O(log n)
min O(n) O(1) O(1)
removeMin O(n) O(1) O(1)
Heap: Insertion
Insertion of a new entry to a heap
is known as up-heap bubbling.
◦ Insert the node at the bottom
level, the subsequent left
position.
◦ Swap the key with the parent’s
key until the heap order
property is restored
Inserting a new entry with key “2”
into a heap.
The complexity is O(log n).
Heap: Insertion
Heap: Deletion (Minimum Key)
When heap is used to implement priority
queue, deletion is applied at the root to
remove the node with the highest priority.
The process to delete a node is known as
down-heap bubbling.
◦ Delete the root node.
◦ Move the right most node at the
bottom level to to root.
◦ Repeatedly swap the key with one of
the children with the smallest key.
Repeat until heap order property is
restored.
The complexity is O(log n).
Heap: Deletion (Minimum Key)
Heap: Implemented Using Array
Heap can be implemented using array.
How to know the parent of a node from
array?
◦ If p is the root, then f(p)=0.
◦ If p is the left child of position q, then
f(p) = 2f(q)+1.
◦ If p is the right child of position q, then
f(p) = 2f(q)+2.

f(q) = 4
The child nodes of f(q)=4: f(p) = 2(4) + 1 = 9 and,
f(p) = 2(4) + 2 = 10
Heap: Bottom-Up Heap
Construction
If we construct the heap by inserting one by one, the complexity will be O(n log n).
In some situation where, we can obtain all the entries (key) at once, we can use a more efficient approach to
construct the heap with complexity at O(n).
The approach is known as bottom-up heap construction.
i. Based on the number of entries/keys, determine the height of the heap tree, and the number of nodes at each
level.
◦ 2-3 nodes: height= 1
◦ 4-7 nodes: height-2
◦…
ii. Simply insert the entry based on the key into the nodes at the bottom level.
iii. Simply fill up entry based on the keys into the nodes at 1 level above, and perform down-heap bubbling if
required.
iv. Repeat step iii, until all keys are filled up.
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “7, Molly”, “11, Ian”.
Step 1:
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “7, Molly”, “11, Ian”.
Step 2:

15 9 18 4
Note: The value of
entry is not shown.
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “7, Molly”, “11, Ian”.
Step 3:

2 17 2 4 20 5
20 5

down-heap bubbling 15 9 18 17
15 9 18 4
Note: The value of
entry is not shown.
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “3, Molly”, “11, Ian”.
Step 4:

10 2 5
7

2 17 10 4 20 7
20 5

15 9 18 4 15 9 18 17
Note: The value of
entry is not shown.
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “3, Molly”, “11, Ian”.
Step 4:

2 5

9 4 20 7

15 10 18 17
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “3, Molly”, “11, Ian”.
Step 4:
11 2 possibilities, select the 2
smallest

2 11 5
5

9 4 9 4 20 7
20 7

15 10 18 17 15 10 18 17
Note: The value of
entry is not shown.
Heap: Bottom-Up Heap
Construction
Example: “15, Jane”, “9, Alex”, “18, June”, “4, Bell”, “2, Alan”, “17, John”, “ 20, Owen”, “5,
Sharon”, “10, Elly”, “3, Molly”, “11, Ian”.
Step 4:
5

4 5

9 11 20 7

15 10 18 17
Heap: Bottom-Up Heap
Construction Analysis
The computational cost is mostly in down-heap step.
Worst case, down-heap step movement equal to the height of the subtree of that node.
◦ Leaf nodes do not move.
◦ The sum of the total number of down-heap movement do not exceed the total number of edges of the
tree.
◦ Time complexity of bottom-up heap construction is O(n).
Heap Sort
Heap sort is done through 2 steps
◦ Step 1: Insert the values into the heap.
◦ Step 2: Delete the root of a heap, and perform down-heap
bubbling.

Heap can be implemented using array.


Heap sort is an in place algorithm because it involves
only small amount of memory.
Time complexity: step 1 takes O(n), and step 2 takes O(n
log n). So, the algorithm takes O(n log n)

Вам также может понравиться