lecture11: tree i bohyung han cse, postech [email protected] csed233: data structures (2014f)

22
Lecture11: Tree I Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

Upload: amice-mckenzie

Post on 04-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

Lecture11: Tree I

Bohyung HanCSE, POSTECH

[email protected]

CSED233: Data Structures (2014F)

Page 2: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

2 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

A Hierarchical Structure

• Unix/Linux file systems

Page 3: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

3 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Data Structures

• An abstract model of a hierarchical structure “2 dimensional” structure‐ Easy access (similar to array) Easy insert and removal (similar to linked list)

Computers”R”Us

Sales

R&D

Manufactur-ing

Lap-tops

Desk-tops

US

Interna-tional

Eu-rope

Asia

Canada

Page 4: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

4 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Data Structures

• An abstract model of a hierarchical structure Trees consist of nodes and links denoting parent child relation‐ Special case of graphs without loops Each element (except root) has a parent and zero or more children.

Computers”R”Us

Sales

R&D

Manufactur-ing

Lap-tops

Desk-tops

US

Interna-tional

Eu-rope

Asia

Canada

Page 5: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

5 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Terminology

• Root: node without parent• Internal node: node with at least

one child • Leaf: node without children

• Subtree: tree consisting of a node and its descendants

• Ancestors of a node: parent, grandparent, grand-grandparent, …• Descendant of a node: child, grandchild, grand-grandchild, …• Depth (level) of a node: number of ancestors• Height of a tree: maximum depth of any node

A

B DC

G HE F

I J K

A

A,B,C,F

E,I,J,K,G,H,D

Subtree

3

Page 6: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

6 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Abstract Data Type

• Data structure Node: data element and link to children Root: a special node

• List of methodsmethod description

root() Return the tree’s root; error if tree is empty

parent(v) Return v’s parent; error if v is a root

children(v) Return v’s children (an iterable collection of nodes)

isRoot(v) Test whether v is a root

isExternal(v)

Test whether v is an external node

isInternal(v)

Test whether v is an internal node

isEmpty() Test whether the tree has any nodes or not

size() Return the number of nodes in the tree

Page 7: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

7 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

A Linked Structure for Trees

• A node is represented by an object storing Element Parent node Sequence of children nodes

Element

Parent node

A list of children

A child node

Page 8: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

8 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

A Linked Structure for Trees

B

DA

C E

F

B

A D F

C

E

Page 9: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

9 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Binary Tree

• Definition A tree in which each node has at most two children Each child is either the left child or the right child of its parent.

Left child Right child

Page 10: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

10 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Arithmetic Expression Tree

• Binary tree associated with an arithmetic expression Internal nodes: operators External nodes: operands

• Example: arithmetic expression tree for the expression:

+

÷

−2

a 1

3 b

𝟐÷ (𝒂−𝟏 )+(𝟑×𝒃 )

Page 11: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

11 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Decision Tree

• Binary tree associated with a decision process Internal nodes: questions with yes/no answer External nodes: decisions

• Example: dining decision

Want a fast meal?

On a diet?

On expense ac-count?

Subway McDonald’s ???????? ????????

Yes No

Yes No Yes No

Page 12: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

12 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

A Linked Structure for Binary Trees

• A node is represented by an object storing Element Parent node Left child node Right child node

Element

Parent node

Left child Right child

Page 13: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

13 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

A Linked Structure for Binary Trees

B

A D

C E

B

DA

C E

Page 14: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

14 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Binary Tree Implementation

public class Node{ int value; // data stored at node Node left; // pointer to left child Node right; // pointer to right child

public Node(int i) { value = i; left = null; right = null; }

public Node getLeftChild(Node node) { return left; } public Node getRightChild(Node node) { return right; } public void setLeftChild(Node node) { left = node; } public void setRightChild(Node node) { right = node; }}

Page 15: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

15 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Binary Tree Implementation

public class BinaryTree{ Node root;

public Tree() { root = null; }

// insert an element to the current tree public void insert(int i) { ... }

// deleted a node from the current tree public Node delete(int i) { ... }

// find a node containing a particular value public Node find(int i) { ... }}

Page 16: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

16 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

An Array Based Representation‐

• Basic idea Each node has up to 2 children and can be mapped with a particular

location in an array.

• Implementation Nodes are stored in an array . Node is stored at

• Left in even: if a node is the left child of a parent node,

• Right in odd: if node is the right child of a parent node,

is always empty is empty if there is no node in the -th position

Page 17: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

17 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

An Array Based Representation‐

1 2 3 10 11

1

2 3

6 74 5

10 11

A

B D

E F C J

G H

A B D E F C J G H

4 5 6 7

Page 18: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

18 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Balance

• Perfect binary tree A binary tree of height h is perfect if every node has exactly two

children and all leaf nodes have the same level. Perfect binary tree is sometimes called full binary tree.

Page 19: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

19 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Balance

• Complete binary tree A binary tree is complete if it is full to level h 1 and level h is filled ‐

from the left with contiguous nodes.

Page 20: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

20 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Balance

• Proper binary tree A binary tree in which each node has exactly zero or two children Proper binary tree is sometimes called full binary tree.

Page 21: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

21 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Proper Binary Trees

• Each internal node has exactly 2 children.• Properties

: number of total nodes : number of external nodes : number of internal nodes : height (maximum depth of a node)

Page 22: Lecture11: Tree I Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

22