Lecture11: Trees I A Hierarchical Structure Tree Data Structures

A Hierarchical Structure
• Unix/Linux file systems
CSED233: Data Structures (2013F)
Lecture11: Trees I
Bohyung Han
CSE, POSTECH
[email protected]
2
Tree Data Structures
Tree Data Structures
• An abstract model of a hierarchical structure
• An abstract model of a hierarchical structure
 “2‐dimensional” structure
 Easy access (similar to binary search in array)
 Easy insert and removal (similar to linked list)
 Trees consist of nodes and links denoting parent‐child relation
 Special case of graphs without loops
 Each element (except root) has a parent and zero or more children.
Computers”R”Us
Sales
US
Europe
Computers”R”Us
Manufacturing
International
Asia
Laptops
R&D
Sales
US
Desktops
Canada
3
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Europe
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Manufacturing
International
Asia
Laptops
R&D
Desktops
Canada
4
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Terminology
• Root: node without parent A
• Internal node: node with at least one child A, B, C, F
• Leaf: node without children E, I, J, K, G, H, D
• Subtree: tree consisting of a node and its descendants
Tree Abstract Data Type
B
E
C
F
I
•
•
•
•
• Data structure
A
J
G
D
H
K
Ancestor of a node: parent, grandparent, grand‐grandparent …
Descendant of a node: child, grandchild, grand‐grandchild …
Depth (level) of a node: number of ancestors
Height of a tree: maximum depth of any node (3)
5
 Node: data element and link to children
 Root: a special node
• List of methods
method
description
root()
Return the tree’s root; error if tree is empty
parent(v)
Return v’s parent; error if v is a root
children(v)
Return v’s children (an iterable collection of nodes)
isRoot(v)
Test whether v is a root
isExternal(v)
Test whether v is an external node
isInternal(v)
Test whether v is an internal node
6
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Tree Abstract Data Type
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
A Linked Structure for Trees
• Generic methods (not necessarily related to a tree structure):
• A node is represented by an object storing
 Element
 Parent node
 Sequence of children nodes
method
description
isEmpty()
Test whether the tree has any nodes or not
size()
Return the number of nodes in the tree
iterator()
Return an iterator of all the elements stored in the tree
positions()
Return an iterable collection of all the nodes of the tree
replace(v,e)
Replace with e and return the element stored at node v
(parent node)
(a list of children)
(element)
(child node)
7
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
8
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
A Linked Structure for Trees
Binary Tree
• Definition

 a tree in which each node has at most two children
 Each child is either the left child or the right child of its parent.
B
B

D
A

A
F
D
F
left child
C
right child
E

C
9

E
10
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Arithmetic Expression Tree
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Decision Tree
• Binary tree associated with an arithmetic expression
 Internal nodes: operators
 External nodes: operands
• Binary tree associated with a decision process
 Internal nodes: questions with yes/no answer
 External nodes: decisions
• Example: arithmetic expression tree for the expression: (2  (a  1)  (3  b))
• Example: dining decision
Want a fast meal?
+
Yes


‐
2
a
3
On a diet?
b
1
11
No
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
On expense account?
Yes
No
Subway
McDonald’s
Yes
?????????
12
No
????????
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Proper Binary Trees
A Linked Structure for Binary Trees
• Each internal node has exactly 2 children.
• Properties







ei1
n  2e  1
hi
h  (n  1)2
e  2h
h  log2 e
h  log2 (n  1)  1




n :number of total nodes
e :number of external nodes
i :number of internal nodes
h :height (maximum depth of a node)
• A node is represented by an object storing




Element
Parent node
Left child node
Right child node
Element
Left child
13
public class Node {
int value;
Node left; Node right; B

D
C
E
D


C
15
// data stored at node
// pointer to left child
// pointer to right child
public Node(int i) {
value = i;
left = null;
right = null;
}

A
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Binary Tree Implementation

B
Right child
14
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
A Linked Structure for Binary Trees
A
Parent node

public
public
public
public

E
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Node
Node
void
void
getLeftChild(Node node) { return left; }
getRightChild(Node node) { return right; }
setLeftChild(Node node) { left = node; }
setRightChild(Node node) { right = node; }
}
16
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Binary Tree Implementation
An Array‐Based Representation
• Nodes are stored in an array A.
• Node v is stored at A[rank(v)]
public class BinaryTree
{
Node root;
 rank(root) = 1  Left in even: if node is the left child of parent(node),
rank(node) = 2  rank(parent(node))
 Right in odd: if node is the right child of parent(node),
rank(node) = 2 rank(parent(node))  1
public Tree() { root = null; }
// insert an element to the current tree
public void insert(int i) { ... }
• A[0] is always empty
• A[i] is empty if there is no node in the ith position
• The array size N is 2(h+1)
// deleted a node from the current tree
public Node delete(int i) { ... }
// find a node containing a particular value
public Node find(int i) { ... }
}
17
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
An Array‐Based Representation
18
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Tree Balance
• Full binary tree (perfect binary tree)
1
A
3
2
D
B
5
4
E
0
7
6
F
10
 A binary tree of height h is full if every node has exactly two children and all leaf nodes have the same level.
J
C
H
G
A
B
D
1
2
3
11
…
19
G
H
10
11
…
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
20
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
Tree Balance
• Complete binary tree
 A binary tree is complete if it is full to level h‐1 and level h is filled fro
m the left with contiguous nodes.
21
CSED233: Data Structures
by Prof. Bohyung Han, Fall 2013
22