Академический Документы
Профессиональный Документы
Культура Документы
◦ 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
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.
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
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
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
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?
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
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:
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
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 >
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.
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
48 62 48 62
54
44 44
17 78 17 78
48 62 48 62
+2
54
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
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
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
44 44
17 78 17 78
82 90 82 90
44 44
17 78 17 78
44 44 44
R
17 78 17 78 17 78
R
32 50 88 50 88 50 88
82 90 82 90 82 90
44 44
78
17 78 17 78
44 88
32 50 88 50 88
17
50 82 90
82 90 82 90
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.