systems programming - uc3m · 2014-05-07 · trees systems programming julio villena román...
TRANSCRIPT
![Page 1: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/1.jpg)
Trees
Systems Programming
Julio Villena Román (LECTURER)
CONTENTS ARE MOSTLY BASED ON THE WORK BY:
Carlos Delgado Kloos, M.Carmen Fernández Panadero
and Raquel M.Crespo García
![Page 2: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/2.jpg)
Contents
• Concept
– Non recursive definition
– Recursive definition
– Examples
– Terminology
– Key concepts
– Properties
• Implementation
– Sequence-based
– Linked structure
• Basic operations
• Traversals
• Particular cases
– Binary search trees
– Binary heaps
1
![Page 3: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/3.jpg)
Quote
“The structure of concepts is formally called a hierarchy and since ancient times has been a basic structure for all Western knowledge. Kingdoms, empires, churches, armies have all been structured into hierarchies. Tables of contents of reference material are so structured, mechanical assemblies, computer software, all scientific and technical knowledge is so structured...”
Robert M. Pirsig:
Zen and the Art of Motorcycle Maintenance
2
![Page 4: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/4.jpg)
Tress Concept and characteristics
A tree is a non-linear
data structure that
stores the elements
hyerarchically
(Generalization of lists)
3
![Page 5: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/5.jpg)
• File system
• Structure of a book or a document
• Arithmetic expressions
Examples
4
![Page 6: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/6.jpg)
Tress Concept and characteristics
• Trees can be defined in two ways:
– Non-recursive definition
– Recursive definition
a
e
i j
k
b
f g
T1 T2
Recursive definition 5
a
e
i j
k
b
f g
Non-recursive definition
![Page 7: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/7.jpg)
Non-recursive definition
• A tree consists of a set of nodes and a set of edges, such that:
– There is a special node called root
– For each node c, except for the root, there is one edge from another node p (p is parent of c, c is one of the children of p)
– For each node there is a unique path (sequence of edges) from the root
– Nodes without children are called leaves
6
![Page 8: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/8.jpg)
Example
p
c
Root (no parent)
Hojas
(sin h
Leaves (no
children)
Hojas
(sin hijos) Leaves (no
children)
The parent of c
A child of p sibling
of c
7
![Page 9: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/9.jpg)
Recursive definition (1)
• A tree is
– A node
– or a node and subtrees connected to the
node by means of an edge to its root
Doesn't include
the empty tree
8
![Page 10: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/10.jpg)
Recursive definition (2)
• A tree is
– empty
– or a node and zero or more non-empty
subtrees connected to the node by means
of an edge to its root
9
![Page 11: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/11.jpg)
a
b c d e
f g h i j
k
Trees Recursive definition
10
![Page 12: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/12.jpg)
a
b c d e
f g h i j
k
Trees Recursive definition
11
![Page 13: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/13.jpg)
a
b c d
f g h i j
k
e
Trees Recursive definition
12
![Page 14: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/14.jpg)
a
b c d
f g h i j
k
e
Trees Recursive definition
13
![Page 15: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/15.jpg)
a
b c d
f g h i j
k
e
Trees Recursive definition
14
![Page 16: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/16.jpg)
Terminology
• A node is external, if it doesn't have children (it is a leaf)
• A node is internal, if it has one or more children
• A node is ancestor of another one, if it is its parent or an ancestor of its parent
• A node is descendent of another one, if the latter is ancestor of the former
• The descendents of a node determine a subtree where this node acts as the root
15
![Page 17: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/17.jpg)
Terminology
• A path from one node to another one, is a
sequence of consecutive edges between the
nodes.
– Its length is the number of edges it is composed of.
• The depth of a node is the length of the path
from the root to this node.
• The height of a tree is the depth of the deepest
node.
• The size of a tree is the number of nodes.
16
![Page 18: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/18.jpg)
a
b c d e
f g h i j
k Size of the tree: 11
Height of the tree: 3
Node Height Depth Size Internal /
External
a 3 0 11 Internal
b
c 0 1 1 External
d 1 1 2 Internal
e
f 0 2 1 External
g 0 2 1 External
h
i
j
k
Example Terminology and properties
17
![Page 19: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/19.jpg)
Terminology Ordered tree
• A tree is ordered, if for each node there exists a
linear ordering for its children.
a
b c
a
c b
18
![Page 20: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/20.jpg)
• A binary tree is an ordered tree, where
each node has 0, 1 or 2 children (the
left and the right child).
– Full binary tree: each node is either a leaf
or possesses exactly 2 children
– Complete binary tree: all levels except
possibly the last are full, and the last level
has all its nodes to the left side
Terminology Binary tree
19
![Page 21: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/21.jpg)
BTree interface
public interface BTree<E> {
static final int LEFT = 0;
static final int RIGHT = 1;
boolean isEmpty();
E getInfo() throws BTreeException;
BTree<E> getLeft() throws BTreeException;
BTree<E> getRight() throws BTreeException;
void insert(BTree<E> tree, int side) throws BTreeException;
BTree<E> extract(int side) throws BTreeException;
String toStringPreOrder();
String toStringInOrder();
String toStringPostOrder();
String toString(); // preorder
int size();
int height();
boolean equals(BTree<E> tree);
boolean find(BTree<E> tree);
} 20
![Page 22: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/22.jpg)
Implementations
• Array-based implementation
• Based on a linked structure
21
![Page 23: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/23.jpg)
Array-based
implementation
1
3 2
7 6 5 4
1 2 3 4 5 6 7
p(root)=1
p(x.left)=2*p(x)
p(x.right)=2*p(x)+1
22
![Page 24: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/24.jpg)
Implementation based
on a linked structure
1
3 2
7 6 5 4
23
Linked Binary Node (LBNode)
Linked Binary Tree (LBTree)
• Each tree (LBTree) has a root node (LBNode
attribute)
• Each root node LBNode contains two trees (LBTree
attributes), which can be empty (null)
![Page 25: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/25.jpg)
LBNode class
(recursive tree)
public class LBNode<E> {
private E info;
private BTree<E> left;
private BTree<E> right;
public LBNode(E info, BTree<E> left, BTree<E> right) {…}
public E getInfo() {…}
public void setInfo(E info) {…}
public BTree<E> getLeft() {…}
public void setLeft(BTree<E> left) {…}
public BTree<E> getRight() {…}
public void setRight(BTree<E> right){…}
}
24
![Page 26: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/26.jpg)
public class LBTree<E> implements BTree<E> {
private LBNode<E> root;
public LBTree() {
root = null;
}
public LBTree(E info) {
root = new LBNode<E>(info, new LBTree<E>, new
LBTree<E>);
}
public boolean isEmpty() {
return (root==null);
}
LBTree class...
(recursive tree)
25
![Page 27: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/27.jpg)
public E getInfo() throws BTreeException {
if (isEmpty()) {
throw new BTreeException("empty trees do not have info");
}
return root.getInfo();
}
public BTree<E> getLeft() throws BTreeException {
if (isEmpty()) {
throw new BTreeException("empty trees do not have a left child");
}
return root.getLeft();
}
public BTree<E> getRight() throws BTreeException {
if (isEmpty()) {
throw new BTreeException("empty trees do not have a right child");
}
return root.getRight();
}
… LBTree class...
(recursive tree)
26
![Page 28: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/28.jpg)
public class LBNode<E> {
private E info;
private LBNode<E> left;
private LBNode<E> right;
public LBNode() {
this(null);
}
public LBNode(E info) {
this(info,null,null);
}
public LBNode(E info, LBNode<E> l, LBNode<E> r) {
this.info = info;
left = l;
right = r;
}
}
Binary node...
(non-recursive)
27
![Page 29: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/29.jpg)
Basic algorithms
• Size (number of nodes)
• Depth of a node
• Height
• Traversals
– Euler
– Pre-, in- and post-order
(To simplify, we assume binary trees)
28
![Page 30: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/30.jpg)
… LBTree class...
(recursive tree)
public int size() {
if (isEmpty())
return 0;
else
return 1 + root.getLeft().size() + root.getRight().size();
}
public int height() {
if (isEmpty())
return -1;
else {
int leftHeight = root.getLeft().height();
int rightHeight = root.getRight().height();
if (leftHeight > rightHeight)
return 1 + leftHeight;
else
return 1 + rightHeight;
}
}
29
1+Math.max(leftHeight, rightHeight);
![Page 31: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/31.jpg)
Euler traversal
30
![Page 32: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/32.jpg)
Preorder traversal
• First the node
• Then its children
(recursively)
1
3 2
6 4
7 5
31
![Page 33: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/33.jpg)
Postorder traversal
7
6 1
5 3
4 2
• First the children trees
(recursively)
• Then the node
32
![Page 34: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/34.jpg)
Inorder (symmetric)
traversal
2
5 1
7 3
6 4
• First the left tree
(recursively)
• Then the node
• Finally, the right tree
(recursively)
33
![Page 35: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/35.jpg)
(A+B)*(C–D)
*
– +
D C B A
34
![Page 36: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/36.jpg)
Example
Infix Prefix Postfix
A+B +AB AB+
A+B–C –+ABC AB+C–
(A+B)*(C–D) *+AB–CD AB+CD–*
35
![Page 37: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/37.jpg)
Activity
• Visualize expressions as trees http://www.cs.jhu.edu/~goodrich/dsa/05trees/Demo1/
36
![Page 38: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/38.jpg)
Postfix notation
• HP calculators, RPN=Reverse Polish
Notation
• Stack to store objects
• Eg: 3 5 + 6 2 – *
3
5 8 6 2 4 32
37
![Page 39: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/39.jpg)
LBTree class: inorder
(recursive tree)
public String toStringInOrder() {
if (isEmpty()) {
return "";
} else {
return root.getLeft().toStringInOrder() +
root.getInfo().toString() + " " +
root.getRight().toStringInOrder(); }
}
38
![Page 40: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/40.jpg)
Properties of binary trees
• Let
– E=Number of external nodes
– I=Number of internal nodes
– N=Size=E+I
– H=Height
• then
– E=I+1
– H+1≤E≤2H H≤I≤2H-1 2*H+1≤N≤2H+1-1
– log2(N+1)-1≤H≤(N-1)/2
39
![Page 41: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/41.jpg)
Binary search trees
• A binary search tree is a binary tree
where for each node n,
– all the keys in the left subtree are
smaller (or equal) than the key of n
– and all those of the right subtree larger
(or equal)
40
![Page 42: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/42.jpg)
Example
4
8 2
9 6 3 1
7 5
1
2
3
3
41
![Page 43: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/43.jpg)
Example
4
8
2
9 6
3 1
7
5
4
3
2
1
42
![Page 44: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/44.jpg)
Operations
• Search
• Insertion
• Extraction
43
![Page 45: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/45.jpg)
Search
Searching “3”: • 3<4: go to left subtree • 3>2: go to right subtree • 3=3: element found
4
8 2
9 6 3 1
7 5
4
2
3
http://www.cosc.canterbury.ac.nz/
mukundan/dsal/BST.html
44
![Page 46: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/46.jpg)
Insertion
Inserting “6”: • 6<7: go to left subtree • 6>2: go to right subtree • when hole: insert
7
9 2
5 1
3 6
7
2
5
45
![Page 47: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/47.jpg)
Extraction (1)
Extracting “5”: • if leaf: extract • if degenerate: replace by child 7
9 2
5 1
3
3
46
![Page 48: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/48.jpg)
Extraction (2)
Extracting “2”: • if 2 children: replace by
• largest at left, or • smallest at right subtree
7
9 2
5 1
3
3
47
![Page 49: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/49.jpg)
Activity
• See animation of binary search trees http://www.ibr.cs.tu-
bs.de/courses/ss98/audii/applets/BST/BST-Example.html
48
![Page 50: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/50.jpg)
Heaps
• A binary heap is a complete binary tree
where every node has a key greater(*)
than or equal to the key of its parent.
– Usually, heaps refer to binary heaps * It could also be defined as less or equal (the order criteria is arbitrary)
• Utitity
– Priority queues
– Sorting algorithm
49
![Page 51: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/51.jpg)
Heaps properties
• A heap fulfils two properties:
– Heap property: for each node n (except for
the root), its key is larger or equal than the
one of its parent.
– Completeness
50
![Page 52: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/52.jpg)
Example: heap property
1
4 2
9 6 3
7 8
But not complete
51
5
![Page 53: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/53.jpg)
Example: complete heap
1
4 2
9 6 5 3
7 8
52
![Page 54: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/54.jpg)
Sequence-based
implementation
1
3 2
7 6 5 4
1 2 3 4 5 6 7
p(root)=1
p(x.left)=2*p(x)
p(x.right)=2*p(x)+1
53
![Page 55: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/55.jpg)
Insert
4
6 5
20 7 9 15
25 16 12 14 8 11
54
![Page 56: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/56.jpg)
Insert
4
6 5
20 7 9 15
25 16 12 14 8 11 2
55
![Page 57: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/57.jpg)
Insert
4
6 5
2 7 9 15
25 16 12 14 8 11 20
56
![Page 58: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/58.jpg)
Insert
4
2 5
6 7 9 15
25 16 12 14 8 11 20
57
![Page 59: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/59.jpg)
Insert
2
4 5
6 7 9 15
25 16 12 14 8 11 20
58
![Page 60: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/60.jpg)
Extract
4
6 5
20 7 9 15
25 16 12 14 8 11
59
![Page 61: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/61.jpg)
Extract
8
6 5
20 7 9 15
25 16 12 14 11
60
![Page 62: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/62.jpg)
Extract
5
6 8
20 7 9 15
25 16 12 14 11
61
![Page 63: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/63.jpg)
Activity
• Try out the form in http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Prior
ity-Q/
62
![Page 64: Systems Programming - UC3M · 2014-05-07 · Trees Systems Programming Julio Villena Román (LECTURER) CONTENTS ARE MOSTLY BASED ON THE WORK BY: Carlos](https://reader033.vdocument.in/reader033/viewer/2022041921/5e6c0475c8d8ca400a794d6d/html5/thumbnails/64.jpg)
Activity
• Try out the applet in http://www.cosc.canterbury.ac.nz/mukundan/dsal/MinHea
pAppl.html
63