trees by dr. bun yue professor of computer science [email protected] 2013 [email protected] csci 3333 data...

31
Trees by Dr. Bun Yue Professor of Computer Science [email protected] http://sce.uhcl.edu/yue/ 2013 CSCI 3333 Data Structures

Upload: bryce-ray

Post on 04-Jan-2016

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Trees

by

Dr. Bun YueProfessor of Computer Science

[email protected]://sce.uhcl.edu/yue/

2013

CSCI 3333 Data Structures

Page 2: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Acknowledgement

Mr. Charles Moen Dr. Wei Ding Ms. Krishani Abeysekera Dr. Michael Goodrich

Page 3: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

What is a Tree In computer

science, a tree is an abstract model of a hierarchical structure

A tree consists of nodes with a parent-child relation

Computers”R”Us

Sales R&DManufacturing

Laptops DesktopsUS International

Europe Asia Canada

Page 4: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Trees

Each node in the tree has 0 or 1 parent node. 0 or more child nodes.

Only the root node has no parent. A tree can be considered as a special case

of a directed (acyclic) graph. Example: a family tree may not

necessarily be a tree in the computing sense.

Page 5: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

5

British Royal Family TreeTrees

Queen Victoria

King Edward VII

Albert Victor

Alice

King George V

King George VIKing Edward VIII

Queen Elizabeth II

Charles Ann Andrew Edward

William Henry Peter Zara Beatrice Eugenie

Page 6: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Some Tree Applications

Applications: Organization charts File Folders Email Repository Programming environments: e.g. drop

down menus.

Page 7: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Recursive Definition of Trees

An empty tree is a tree. A tree contains a root node

connected to 0 or more child sub-trees.

Page 8: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

8

Are these trees?Trees

1 2

34

Page 9: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

subtree

Tree Terminology Root: node without parent (A) Internal node: node with at least

one child (A, B, C, F) External node (a.k.a. leaf ): node

without children (E, I, J, K, G, H, D)

Ancestors of a node: parent, grandparent, grand-grandparent, etc.

Depth of a node: number of ancestors

Height of a tree: maximum depth of any node (3)

Descendant of a node: child, grandchild, grand-grandchild, etc.

A

B DC

G HE F

I J K

Subtree: tree consisting of a node and its descendants

Page 10: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Tree ADT (§ 6.1.2) We use positions to

abstract nodes Generic methods:

integer size() boolean isEmpty() Iterator elements() Iterator positions()

Accessor methods: position root() position parent(p) positionIterator

children(p)

Query methods: boolean isInternal(p) boolean isExternal(p) boolean isRoot(p)

Update method: object replace (p, o)

Additional update methods may be defined by data structures implementing the Tree ADT

Page 11: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Interface TreeNode in Java

Enumeration children(): Returns the children of the receiver as an Enumeration.

boolean getAllowsChildren(): Returns true if the receiver allows children.

TreeNode getChildAt(int childIndex): Returns the child TreeNode at index childIndex.

int getChildCount(): Returns the number of children TreeNodes the receiver contains.

int getIndex(TreeNode node): Returns the index of node in the receivers children.

TreeNode getParent(): Returns the parent TreeNode of the receiver.

Boolean isLeaf(): Returns true if the receiver is a leaf.

Page 12: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Interface MutatableTreeNode

Sub-interface of TreeNode Methods:

void insert(MutableTreeNode child, int index): Adds child to the receiver at index.

void remove(int index): Removes the child at index from the receiver.

void remove(MutableTreeNode node): Removes node from the receiver.

void removeFromParent(): Removes the receiver from its parent.

void setParent(MutableTreeNode newParent): Sets the parent of the receiver to newParent.

void setUserObject(Object object): Resets the user object of the receiver to object.

Page 13: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Size of a tree

Size = number of nodes in the tree. Recursion analysis:

Base case: empty root => return 0; Recursive case: non-empty root

=> return 1 + sum of size of allchild sub-trees.

Page 14: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Size of a tree

Algorithm size(TreeNode root)Input TreeNode rootOutput: size of the treeif (root = null)

return 0;

