24
Mathematical Binary Tree Notation 24 February 2013 OSU CSE 1

Mathematical Binary Tree Notationweb.cse.ohio-state.edu/.../extras/slides/10.Binary-Tree-Theory.pdf · Binary Tree Theory • Another mathematical model that we will use is that of

Embed Size (px)

Citation preview

Mathematical Binary Tree Notation

24 February 2013 OSU CSE 1

Binary Tree Theory

•  Another mathematical model that we will use is that of binary trees

•  A binary tree can be thought of as a structure comprising zero or more nodes, each with a label of some mathematical type, say T, called the label type

•  We call this math type binary tree of T

24 February 2013 OSU CSE 2

Recursive Structure •  A binary tree (type binary tree of T) is

either –  the empty tree (empty_tree), which has no nodes

at all; or –  a non-empty tree, which consists of:

•  A root node (type T) •  Two subtrees of the root, each of which is a binary tree (type binary tree of T)

•  Since a non-empty binary tree contains two other binary trees (each of which may contain others), its structure is recursive

24 February 2013 OSU CSE 3

Recursive Structure •  A binary tree (type binary tree of T) is

either –  the empty tree (empty_tree), which has no nodes

at all; or –  a non-empty tree, which consists of:

•  A root node (type T) •  Two subtrees of the root, each of which is a binary tree (type binary tree of T)

•  Since a non-empty binary tree contains two other binary trees (each of which may contain others), its structure is recursive

24 February 2013 OSU CSE 4

Previously, with general trees and XMLTree, we downplayed the recursive structure. Not here!

The Empty Tree

•  The empty binary tree is denoted by empty_tree and is shown in our diagrams as simply the absence of a tree where there could be one, or (when it’s not completely clear from context) as

24 February 2013 OSU CSE 5

Non-Empty Trees

•  Every non-empty tree is the result of the mathematical function compose applied to a value of the label type T and two other values of type binary tree of T, which are the root and the left and right subtrees of the resulting binary tree

•  In the diagrams that follow, T is integer

24 February 2013 OSU CSE 6

The compose Function

24 February 2013 OSU CSE 7

x, t1, t2 compose(x, t1, t2)

x = 42

t1 =

t2 =

42

The compose Function

24 February 2013 OSU CSE 8

x, t1, t2 compose(x, t1, t2)

x = 42

t1 =

t2 =

42

Diagrams use color triangles to stand for any non-empty tree.

The compose Function

24 February 2013 OSU CSE 9

x, t1, t2 compose(x, t1, t2)

x = 42

t1 =

t2 =

42

root

left subtree

right subtree

The compose Function

24 February 2013 OSU CSE 10

x, t1, t2 compose(x, t1, t2)

x = 42

t1 =

t2 =

42

The compose Function

24 February 2013 OSU CSE 11

x, t1, t2 compose(x, t1, t2)

x = 42

t1 =

t2 =

42

The compose Function

24 February 2013 OSU CSE 12

x, t1, t2 compose(x, t1, t2)

x = 42

t1 =

t2 =

42

Size, Height, and Labels •  The size of t, i.e., the total number of

nodes in t, is denoted by |t| •  The height of t, i.e., the number of nodes

on the longest path from the root to an empty subtree of t, is denoted by ht(t)

•  The labels of t, i.e., the set whose elements are exactly the labels of t (i.e., the tree’s labels without duplicates and ignoring order) is denoted by labels(t)

24 February 2013 OSU CSE 13

Size

24 February 2013 OSU CSE 14

t |t|

0

1 + |ls| + |rs|

13

ls rs

Height

24 February 2013 OSU CSE 15

t ht(t)

0

1 + max(ht(ls), ht(rs))

13

ls rs

Labels

24 February 2013 OSU CSE 16

t labels(t)

{ }

{13} union labels(ls) union

labels(rs)

13

ls rs

What Is This Tree’s Mathematical Value?

24 February 2013 OSU CSE 17

1 3

2

4

5

24 February 2013 OSU CSE 18

1 3

2

4

5

Note: Its size is 5.

Its height is 3.

24 February 2013 OSU CSE 19

1 3

2

4

5

compose(4, , )

24 February 2013 OSU CSE 20

1 3

2

4

5

compose(2, , )

24 February 2013 OSU CSE 21

1 3

2

4

5

compose(1, , )

24 February 2013 OSU CSE 22

1 3

2

4

5

compose(3, , )

24 February 2013 OSU CSE 23

1 3

2

4

5

compose(5, , )

Its Mathematical Value Must Be...

compose(4, compose(2, compose(1, empty_tree, empty_tree), compose(3, empty_tree, empty_tree)), compose(5, empty_tree, empty_tree)))

24 February 2013 OSU CSE 24

1 3

2

4

5