Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Similar presentations


Presentation on theme: "Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons."— Presentation transcript:

1 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. http://jagger.me.berkeley.edu/~pack/e77http://creativecommons.org/licenses/by-sa/2.0/

2 Trees Sciences OrganicMolecular MathPhysicsChemistryBiology QuantumAlgebra What is a tree? A data structure to represent hierarchical or sorted data

3 Trees: terminology A G D CB H F E 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)

4 Trees: storing with a struct A G D CB H F E 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

5 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 C B G F E I L 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 D i ≤D k or D k ≤D i. Moreover, if D 1 ≤D 2 and D 2 ≤D 3 then it must be that D 1 ≤D 3. This gives efficient search routines. More in Weeks 11, 12 and 13.

6 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

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

8 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 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 A GD CB HFE I ABEDCFIGH

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

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

11 InOrder A D C B G F E function inordop(BTree,Node) % First call inordop % 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); D, B, E, A, F, C, G


Download ppt "Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons."

Similar presentations


Ads by Google