Title: The Tree Data model
1The TreeData model
2Why Trees ?
- Convenient for representing many problem
domains. - Efficient representation for large sets,
- the linear organization (list), with running
time of T(n) O(n), is prohibitive. - Binary Search Trees can be used to handle data
with running time of T(n) O(lg n)on
average, for most operations. - EX
- Linear search O(n)
- Binary search O(lg n), but insert/delete O(n)
- BST search, insert, delete O(n)
3Basic Terminology
- Trees are sets of points called nodes and lines
connecting nodes called edges. - The nodes and edges satisfy the following
properties - One node is distinguished, called root ( r ). The
root has no parent. - Every node other than the root has a parent, and
a single one. - The tree is connected.
- For every node v of the tree, there is a unique
pathfrom r to v.
r
v
4Recursive definition of a Tree
- Base
- An empty tree is a tree
- Induction
- let T1,T2,Tn be a set of trees with distinct
nodes as roots r1,r2,,rn, - let a be a new node.
- construct edges (a, r1), (a, r2),,(a, rn)(make
a the parent of the roots of T1,.,Tn).The
resulting graph is a tree with root a.
5More Terminology
- Path in a tree a sequence of nodes
- length of a path of edges on the path
- Ancestor and Descendant
- parent and child
- Subtree
- Leaves and interior-node
- Height of a node x - length of the longest path
from x to a leaf. (all leaves at height
0) - Height of a tree - height of the root
- Depth of a node n - length of the path from the
root (root at depth 0) - Degree of a node
depth 0
depth 1
depth 2
6Think Recursively
- Computing the height of a tree is complex without
recursion. - The height of a tree is one more than the maximum
of the heights of the subtrees.
HR1
HL1
HR
HL
7Routine to Compute Height
- Handle base case (empty tree)
- Use previous observation for other case.
- public static int Height( TreeNode T )
-
- if( T null )
- return -1 // Why???
- else
- return 1 Max(Height(T.Left),Height(T.Rig
ht))
8Running Time
- This strategy is a postorder traversal
information for a tree node is computed after the
information for its children is computed. - Postorder traversal running time is N times the
cost of processing each node. - The running time is linear because we do constant
work for each node in the tree.
9Ordered Trees
- Ordered tree - (to the left of..)
- A tree with a linear ordering on the children of
each node. (left to right). - Defines a total order between siblings
- Defines a total order between nodes not on the
same path to the root.Ex Defines an order
between all leaves
Ex Not the same trees
z
y
y
x
z
x
g
d
h
a
c
e
f
g
a
c
b
h
b
d e f
10Unordered Trees
Ex the same trees
z
y
y
x
z
x
g
d
h
a
c
e
f
g
a
c
b
h
b
f d e
11Labeled Trees
Associates a label (value) with each node.
Example Expression Trees, representing
arithmetic expressions. Just as expressions can
be defined recursively, so can the corresponding
labeled tree.
3
5
2
5
3
5
2
5
35
25
(35) (25)
12Example
((57)(34))3
3
4
3
5
7
13A Common Tree ApplicationDirectory Structure
\usr
\avner
\moshe
\leah
\books
\classes
\junk
lecture1.ppt
lecture1.ppt
Example of a labeled tree
14Tree traversal
Example Printing the directory tree
public void listDirectory(int depth) printname(
depth) // print name of directory or file if
(isDirectory) for each file f in this directory
(i.e., for each child) f.printDirectory(depth1
)
An example of a preorder traversal.
15Directory Structure
\usr \Moshe \classes
lecture1.ppt lecture2.ppt \junk
\books \Avner \Leah
16Tree traversal
Example Printing file sizes (also evaluating
expression trees)
\usr 3
\avner 1
\moshe 8
\leah 1
\books 1
\classes 10
\junk 1
lecture2.ppt 20
lecture1.ppt 3
17Tree traversal
Printing file sizes
public int size( ) int totalSize
sizeOfThisFile( ) if ( isDirectoty ) for
each file f in this directory totalSize
f.size( ) return totalSize
Postorder traversal
18Tree traversal
Postorder traversal
lecture1 20 lecture2 13 \classes
33 \junk 1 \books 1 \moshe
35 \avner 1 \leah 1 \usr 37
19Data Structures for Trees
20Dynamic Representation Array of pointers
array contains object or pointer to object array
of pointers to children
..
..
..
..
..
..
..
..
Array of references - allow access to child in
O(1)
Class TreeNode Class Tree - a reference to
a TreeNode
21Dynamic Representation Left Child /Right
Sibling
tree
tree
parent pointer ( root (node with parent
pointer null) )
Use linked list to represent siblings - access in
O(i)
22Left Child, right sibling
public class TreeNode private Object
data private TreeNode lson private TreeNode
rsibling ..
public class TreeNode private Object
data private TreeNode lson private TreeNode
rsibling private TreeNode parent ..
23Binary Trees
24Binary Tree Recursive Definition
Base a single node is a binary tree.
(!!) Induction if T1 and T2 are binary
trees, then so is T
T2
T1
T
25Binary Tree
A binary tree is an ordered , positional tree
T1
T2
T2
T1
Different binary trees
Five different binary trees with 3-nodes
Same ordered, different positional
Worst case binary trees
26Data structures for binary trees
- Use a dynamic structure with pointers to left
and right sons
class TreeNode private Object data private
TreeNode left private TreeNode
right .. class Tree TreeNode
root .
27Example Labeled Binary Trees
- Expression trees (tree representation of
arithmetic expressions) - An expression tree is a labeled binary tree
defined as follows - Base
- a single operand is an expression, represented by
a single node, labeled by the operand. - Induction
- if E1, E2 are expressions represented by labeled
binary trees T1 and T2, then E1 E2 is
represented by the following tree
T1
T2
28Recursive traversal of binary trees
// preorder traversal void preorder( ) print
data if (left ! null) left.preorder (
) if (right ! null) right.preorder ( )
// postorder traversal void postder( ) if
(left ! null) left.postorder ( ) if (right
! null) right.postorder ( ) print data
// inorder traversal void inorder( ) if (left
! null) left.inorder( ) print data if
(right ! null) right.inorder( )
29Binary Trees Representation
public class TreeNode private Object data
private TreeNode left private TreeNode
righ public void preorder() ..
public void inorder() . public void
postorder() ..
30Example Expression trees
What will we get if we perform a postorder
traversal preorder traversal inorder
traversal of the following expression
tree ???
3
4
3
5
7
31Complete, Full and Perfectbinary trees
A binary tree T is full, if each node in T is
either (a) an internal node with
2-children (b) a leaf
full
not full
A binary tree is complete, if it is filled from
left to right, a level at a time.
Complete but not full
not full and not complete
32The Full Binary Tree Theorem
The number of leaves in a non-empty, full
binary tree is one more than the number of
internal nodes
Proof induction on the of internal
nodes.
33Number of Nodes versus Height of a Complete
Binary Tree
n lt 20 21 22 2h (2h1 -
1)/(2-1) 2h1 - 1
20
21
sum 20 21 2h -2sum -
21 - - 2h - 2h1 n(1-2) lt (20 - 2h1)
22
23
n lt 2h1 - 1 and ngt 2h - 1