Trees

advertisement
Trees
UC Berkeley
Fall 2004, E77
http://jagger.me.berkeley.edu/~pack/e77
Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Trees
What is a tree?
A data structure to represent hierarchical or sorted
data
Sciences
Biology
Molecular
Chemistry
Physics
Organic
Quantum
Math
Algebra
Trees: terminology
Node: any item of the tree
Root Node: initial item of tree
Parent: the parent of F is C
A
Child: E is a child of B
Leaf Node: A node with no
children.
B
C
Subtree
A tree in its own right
D
E
F
Ancestors (parent, parent of parent,
etc.)
Descendents (children, children of
children, etc)
Siblings (children with
same parent)
G
I
H
Trees: storing with a struct
T(1).Data = {’A’ [12 5]};
T(2).Data = {’B’ [01 4]};
T(3).Data = {’C’ [-7 3]};
T(4).Data = {’D’ [41 0]};
T(5).Data = {’E’ [.7 9]};
T(6).Data = {’F’ [21 2]};
T(7).Data = {’G’ [14 1]};
T(8).Data = {’H’ [06 6]};
T(9).Data = {’I’ [97 4]};
T(1).Children = [2 3];
T(2).Children = [4 5];
T(3).Children = [6 7 8];
T(4).Children = [];
T(5).Children = [];
T(6).Children = [];
T(7).Children = 9;
T(8).Children = [];
T(9).Children = [];
A
B
D
C
E
F
G
I
In this example, the data at each
node is a cell array, containing the
node name and a 1-by-2 array of
numerical information
H
Binary Tree
If every node has 0, 1 or 2 children, then the tree is called
a binary tree.
The children of a node in a binary tree are referred to as
Left and Right.
A
Binary Search Trees:
Used to store data that can be
“sorted”, ie., a notion of ≤ exists for
the data.
B
The data at each node must have the
property that Di≤Dk or Dk≤Di.
Moreover, if D1≤D2 and D2≤D3 then it
must be that D1≤D3.
This gives efficient search routines.
More in Weeks 11, 12 and 13.
D
L
C
M
G
F
E
J
H
I
Traversing a Tree
An elementary and common
operation on a tree is to start at the
root node, and
– traverse the tree (ie., visit every
node), while…
– performing some operation at each
node (using, for example, the Data
at the node)
Recursive functions serve this
purpose well.
Two traversals: PreOrder and
PostOrder
InOrder for Binary trees
A
B
D
C
E F
G
I
H
PreOrder traversal of a Tree
function preordop(Tree,Node)
% First, do operation using Data
% at the given Node of the Tree
Operation(Tree(Node).Data);
% Then loop through all Children,
% calling preordop recursively
for i=Tree(Node).Children
preordop(Tree,i);
In this example, Operation
end
represents any desired
function to act on the data
Note: As written here, based on Matlab
syntax, the for loop requires that the
children nodes be listed in a row vector.
Preorder traversal of a Tree
function poprint(T,Node)
disp(T(Node).Name)
poprint(T,T(Node).Kids(1))
…
poprint(T,T(Node).Kids(end))
A
B
C
>> poprint(Tree,1)
At A, print A, do same A’s kids
At B, print B, do same for B’s kids
At D, print D.
At E, print E.
At C, print C, do same for C’s kids
At F, print F
At G, print G, do same for G’s kids
At I, print I.
At H, print H
D
E F
G
H
I
A B DE C F G I H
Post-order traversal of a Tree
function postordop(Tree,Node)
% First loop through all Children,
% calling postordop recursively
for i=Tree(Node).Children
postordop(Tree,i);
end
% Then do operation using the Data
% at the given Node of the Tree
Operation(Tree(Node).Data);
In-order traversal of a Binary Tree
function inordop(BTree,Node)
% First call inordop recursively
% on the “left” child
inordop(Tree,Tree(Node).Left);
% Do operation using the Data
% at the given Node of the Tree
Operation(Tree(Node).Data);
% Finally call inordop recursively
% on the “right” child
inordop(Tree,Tree(Node)).Right);
InOrder
A
function inordop(BTree,Node)
B
% First call inordop
% on the “left” child
inordop(Tree,Tree(Node).Left);
D
% Do operation using the Data
% at the given Node of the Tree
Operation(Tree(Node).Data);
% Finally call inordop recursively
% on the “right” child
inordop(Tree,Tree(Node)).Right);
D, B, E, A, F, C, G
C
E
F
G
Download