trees

39
Data Structures and Algor ithms 1 Trees The definitions for this presentation are from from: Corman , et. al., Introduction to Algorithms (MIT Press), Chapter 5. Some material on binomial trees is from Hull.

Upload: carys

Post on 21-Jan-2016

61 views

Category:

Documents


0 download

DESCRIPTION

Trees The definitions for this presentation are from from: Corman , et. al., Introduction to Algorithms (MIT Press), Chapter 5. Some material on binomial trees is from Hull. A Few Applications. Arithmetic Expressions b + a * b. +. b. *. b. a. A Few Applications. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Trees

Data Structures and Algorithms 1

Trees

The definitions for this presentation are from from: Corman , et. al., Introduction to Algorithms (MIT Press), Chapter 5.Some material on binomial trees is from Hull.

Page 2: Trees

Data Structures and Algorithms 2

A Few Applications

Arithmetic Expressions

b + a * b+

b *

a b

Page 3: Trees

Data Structures and Algorithms 3

A Few Applications

<employee>

<name> <ssn> title

#text #text #text

XML Document Object Model

Page 4: Trees

From Hull 4

A Few Applications

Binomial “Trees” Movements in Time t

Su

Sd

S

p

1 – p

Binomial trees are frequently used to approximate the movements in the price of a stock or other assetIn each small interval of time the stock price is assumed to move up by a proportional amount u or to move down by a proportional amount d.

Page 5: Trees

Data Structures and Algorithms 5

Definitions

A Free tree is a connected, acyclic undirected graph.

Page 6: Trees

Data Structures and Algorithms 6

If an undirected graph is acyclic but possibly disconnected,it is a forest.

Page 7: Trees

Data Structures and Algorithms 7

This is a graph that contains a cycle and is therefore neither atree nor a forest.

Page 8: Trees

Data Structures and Algorithms 8

Theorem (Properties of Free Trees)

Let G = (V, E) be an undirected graph.The following statements are equivalent:

1. G is a free tree.

Page 9: Trees

Data Structures and Algorithms 9

2. Any two vertices in G are connected by a unique simple path.

Page 10: Trees

Data Structures and Algorithms 10

3. G is connected, but if any edge is removed from E, the resulting graph is disconnected.

Page 11: Trees

Data Structures and Algorithms 11

4. G is connected, and |E| = |V| - 1.

Page 12: Trees

Data Structures and Algorithms 12

5. G is acyclic, and |E| = |V| - 1.

Page 13: Trees

Data Structures and Algorithms 13

6. G is acyclic, but if any edge is added to E, the resulting graph contains a cycle.

Page 14: Trees

Data Structures and Algorithms 14

A rooted tree is a free tree in which one of the vertices isdistinguished from the others. The distinguished vertex is called the root of the tree. We often refer to a vertex of arooted tree as a node (we may also call this a vertex) of thetree. The following figure shows a rooted tree on a set of 12nodes with root 7.

7

3 10 4

8 12 211

9

6 5 1

Page 15: Trees

Data Structures and Algorithms 15

Consider a node x in a rooted tree T with root r. Anynode y on the unique path from r to x is called an ancestorof x. If y is an ancestor of x, then x is a descendant of y.(Every node is both an ancestor and a descendant of itself.)If y is an ancestor of x and x y, then y is a proper ancestorof x and x is a proper descendant of y. The subtree rootedat x is the tree induced by descendants of x, rooted at x.

8

6 5

9

r

x

y

Page 16: Trees

Data Structures and Algorithms 16

If the last edge on the path from the root r of a tree T to anode x is (y, x), then y is the parent of x, and x is a child ofy. The root is the only node in T with no parent. If twonodes have the same parent, they are siblings. A node withno children is an external node or leaf. A nonleaf node isan internal node.

Page 17: Trees

Data Structures and Algorithms 17

The number of children of a node x in a rooted tree T is called the degree of x. The length of the path from the root r to a node x is the depth of x in T. The largest depth of any node in T is the height of T.

7

3 10 4

8 12 211

9

6 5 1

depth 0

depth 1

depth 2

depth 3

depth 4

height = 4

Page 18: Trees

Data Structures and Algorithms 18

An ordered tree is a rooted tree in which the children of each node are ordered. That is, if a node has k children,then there is a first child, a second child, …, and a kthchild. The two trees shown below are different whenconsidered to be ordered trees, but the same when considered to be just rooted trees.

7

3 10 4

8 12 211

9

6 5 1

7

561

211812

4103

9

Page 19: Trees

Data Structures and Algorithms 19

A binary tree T is a structure defined on a finite set ofnodes that either

• contains no nodes, or• is comprised of three disjoint sets of nodes:

a root node, a binary tree called its left subtreeand a binary tree called its right subtree.

32 7

4 1 5

6

Page 20: Trees

Data Structures and Algorithms 20

Full binary tree: each node is either a leaf or has degreeexactly 2.

In a positional tree, the children of a node are labeled with distinct positive integers. The ith child of a node isabsent if no child is labeled with integer i. A k-ary treeis a positional tree in which for every node, all childrenwith labels greater than k are missing. Thus, a binary tree is a k-ary tree with k = 2.

Page 21: Trees

Data Structures and Algorithms 21

A complete k-ary tree is a k-ary tree in which all leaveshave the same depth and all internal nodes have degreek.

height = 3

depth 0

depth 1

depth 2

depth 3

Page 22: Trees

