44
Chapter 9 Binary Trees

Chapter 9

  • Upload
    urian

  • View
    28

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 9. Binary Trees. A binary tree t is either empty or consists of an element, called the root element , and two distinct binary trees, called the left subtree and right subtree of t. External Path Length. Traversals of a Binary Tree. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 9

Chapter 9

Binary Trees

Page 2: Chapter 9

Here are some binary trees we will be studying in the next few chapters Binary Tree Binary Expression Heap Decision Huffman Search Tree Tree Tree Tree AVL Red-Black Tree Tree

Page 3: Chapter 9

A binary tree t is either empty or consists of an element, called the root element, and two distinct binary trees, called the left subtree and right subtree of t.

Define: leaf , branch , parent, ancestor, descendant, siblings

Page 4: Chapter 9

+

– / X Y Z * A B

This is an expression tree: Each leaf is an operand, and each non-leaf is a binary operator.

Page 5: Chapter 9

50

30 90

12 40 100

94

This is a binary search tree: Each element in the left subtreeis less than the root element, each element in the rightsubtree is greater than the root element, and the left andright subtrees are themselves binary search trees.

Page 6: Chapter 9

Suppose a binary tree t is a chain – that is, each element except the only leaf has exactly one branch. If t has n elements, how many branches are there from the root, the only leaf?

How can we define leaves(t), the number of leaves in a binary tree t? Recursively!

Page 7: Chapter 9

if t is empty leaves(t) = 0 else if t consists of a root element only leaves(t) = 1 else leaves(t) = leaves(leftTree(t)) +

leaves(rightTree(t)) Similiarly, define n(t) i.e the number of elements

Page 8: Chapter 9

A path in a binary tree is a sequence ofelements in which each element exceptthe last is the parent of the next elementin the sequence.

Page 9: Chapter 9

50

30 80

62 90

84

Determine the path from 50 to 84.

Page 10: Chapter 9

In a binary tree t, height(t) is the number of branches from the root to the farthest leaf. 50

30 80

62 90 84

Determine height(t); Define height(t) recursively

Page 11: Chapter 9

depth(x), the depth of an element x is the numberof branches from the root element to x.

If x is the root elementdepth(x) = 0

Else depth(x) =

level(x), the level of x, is the same as the depth of x.

Page 12: Chapter 9

In the following binary tree, what is depth(62)? level(90)? The height of the subtree rooted at 90? 50

30 80

62 90 84

Page 13: Chapter 9

A binary tree t is a two-tree if t is emptyor if each non-leaf in t has two branches.

An example of a two-tree: A B C D E F G

Page 14: Chapter 9

Is this a two-tree?

A

B C

D E F

G H I J K L

Page 15: Chapter 9

A binary tree t is a two-tree if t is empty or if leftTree(t) and rightTree(t) are either both empty or both non-empty two-trees.

A binary tree t is full if t is a two-tree andall of the leaves of t have the same depth.

Page 16: Chapter 9

A full binary tree:

A

B C

D E F G

H I J K L M N O

Page 17: Chapter 9

Recursively speaking: A binary tree t is full if t is empty or if height(leftTree(t)) = height(rightTree(t)) and both leftTree(t) and rightTree(t) are …?

Page 18: Chapter 9

A binary tree t is complete if t is fullthrough a depth of height(t) - 1, andeach leaf whose depth is height(t) is as farto the left as possible.

Page 19: Chapter 9

A complete binary tree: A B C D E R S F G L

Page 20: Chapter 9

A binary tree that is not complete: A B C D E R F G L Q B G

Page 21: Chapter 9

The binary-tree theorem:

For any non-empty binary tree t:

n(t) + 11. leaves(t) <= 2.0

n(t) + 12. <= 2height(t)

2.0

Page 22: Chapter 9

3. Equality holds in part 1 if and only if tis a two-tree.

4. Equality holds in part 2 if and only if tis full.

Page 23: Chapter 9

What is the significance of the binary treetheorem? Suppose t is full. Then

n(t) + 1 = 2 height(t)

2.0

so n(t) + 1height(t) = log2 ( ) 2.0

Page 24: Chapter 9

That is, a full tree has height that islogarithmic in n.

The height of a complete binary tree is alsologarithmic in n.

What about the height of a chain?

Page 25: Chapter 9

In Chapter 10, we will look at a special kindof binary tree: The binary search tree.

Page 26: Chapter 9

50 30 90 Binary Search Tree 12 40 86 100

25 60 71

Page 27: Chapter 9

In a binary search tree, each element in the left subtree is less than the root element, each element in the right subtree is greater than the root element, and the left and right subtrees are …?

Page 28: Chapter 9

Let t be a non-empty binary tree. E(t), theexternal path length of t, is the sum of thedepths of all leaves in t.

External Path Length

Page 29: Chapter 9

For example, find the external path lengthof the following binary tree:

A

B C

D E R S

F G L

Page 30: Chapter 9

The external path length theorem:

Let t be a binary tree with k > 0 leaves. Then

E(t) >= (k / 2) floor (log2k).

Page 31: Chapter 9

A traversal of a binary tree is an algorithmthat accesses each item in the binary tree.

Traversals of a Binary Tree

Page 32: Chapter 9

1. inOrder(t) {

if (t is not empty) { inOrder(leftTree(t)); access the root element of t; inOrder(rightTree(t)); } // if } // inOrder traversal Left – Root – Right

Page 33: Chapter 9

50

30 90

12 40 86 100

Determine the order in which the elementswould be accessed during an in-ordertraversal.

Page 34: Chapter 9

Answer: 12, 30, 40, 50, 86, 90, 100

Page 35: Chapter 9

2. postOrder (t) {

if (t is not empty) { postOrder(leftTree(t)); postOrder(rightTree(t)); access the root element of t; } // if } // postOrder traversal Left – Right – Root

Page 36: Chapter 9

Determine the order in which the elements would be accessed during a post-order traversal. Hint: An operator immediately follows its operands. +

– / X Y Z * A B

Page 37: Chapter 9

Answer: X, Y, -, Z, A, B, *, / +

Postfix!

Page 38: Chapter 9

3. preOrder (t) {

if (t is not empty) { access the root element of t; preOrder (leftTree (t)); preOrder (rightTree (t)); } // if } // preOrder traversal

Root – Left – Right

Page 39: Chapter 9

Determine the order in which the elements would be accessed during a pre-order traversal. Hint: An operator immediately precedes its operands. +

– / X Y Z * A B

Page 40: Chapter 9

Answer: +, -, X, Y, /, Z, *, A, B

Prefix!

Page 41: Chapter 9

4. To perform a breadth-first traversal of a non-empty binary tree, first access the root element, then the children of the root element, from left to right, then the grandchildren of the root element, from left to right, and so on.

Page 42: Chapter 9

Perform a breadth-first traversal of the following binary tree. A B C D E R S F G L

Page 43: Chapter 9

Answer: A, B, C, D, E, R, S, F, G, L

Page 44: Chapter 9

Perform the other traversals of that tree:

inOrder (Left-Root-Right)postOrder (Left - Right - Root)preOrder (Root - Left - Right)

A

B C

D E R S

F G L