Sie sind auf Seite 1von 8

1

L8:Trees 1
Trees
L8:Trees 2
Why Do We Need Trees?
Lists, Stacks, and Queues are linear
relationships
Information often contains hierarchical
relationships
File directories or folders
Moes in a !ame
"ierarchies in or!ani#ations
$an %uild a tree to support fast searchin!
L8:Trees &
Tree Jargon
root
nodes and edges
leaves
parent, children, siblings
ancestors, descendants
subtrees
path, path length
height, depth
A
B C D
E F
L8:Trees '
More Tree Jargon
Length of a path ( num%er
of ed!es
Depth of a node ) ( len!th
of path from root to )
Height of node )( len!th
of lon!est path from ) to a
leaf
Depth of tree ( depth of
deepest node
Height of tree ( hei!ht of
root
A
B C D
E F
depth=0,
height = 2
depth = 2,
height=0
depth=1,
height =0
2
L8:Trees *
Definition and Tree Trivia
+ tree is a set of nodes,i,e,, either
it-s an empt. set of nodes, or
it has one node called the root from /hich #ero
or more trees 0su%trees1 descend
T/o nodes in a tree hae at most one path
%et/een them
$an a non2#ero path from node ) reach node
) a!ain3
)o, Trees can neer hae c.cles 0loops1
L8:Trees 4
Paths
+ tree /ith ) nodes al/a.s has )21
ed!es 0proe it %. induction1
Base Case: N=1 one node, zero edges
Inductive Hypothesis: Suppose that a tree with N=k nodes
always has k-1 edges.
Induction: Suppose N=k+1
The k+1st node must connect
to the rest by 1 or more edges.
If more, we get a cycle. So it connects by just 1 more edge
k
+1
L8:Trees 5
Implementation of Trees
6ne possi%le pointer2%ased Implementation
tree nodes /ith alue and a pointer to each child
%ut ho/ man. pointers should /e allocate space for3
+ more fle7i%le pointer2%ased implementation
1
st
$hild 8 )e7t Si%lin! List 9epresentation
:ach node has 2 pointers: one to its first child and one
to ne7t si%lin!
$an handle ar%itrar. num%er of children
L8:Trees 8
Arbitrary ran!hing
A
B C D
E F
A
B C D
E F
Data
FirstChild Sibling
Nodes
of same
depth
3
L8:Trees ;
inary Trees
:er. node has at most t/o children
Most popular tree in computer science
<ien ) nodes, /hat is the minimumdepth of a
%inar. tree3 0This means all leels %ut the last are full=1
+t depth d, .ou can hae ) ( 2
d
to ) ( 2
d>1
21 nodes

N log d implies 1 2 N 2
2 min
1 d d
=
+
L8:Trees 1?
Minim"m depth vs node !o"nt
+t depth d, .ou can hae ) ( 2
d
to 2
d>1
21 nodes
minimum depth d is 0lo! )1
1
2 3
6 7 4 5
T(n) is (f(n)) means
T(n) is O(f(n)) and f(n) is O(T(n)),
i.e. T(n) and f(n) have the same
growth rate
d=2
N=2
2
to 2
3
-1 (i.e, 4 to 7 nodes)
L8:Trees 11
Ma#im"m depth vs node
!o"nt
@hat is the ma7imum depth of a %inar.
tree3
Ae!enerate case: Tree is a linked list=
Ma7imum depth ( )21
<oal: @ould like to keep depth at
around lo! ) to !et %etter
performance than linked list for
operations like Find
L8:Trees 12
A degenerate tree
1
5
2
3
4
7
6
A linked list with high overhead
and few redeeming characteristics
4
L8:Trees 1&
Traversing inary Trees
The definitions of the traersals are
recursie definitions, For e7ample:
Bisit the root
Bisit the left su%tree 0i,e,, isit the tree /hose
root is the left child1 and do this recursiel.
Bisit the ri!ht su%tree 0i,e,, isit the tree /hose
root is the ri!ht child1 and do this recursiel.
Traersal definitions can %e e7tended to
!eneral 0non2%inar.1 trees
L8:Trees 1'
Traversing inary Trees
Creorder: )ode, then $hildren 0startin!
/ith the left1 recursiel. > D > + E $ A
Inorder: Left child recursiel., )ode,
9i!ht child recursiel. + > E D $ > A
Costorder: $hildren recursiel., then )ode
+ E > $ D A >
A
*
B
C
D
+
+
L8:Trees 1*
inary $ear!h Trees
Einar. search trees are %inar. trees in
/hich
all alues in the node-s left su%tree
are less than node alue
all alues in the node-s ri!ht su%tree
are !reater than node alue
6perations:
Find, FindMin, FindMa7, Insert, Aelete
@hat happens /hen /e traerse the
tree in inorder3
9
5
10
96 99
94
97
L8:Trees 14
%perations on inary $ear!h
Trees
"o/ /ould .ou implement these3
9ecursie definition of %inar.
search trees allo/s recursie routines
$all %. reference helps too
FindMin
FindMa7
Find
Insert
Aelete
9
5
10
96 99
94
97
5
L8:Trees 15
inary $ear!hTree
9
5
10
96 99
94
97
data
left right
9
5 94
10 97
96 99
L8:Trees 18
&ind
Find(T : tree pointer, x : element): tree pointer {
case {
T = null : return null;
T.data = x : return T;
T.data > x : return Find(T.left,x);
T.data < x : return Find(T.right,x)
}
}
L8:Trees 1;
&indMin
Aesi!n recursie FindMin operation
that returns the smallest element in a
%inar. search tree,
FindMin(T : tree pointer) : tree pointer {
// precondition: T is not null //
???
}
L8:Trees 2?
Insert %peration
Insert(T: tree, X: element)
Ao a FFindG operation for
H
If H is found update
0no need to insert1
:lse, FFindG stops at a
)ILL pointer
Insert )ode /ith H there
:7ample: Insert ;*
10
96 99
94
97
?
6
L8:Trees 21
Insert '(
10
96 99
94
97
10
96 99
94
97
95
L8:Trees 22
Insert Done )ith !all*by*
referen!e
Insert(T : reference tree pointer, x : element) : integer {
if T = null then
T := new tree; T.data := x; return 1;//the links to
//children are null
case
T.data = x : return 0;
T.data > x : return Insert(T.left, x);
T.data < x : return Insert(T.right, x);
endcase
}
Advantage of reference parameter is that the call has
the original pointer not a copy.
This is where call by
reference makes a
difference.
L8:Trees 2&
+all by ,al"e vs
+all by -eferen!e
$all %. alue
$op. of parameter is used
$all %. reference
+ctual parameter is used
p
p
F(p)
used inside call of F
L8:Trees 2'
Delete %peration
Aelete is a %it trickierJ@h.3
Suppose .ou /ant to delete 1?
Strate!.:
Find 1?
Aelete the node containin! 1?
Cro%lem: @hen .ou delete a node,
/hat do .ou replace it %.3
94
10 97
5 24
11
17
7
L8:Trees 2*
Delete %peration
Cro%lem: @hen .ou delete a node,
K /hat do .ou replace it %.3
Solution:
If it has no children, %. )ILL
If it has 1 child, %. that child
If it has 2 children, %. the node /ith
the smallest alue in its ri!ht su%tree
0the successor of the node1
94
10 97
5 24
11
17
L8:Trees 24
Delete .(/ * No !hildren
Find 5 node
Then Free
the 5 node and
NULL the
pointer to it
94
10 97
5 24
11
17
94
10 97
5 24
11
17
L8:Trees 25
Delete .01/ * %ne !hild
Find 24 node
Then Free
the 24 node and
replace the
pointer to it with
a pointer to its
child
94
10 97
5 24
11
17
94
10 97
5 24
11
17
L8:Trees 28
Delete .23/ * t)o !hildren
Find 10,
Copy the smallest
value in
right subtree
into the node
Then (recursively)
Delete node with
smallest value
in right subtree
Note: it cannot
have two children
(why?)
94
10 97
5 24
11
17
94
11 97
5 24
11
17
8
L8:Trees 2;
Then Delete .22/ * %ne !hild
Remember
11 node
Then Free
the 11 node and
replace the
pointer to it with
a pointer to its
child
94
11 97
5 24
11
17
94
11 97
5 24
11
17
L8:Trees &?
-emove from Te#t
priate Einar.)ode remoe0 $ompara%le 7, Einar.)ode t1 L
if 0 t (( null1 return tM 88 not found
if 0 7,compareTo0 t,element 1 N ? 1
t,left ( remoe0 7, t,left 1M 88 search left
else if 0 7,compareTo0 t,element1 O ? 1
t,ri!ht ( remoe07, t,ri!ht 1M 88 search ri!ht
else if 0t,left =( null PP t,ri!ht =( null1 88 found itM t/o
children
L t,element ( findMin 0t,ri!ht 1,elementM 88 find the min,
replace,
t,ri!ht ( remoe0 t,element, t,ri!ht1M Q and remoe it
else t ( 0t,left =( null 1 3 t,left : t,ri!htM 88 found itM one
child
return tM Q
L8:Trees &1
&indMin $ol"tion
FindMin(T : tree pointer) : tree pointer {
// precondition: T is not null //
if T.left = null
return T
else
return FindMin(T.left)
}

Das könnte Ihnen auch gefallen