elsereturn 1 + Sum of sizes of all children of

root

end if

Page 15: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Implementation in Java

public static int size(TreeNode root) {if (root == null) return 0;Enumeration enum = root.children();int result = 1; // count the root.while (enum.hasMoreElement()) {

result += size((TreeNode) enum.nextElement());

}return result;

}

Page 16: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Trees in languages

There are no general tree classes in the standard libraries in many languages: e.g. C++ STL, Ruby, Python, etc. No single interface tree design satisfies all

users. There may be specialized trees, especially

for GUI design. E.g. JTree in Java FXTreeList in Ruby

Page 17: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Tree Traversal

Traversing is the systematic way of accessing, or ‘visiting’ all the nodes in a tree.

Consider the three operations on a binary tree: V: Visit a node L: (Recursively) traverse the left

subtree of a node R: (Recursively) traverse the

right subtree of a node

Page 18: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Tree Traversing

We can traverse a tree in six different orders:

LVRVLR

LRVVRLRVLRLV

Page 19: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Preorder Traversal A traversal visits the nodes of

a tree in a systematic manner In a preorder traversal, a node

is visited before its descendants

Application: print a structured document

Make Money Fast!

1. Motivations References2. Methods

2.1 StockFraud

2.2 PonziScheme

1.1 Greed 1.2 Avidity2.3 BankRobbery

1

2

3

5

4 6 7 8

9

Algorithm preOrder(v)visit(v)for each child w of v

preorder (w)

Page 20: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Java Implementation

public static void preorder(TreeNode root) {if (root != null) {

visit(root); // assume declared.Enumernation enum = root.children();while (enum.hasMoreElement()) {

preorder((TreeNode) enum.nextElement());

}}

Page 21: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Postorder Traversal In a postorder traversal, a

node is visited after its descendants

Application: compute space used by files in a directory and its subdirectories

Algorithm postOrder(v)for each child w of v

postOrder (w)visit(v)

cs16/

homeworks/todo.txt

1Kprograms/

DDR.java10K

Stocks.java25K

h1c.doc3K

h1nc.doc2K

Robot.java20K

9

3

1

7

2 4 5 6

8

Page 22: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Inorder Traversal

For binary tree, inorder traversal can also be defined: L V R.

Page 23: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Java Implementation

public static void inorder(TreeNode root) {if (root != null) {

inorder(root.getChildAt(0));visit(root); // assume declared.inorder(root.getChildAt(1));

}

Page 24: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Expression Notation

There are three common notations for expressions: Prefix: operator come before operands. Postfix: operator come after operands. Infix:

Binary operator come between operands. Unary operator come before the operand.

Page 25: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Examples

Infix: (a+b)*(c-d/f/(g+h)) Used by human being.

Prefix: *+ab-c//df+gh E.g. functional language such as Lisp: Lisp: (* (+ a b) (- c (/ d (/ f (+ g h)))))

Postfix: ab+cdf/gh+/-* E.g. Postfix calculator

Page 26: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Expression Trees

An expression tree can be used to represent an expression. Operator: root Operands: child sub-trees Variable and constants: leaf nodes.

Page 27: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Traversals of Expression Trees

Preorder traversal => prefix notation.

Postorder traversal => postfix notation.

Inorder traversal with parenthesis added => infix notation.

Page 28: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Linked Structure for Trees A node is represented

by an object storing Element Parent node Sequence of children

nodes Node objects implement

the Position ADT

B

DA

C E

F

B

A D F

C

E

Page 29: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Tree Classes in Ruby

FXTreeItem: a tree node for GUI application.

Some methods: Tree related: childOf?, parentOf?,

numChildren, parent, etc. Traversal of child nodes: first, next,

last, etc. GUI related: selected?, selected=,

hasFocus?, etc.

Page 30: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Tree Classes in Ruby 2

FXTreeList: a list of FXTreeItem. Include methods for list

manipulation, such as appendItem, clearItems, etc.

Page 31: Trees by Dr. Bun Yue Professor of Computer Science yue@uhcl.edu  2013 yue@uhcl.edu  CSCI 3333 Data Structures

Questions and Comments?