trees uc berkeley fall 2004, e77 pack/e77 copyright 2005, andy packard. this work is licensed under...

11
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

Upload: vincent-roberts

Post on 19-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

TreesUC Berkeley

Fall 2004, E77http://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.

Page 2: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

Trees

Sciences

OrganicMolecular

MathPhysicsChemistryBiology

Quantum Algebra

What is a tree?

A data structure to represent hierarchical or sorted data

Page 3: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

Trees: terminology

A

GD

CB

HFE

I

Node: any item of the tree

Root Node: initial item of tree

Parent: the parent of F is C

Child: E is a child of B

Leaf Node: A node with no children.

Subtree

A tree in its own right

Ancestors (parent, parent of parent, etc.)

Descendents (children, children of children, etc)

Siblings (children with same parent)

Page 4: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

Trees: storing with a struct

A

GD

CB

HFE

I

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 = [];

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

Page 5: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

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

D

CB

GFE

IL M J H

Binary Search Trees:

Used to store data that can be “sorted”, ie., a notion of ≤ exists for the data.

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.

Page 6: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

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

GD

CB

HFE

I

Page 7: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

PreOrder traversal of a Tree

function preordop(Tree,Node)

% First, do operation using Data% at the given Node of the TreeOperation(Tree(Node).Data);

% Then loop through all Children,% calling preordop recursivelyfor i=Tree(Node).Children preordop(Tree,i);end

Note: As written here, based on Matlab

syntax, the for loop requires that the children nodes be listed in a row vector.

In this example, Operation represents any desired function to act on the data

Page 8: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

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))

>> poprint(Tree,1)

At A, print A, do same A’s kidsAt 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 kidsAt F, print FAt G, print G, do same for G’s kids

At I, print I. At H, print H

A

GD

CB

HFE

I

A B ED C F IG H

Page 9: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

Post-order traversal of a Tree

function postordop(Tree,Node)

% First loop through all Children,% calling postordop recursivelyfor i=Tree(Node).Children postordop(Tree,i);end

% Then do operation using the Data% at the given Node of the TreeOperation(Tree(Node).Data);

Page 10: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

In-order traversal of a Binary Tree

function inordop(BTree,Node)

% First call inordop recursively% on the “left” childinordop(Tree,Tree(Node).Left);

% Do operation using the Data% at the given Node of the TreeOperation(Tree(Node).Data);

% Finally call inordop recursively% on the “right” childinordop(Tree,Tree(Node)).Right);

Page 11: Trees UC Berkeley Fall 2004, E77 pack/e77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons

InOrder A

D

CB

GFE

function inordop(BTree,Node)

% First call inordop% on the “left” childinordop(Tree,Tree(Node).Left);

% Do operation using the Data% at the given Node of the TreeOperation(Tree(Node).Data);

% Finally call inordop recursively% on the “right” childinordop(Tree,Tree(Node)).Right);

D, B, E, A, F, C, G