Data Structures and Algorithms 22

How many leaves L does a complete binary tree of height hhave?

d = 0

d = 1

d = 2

The number of leaves at depth d = 2d

If the height of the tree is h it has 2h leaves.

L = 2h.

Page 23: Trees

Data Structures and Algorithms 23

What is the height h of a complete binary tree with L leaves?

leaves = 1 height = 0

leaves = 2 height = 1

leaves = 4 height = 2

leaves = L height = Log2L

Since L = 2h

Log2L = Log22h

h = Log2L

Page 24: Trees

Data Structures and Algorithms 24

The number of internal nodes of a complete binary tree of height h is ?

Internal nodes = 0 height = 0

Internal nodes = 1 height = 1

Internal nodes = 1 + 2 height = 2

Internal nodes = 1 + 2 + 4 height = 3

1 + 2 + 22 + . . . + 2 h-1 = 2i

= 2h - 1 2 - 1

Thus, a complete binary tree of height = h has 2h-1 internalnodes.

Geometric series

Page 25: Trees

Data Structures and Algorithms 25

The number of nodes n of a complete binarytree of height h is ?

nodes = 1 height = 0

nodes = 3 height = 1

nodes = 7 height = 2

nodes = 2h+1- 1 height = h

Since L = 2h

and since the number of internal nodes = 2h-1 the total number of nodes n = 2h+ 2h-1 = 2(2h) – 1 = 2h+1- 1.

Page 26: Trees

Data Structures and Algorithms 26

If the number of nodes is n then what is the height?

nodes = 1 height = 0

nodes = 3 height = 1

nodes = 7 height = 2

nodes = n height = Log2(n+1) - 1

Since n = 2h+1-1n + 1 = 2h+1

Log2(n+1) = Log2 2h+1

Log2(n+1) = h+1h = Log2(n+1) - 1

Page 27: Trees

Data Structures and Algorithms 27

n

n

n

2

1

1= 1 (2n)! (n+1) n! (2n-n)!

Catalan Numbers

1,1,2,5,14,...

Page 28: Trees

Data Structures and Algorithms 28

The number of distinct binary trees with n nodes

n

n

n

2

1

1N = 1 N=2

...

N=3N = 0

Page 29: Trees

Data Structures and Algorithms 29

Class for Binary Nodes

public class BTNode { private Object data; private BTNode left; private BTNode right; ...

Page 30: Trees

Data Structures and Algorithms 30

public BTNode(Object obj, BTNode l, BTNode r) { data = obj; left = l; right= r; } public boolean isLeaf() { return (left == null) && (right == null); } ...

Page 31: Trees

Data Structures and Algorithms 31

Copying Trees public static BTNode treeCopy(BTNode t) { if (t == null) return null; else { BTNode leftCopy = treeCopy(t.left); BTNode rightCopy = treeCopy(t.right); return new BTNode(t.data, leftCopy, rightCopy); } }

Page 32: Trees

Data Structures and Algorithms 32

Tree Traversals

•Preorder•Inorder•Postorder•Levelorder

Page 33: Trees

Data Structures and Algorithms 33

public void preOrderPrint(){System.out.println(data);if (left != null)

left.preOrderPrint();if (right != null)

right.preOrderPrint();}

a b c d e f g a

b e

c d f g

Root, Left, Right

Page 34: Trees

Data Structures and Algorithms 34

public void inOrderPrint(){if (left != null) {

left.inOrderPrint()System.out.println(data);if (right != null)

right.inOrderPrint()}

c b d a f e g a

b e

c d f gLeft, Root, Right

Page 35: Trees

Data Structures and Algorithms 35

public void postOrder(){if (left != null

left.postOrder()if (right != null)

right.postOrder()System.out.println(data);

}

c d b f g e a

a

b e

c d f g

Left, right, root

Page 36: Trees

Data Structures and Algorithms 36

levelorder (T) { Q = makeEmptyQueue()

enqueue (T,Q)until isempty (Q) {

p = dequeue(Q)visit (p)for each child of P, in order, do

enqueue (, Q)}

}

a b e c d f g

a

b e

c d f g

Page 37: Trees

Data Structures and Algorithms 37

An Array Representation

Suppose we are lucky enough to be working with complete binary trees.

We can store the tree in an array.

Let the root be at index 0 and let the left and right childrenof node i be at indexes 2i+1 and 2i+2 respectively.

Lewis and Denemberg, Page 111

Page 38: Trees

Data Structures and Algorithms 38

An Array Representation

isLeaf(i) : 2i + 1 >= nleftChild(i) : 2i + 1 (none if 2i+ 1 >= n)rightChild(i): 2i + 2 (none if 2i + 2 >= n)leftSibling(i): i - 1 (none if i == 0 or i is odd)rightSibling(i) : i + 1 (none if i = n-1 or i is even) parent(i) = int((i-1)/2) (none if i == 0)

Lewis and Denemberg, Page 111

Works if the tree is “almost complete”, growing top to bottomand left to right.

Page 39: Trees

Data Structures and Algorithms 39

Binomial “Trees” Using an Array Representation

S0

S0u

S0d S0 S0

S0u 2

S0d 2

S0u 2

S0u 3 S0u 4

S0d 2

S0u

S0d

S0d 4

S0d 3 The array element bt[0] will be S0.In general, given a node with index i at depth d, its left child is located at bt[i + d+1] and its right child is located at bt[i + d+2]