10. binary trees

24
1 10. Binary Trees Read Sec. 10.1- 10.4 A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set loc = 0; 2. Repeat the following: a. If loc >= length of list Return –1 to indicate item not found. b. If list element at location loc is item Return loc as location of item c. Increment loc by 1. Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find() algorithm in STL.) For a list of length n, its average search

Upload: tanith

Post on 31-Jan-2016

37 views

Category:

Documents


0 download

DESCRIPTION

Read Sec. 10.1-10.4. 10. Binary Trees. A. Introduction: Searching a linked list. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 10. Binary Trees

1

10. Binary Trees

Read Sec. 10.1-10.4

A. Introduction: Searching a linked list.

1. Linear Search/* Linear search a list for a particular item */1. Set loc = 0;2. Repeat the following:

a. If loc >= length of list Return –1 to indicate item not found.

b. If list element at location loc is itemReturn loc as location of item

c. Increment loc by 1.

Linear search can be used for lists stored in an array as well as forlinked lists. (It's the method used in the find() algorithm in STL.)For a list of length n, its average search time will be O(n).

Page 2: 10. Binary Trees

2

2. Binary Search

Ordered lists can be searched more efficiently using binary search:

/* Binary search an ordered list for a particular item */1. Set start = 0 and last = length of list – 1.2. Repeat the following:

a. If start > last Return –1 to indicate item not found.

b. Find the middle element in the sublist from locations start through last and its location mid.c. If item < the list element at mid

Set last = mid – 1. // Search first half of list Else if item > the list element at Loc

Set start = mid + 1. // Search last half of list Else

Return mid as location of item

Page 3: 10. Binary Trees

3

Since the size of the list being searched is reduced by approximately 1/2 on each pass through the loop, the number of times the loop will be executed is O(log2n).

It would seem therefore that binary search is much more efficient than linear search.

This is true for lists stored in arrays in which step 2b can be done simply by calculating mid = (start + last ) / 2 and array[mid] is the middle list element. (It's the method used in the binary_search() algorithm in STL.)

Page 4: 10. Binary Trees

4

As we have seen, for linked lists, binary search is not practical, because we only have direct access to the first node, and locating any other node requires traversing the list until that node is located. Thus step 2b requires:

i. mid = (start + last) / 2 ii. Set locPtr = first; // location of first node

iii. For loc = start to mid - 1 Set locPtr = next part of node pointed to by locPtr. iv. locPtr points to the middle node and the data part of the node

pointed to locPtr is the middle list element.

Locating the middle node clearly negates the efficiency of binary search for array-based lists; the computing time becomes O(n) instead of O(log2n).

However, perhaps we could modify the linked structure to make a binary search feasible. What would we need?

Page 5: 10. Binary Trees

5

Direct access to the middle node:

22 33 44 55 66 77 88

and from it to the middle of the first half and to the middle of the second half:

22 33 44 55 66 77 88

22 33 44 55 66 77 88

and so on:

Page 6: 10. Binary Trees

6

Or if we stretch out the links to give it a tree-like shape

That is, use a binary search tree (BST).

55

33 77

22 44 66 88

Page 7: 10. Binary Trees

7

B. Binary Search Trees

1. Definition and Terminology:Tree: A finite set of elements called nodes (or vertices) and a finite set of directed arcs that connect pairs of nodes.

If the tree is not empty, one of the nodes, called the root, has no incoming arcs,

Nodes directly accessible (usingone arc) from a node are called the children of that node, which is called the parent of these children; these nodes are siblings of each other.

unique

Leaf: Node with no outgoing arcs

but every other node in the tree can be reached from the root by a path (a sequence of consecutive arcs).

Page 8: 10. Binary Trees

8

root

8 9

5 6 7

3

4

2

1

children of this parentsiblings of each other

leaves

Page 9: 10. Binary Trees

9

Morse code trees

2. Examples Game trees

I A N M

TE

• •

• –

– –

Huffman codes -- data compression

Decision trees

Page 10: 10. Binary Trees

10

Parse trees

In STL: red-black trees for associative containers

Page 11: 10. Binary Trees

11

3. Def: A binary tree is a tree in which each node has at most 2 children.

store node #0 in array location 0, node #1 in array location 1, etc.

i 0 1 2 3 4 5 6 . . .

t [i ] O M T C E P U . . .

C E P U

TM

••

O

21

3 4 5 6

0

Every collection of trees can be represented by a binary tree.

4. Array-Based Implementation:An array can be used to store some binary trees. Just number the nodes level by level, from left to right,

Page 12: 10. Binary Trees

12

But, unless each level of the tree is full so there are no "dangling limbs," there can be much wasted space in the array.

For example, this binary tree contains the same characters as before but requires ___ array positions for storage:

MC

E

U

T

P

O

it[i]

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

20 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

21

41

0

……

58

Max # nodes on level i:2i

In array representation,children of i are at: 2i + 1, 2i + 2

Parent of i is at: (i - 1) / 2

E C M U T

O

P

Page 13: 10. Binary Trees

13

5. Linked Implementation:Use nodes of the form

and maintain a pointer to the root.

a. Example:

data

left

Left child Right child

right

BST<int> b;

b operations

8060

75

6558 92

root

75

60

58 65

80

92

Page 14: 10. Binary Trees

14

b. C++ Implementation:

template <typename BinTreeElement>class BinaryTree{ public: // ... BinaryTree function members private: class BinNode // a binary tree node { public: BinTreeElement data; BinNode * left, * right; // ... BinNode member functions }; typedef BinNode * BinNodePointer; // BinaryTree data members BinNodePointer root; // pointer to root node};

Page 15: 10. Binary Trees

15

5. A Binary Search Tree (BST) is a binary tree in which the value in each node is greater than all values in its left subtree and less than all values in its right subtree.

a. We can "binary search" a BST:

1. Set pointer locPtr = root.2. Repeat the following:

If locPtr is nullReturn false

If value < locPtr->datalocPtr = locPtr->left

Else if value > locPtr->datalocPtr = locPtr->right

ElseReturn true

Search time: O(log2n) if tree is balanced.

Page 16: 10. Binary Trees

16

b. What about traversing a binary tree?

Most easily done recursively, viewing a binary tree as a recursive data structure:

Recursive definition of a binary tree:

A binary tree either:

i. is empty Anchor

or

ii. consists of a node called the root, which haspointers to two disjoint binary subtrees Inductive stepcalled the left subtree and the right subtree.

Page 17: 10. Binary Trees

17

Now, for traversal, consider the three operations:

V: Visit a nodeL: (Recursively) traverse the left subtree of a nodeR: (Recursively) traverse the right subtree of a node

We can do these in six different orders:

LVRVLRLRVVRLRVLRLV

Page 18: 10. Binary Trees

18

As a member function in a BinaryTree class:

void Inorder() { InorderAux(root); }

void InorderAux(BinNodePointer r){ if (r != 0) { InorderAux(r->left); // L Process(r->data); // V InorderAux(r->right); // R

}}

Rearranging the steps L, V, and R gives the other traversals.

For example, LVR gives the following traversal algorithm:If the binary tree is empty // anchor

Do nothing.Else do the following: // inductive step

L: Call traversal to traverse the left subtree.V: Visit the root.R: Call traversal to traverse the right subtree.

Page 19: 10. Binary Trees

19

58, 60, 65, 75, 80, 92

75, 60, 58, 65, 80, 92

58, 65, 60, 92, 80, 75

8060

75

6558 92

The first three orders, in which the left subtree is traversed before the right, are the most important of the six traversals and are commonly called by other names:

LVR InorderVLR PreorderLRV Postorder

Note: Inorder traversal of a BST visits the nodesin ascending order.

LVR:

VLR:

LRV:

Page 20: 10. Binary Trees

20

To see why these names are appropriate, recall expression trees, binary trees used to represent the arithmetic expressions like a + b * c / d:

infix expression: a + b * c / d

prefix expression: / + a * b c d

postfix expression: a b c * + d /

a *

d+

/

b c

Inorder traversal

Preorder traversal

Postorder traversal

Page 21: 10. Binary Trees

21

c. So how do we insert in a binary tree so it grows into a BST?

Modify the search algorithm so that a pointer parentPtr trails locPtr down the tree, keeping track of the parent of each node being checked:

1. Initialize pointers locPtr = root, parentPtr = null pointer.2. While locPtr ≠ null pointer:

a. parentPtr = locPtrb. If value < locPtr->data

locPtr = locPtr->leftElse if value > locPtr->data

locPtr = locPtr->rightElse

value is already in the tree; return a found indicator3. Get a new node pointed to by newPtr, put the value in its

data part, and set left and right to null.4. If parentPtr = null pointer // empty tree

Set root = newptr.Else if value < parentPtr->data

Set parentPtr->left = newPtr.Else

Set parentPtr->right = newPtr.

Page 22: 10. Binary Trees

22

Examples:Insert in the order given: Insert in the order given: Insert in the order given:

M, O, T, H, E, R T, H, E, R, M, O E, H, M, O, R, T

M

O

T

H

E

R

T

O

H

E

M

R

E

H

M

O

R

T

M

O

T

H

E

R

T

O

H

E

M

R

E

H

M

O

R

T

Examples:Insert in the order given: Insert in the order given: Insert in the order given:

M, O, T, H, E, R T, H, E, R, M, O E, H, M, O, R, T

M

O

T

H

E

R

T

O

H

E

M

R

E

H

M

O

R

T

M

O

T

H

E

R

T

O

H

E

M

R

E

H

M

O

R

T

M

O

T

H

E

R

T

O

H

E

M

R

E

H

M

O

R

T

Page 23: 10. Binary Trees

23

d. What about deleting a node a BST?

Case 1: A leaf, and Case 2: 1 childEasy — just reset link from parent

Case 3: 2 children: 1. Replace node with inorder successor X.2. Delete X (which has 0 or 1 child)

I M P

OH

K N

L

C

E

F

A

B D

G

Page 24: 10. Binary Trees

24

Some Special Kinds of Trees:

Threaded Binary Search Trees (§13.1)

AVL Trees (§13.2)

2-3-4 Trees (§13.3)

B-Trees (§13.3)

Red-Black Trees (map, set, multimap, multiset in STL) (§13.3)

Tries

Huffman Code Trees (data compression) (§10.5)