Académique Documents
Professionnel Documents
Culture Documents
CAS SD04
Outline
Trees
Tree Traversal
Binary Trees
Implementing Trees
Outline
Trees
Tree Traversal
Binary Trees
Implementing Trees
What is a Tree?
I In mathematics and computer science, a tree is an abstract
model of a hierarchical structure
I A tree consists of a set of connected nodes with a parent-child
relation
I Trees are special cases of connected graphs with no cycles
(there is unique path between any two nodes)
I A disconnected tree, i.e. a set of trees, is called forest
I Trees are usually depicted upside down, with the root on top
I Applications:
Ý File systems
Ý Organization charts
Ý Class hierarchy in OOP
Ý Structured documents (section, subsection, etc.)
196K 253K
algdata/ xyz webprog/
1K 68K 1K
129K 65K
hw1 hw2 hw3 hw4 sl1 sl2 sl3 papers/ demos/
3K 2K 5K 5K 64K 32K 74K 1K 1K
p1 p1 xyz
32K 96K 64K
Tree Terminology
Root: Node without parent (A)
Internal Node: Node with at least one child
(A,B,C,G)
A 0
External Node: Node without children (leaf)
(E,F,I,J,K,H,D)
B C D 1
Descendant: Children, grandchildren, . . .
Ancestors: Parent, grandparent, . . . E F G H 2
Size: Number of nodes (11)
Depth: Number of ancestors I J K 3
Tree ADT
I We use positions to abstract nodes (see list ADT)
I General operations:
Ý size(): returns the number of nodes
Ý height(): returns the height of the tree
Ý isEmpty(): indicates whether the tree is empty
Ý elements(): returns an iterator over all elements
Ý positions(): returns an iterator over all positions
I Accessors:
Ý root(): returns the root node (if it exists)
Ý parent(p): returns the parent of node p (if it exists)
Ý children(p): returns an iterator over all children of p
Ý descendants(p): returns an iterator over all descendants of p
Ý ancestors(p): returns an iterator over all ancestors of p
I Query Operations:
Ý isRoot(p): indicates whether p is a root
Ý isInternal(p): indicates whether p is an internal node
Ý isExternal(p): indicates whether p is an external node
Ý depth(p): returns the depth of node p
I Update Operations:
Ý addRoot(e): adds a root which stores e to an empty tree
Ý insertChild(p,e): inserts a new child for p which stores e
Ý replaceElement(p,e): stores e at node p
Ý swapElements(p,q): swaps the elements stored at p and q
I Further update operations (e.g. for removing nodes) may be
added on request
Algorithm height()
return heightOf(root())
Outline
Trees
Tree Traversal
Binary Trees
Implementing Trees
Postorder Traversal
11
Algorithm postOrder(p)
3 9 10
foreach q in children(p) do
postOrder(q) 1 2 7 8
visit(p)
4 5 6
Preorder Traversal
Outline
Trees
Tree Traversal
Binary Trees
Implementing Trees
Binary Tree
I A binary tree is a tree with the following properties:
Ý Each internal node has at most two children
Ý The children of a node are an ordered pair
I The children of an internal node are called left and right child
I Alternative recursive definition: a binary tree is either
Ý a single node called root, or
Ý a tree whose root has an ordered pair of children, each of
which is a binary tree
I Applications:
Ý Arithmetic expressions
Ý Decision trees
Ý Searching
Inorder Traversal
I In a inorder traversal, a node is visited after its left subtree
and before its right subtree
Algorithm inOrder(p) 4
if hasLeft(p) then
2 8
inOrder(leftChild(p))
visit(p) 1 3 6 9
if hasRight(p) then
5 7
inOrder(rightChild(p))
Algorithm eulerTour(p)
if isExternal(p) then L R
visitExternal(p) B
else
L R L R
visitLeft(p)
B B
if hasLeft(p) then
eulerTour(leftChild(p)) L R
visitBelow(p) E E B E
if hasRight(p) then
eulerTour(rightChild(p))
E E
visitRight(p)
Outline
Trees
Tree Traversal
Binary Trees
Implementing Trees
Level Numbering
The level number L(p) of a node p in a binary tree is defined as
follows:
1,
if p is the root of the tree
L(p) = 2·L(q), if p is the left child of node q
2·L(q) + 1, if p is the right child of node q
2 3
4 5 6 7
11 12
8 9 10 13
16 17 20 21 26 27
Array-Based Implementation
4 5
D null null null null null Array T
0 1 2 3 4 5 6 7 8
B C A
D E B C
D E
A A
C
B C
B
D E F
D E F
UML Diagram
<<interface>> <<interface>>
BinaryTree List
etc. etc.