Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

72
Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes dified from authors’ slides

Transcript of Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Page 1: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Chapter 15

Trees

CS 302 - Data StructuresMehmet H Gunes

Modified from authors’ slides

Page 2: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Content

• Terminology• The ADT Binary Tree• The ADT Binary Search Tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 3: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• Use trees to represent relationships• Trees are hierarchical in nature

– “Parent-child” relationship exists between nodes in tree.

– Generalized to ancestor and descendant – Lines between the nodes are called edges

• A subtree in a tree is any node in the tree together with all of its descendants

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 4: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

Jake’s Pizza Shop

4

Page 5: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

ROOT NODE

A Tree Has a Root Node

5

Page 6: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEAF NODES

Leaf Nodes have No Children

6

Page 7: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEVEL 0

A Tree Has Levels

7

Page 8: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEVEL 1

Level One

8

Page 9: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEVEL 2

Level Two

9

Page 10: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

LEFT SUBTREE OF ROOT NODE

A Subtree

10

Page 11: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Owner Jake

Manager Chef Brad Carol

Waitress Waiter Cook Helper Joyce Chris Max Len

RIGHT SUBTREE OF ROOT NODE

Another Subtree

11

Page 12: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

(a) A tree; (b) a subtree of the tree in part a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 13: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

(a) An organization chart; (b) a family tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 14: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Kinds of Trees

• General Tree– Set T of one or more nodes such that T is

partitioned into disjoint subsets• A single node r , the root• Sets that are general trees, called subtrees of r

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 15: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Kinds of Trees

• n -ary tree– set T of nodes that is either empty or partitioned

into disjoint subsets:• A single node r , the root• n possibly empty sets that are n -ary subtrees of r

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 16: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Kinds of Trees

• Binary tree– Set T of nodes that is either empty or partitioned

into disjoint subsets• Single node r , the root• Two possibly empty sets that are binary trees, called

left and right subtrees of r

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 17: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

A Binary Tree

Q

V

T

K S

A E

L

17

Page 18: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

How many leaf nodes?

Q

V

T

K S

A E

L

18

Page 19: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

How many descendants of Q?

Q

V

T

K S

A E

L

19

Page 20: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

How many ancestors of K?

Q

V

T

K S

A E

L

20

Page 21: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Implementing a Binary Tree with Pointers and Dynamic Data

Q

V

T

K S

A E

L

21

Page 22: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Node Terminology for a Tree Node

22

Page 23: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Example: Algebraic Expressions.

• Binary trees that represent algebraic expressions

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 24: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Search Tree

• For each node n, a binary search tree satisfies the following three properties:– n ’s value is greater than all values in its left

subtree T L

– n ’s value is less than all values in its right subtree T R

– Both T L and T R are binary search trees

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 25: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Search Tree

• A binary search tree of names

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 26: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depends on its key values and their order of insertion.

Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.

The first value to be inserted is put into the root node.

Shape of a binary search tree . .

‘J’

26

Page 27: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left if it is less, or moving right if it is greater.

This continues at each level until it can be inserted as a new leaf.

‘J’

‘E’

27

Inserting ‘F’ into the BST

Page 28: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

‘J’

‘E’

‘F’

28

Inserting ‘F’ into the BST

Page 29: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

‘J’

‘E’

‘F’

‘T’

29

Inserting ‘F’ into the BST

Page 30: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater.

This continues until it can be inserted as a leaf.

‘J’

‘E’

‘F’

‘T’

‘A’

30

Inserting ‘A’ into the BST

Page 31: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?

What binary search tree . . .

‘A’

31

Page 32: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.

Binary search tree . . .

‘A’

‘E’

‘F’

‘J’

‘T’

32

Page 33: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Another binary search tree

Add nodes containing these values in this order:

‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’ ‘P’

33

Page 34: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Is ‘F’ in the binary search tree?

34

‘J’

‘E’

‘A’ ‘H’

‘T’

‘M’

‘K’

‘V’

‘P’ ‘Z’‘D’

‘Q’‘L’‘B’

‘S’

Page 35: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The Height of Trees• Definition of the level of a node n :

– If n is the root of T , it is at level 1– If n is not the root of T , its level is 1 greater than

the level of its parent

• Height of a tree T in terms of the levels of its nodes– If T is empty, its height is 0– If T is not empty, its height is equal to the maximum

level of its nodes

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 36: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The Height of Trees

• Binary trees with the same nodes but different heights

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 37: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Full, Complete, and Balanced Binary Trees

• A full binary tree of height 3

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 38: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Full, Complete, and Balanced Binary Trees

• Definition of a full binary tree– If T is empty, T is a full binary tree of height 0

– If T is not empty and has height h > 0, T is a full binary tree if its root’s subtrees are both full binary trees of height h – 1

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 39: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Full, Complete, and Balanced Binary Trees

• A complete binary tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 40: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Definitions

Full Binary Tree: A binary tree in which all of the leaves are on the same level and every nonleaf node has two children

40

Page 41: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Definitions (cont.)

Complete Binary Tree: A binary tree that is either full or full through the next-to-last level, with the leaves on the last level as far to the left as possible

41

Page 42: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Examples of Different Types of Binary Trees

42

Page 43: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The Maximum and Minimum Heights of a Binary Tree

• The maximum height of an n -node binary tree is n

• Binary trees of height 3

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 44: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The Maximum and Minimum Heights of a Binary Tree

• Counting the nodes in a full binary tree of height h

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 45: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Facts about Full Binary Trees

• A full binary tree of height h ≥ 0 has 2 h – 1 nodes

• You cannot add nodes to a full binary tree without increasing its height

• The maximum number of nodes that a binary tree of height h can have is 2 h – 1

• The minimum height of a binary tree with n nodes is [log 2 ( n + 1)]

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 46: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The Maximum and Minimum Heights of a Binary Tree

• Filling in the last level of a tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 47: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Traversals of a Binary Tree

• General form of recursive transversal algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 48: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Traversals of a Binary Tree

• Preorder traversal

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 49: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Traversals of a Binary Tree

• Three traversals of a binary tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 50: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Printing all the Nodes in Order

50

Page 51: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Three Tree Traversals

51

Page 52: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Traversals of a Binary Tree

• Inorder traversal.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 53: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Traversals of a Binary Tree

• Postorder traversal.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 54: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Tree Operations

• Test whether a binary tree is empty.• Get the height of a binary tree.• Get the number of nodes in a binary tree.• Get the data in a binary tree’s root.• Set the data in a binary tree’s root.• Add a new node containing a given data item

to a binary tree.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 55: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Tree Operations

• Remove the node containing a given data item from a binary tree.

• Remove all nodes from a binary tree.• Retrieve a specific entry in a binary tree.• Test whether a binary tree contains a specific

entry.• Traverse the nodes in a binary tree in

preorder, inorder, or postorder.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 56: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Tree Operations

UML diagram for the class BinaryTreeData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 57: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Tree Operations

• Formalized specifications demonstrated in interface template for binary tree– Listing 15-1

• Note method names matching UML diagram– Figure 15-12

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 58: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The ADT Binary Search Tree

• ADT binary tree ill suited for search for specific item

• Binary search tree solves problem

• Properties of each node, n– n ’s value greater than all values in left subtree TL

– n ’s value less than all values in right subtree TR

– Both TR and TL are binary search trees

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 59: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The ADT Binary Search Tree

• A binary search tree of names

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 60: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The ADT Binary Search Tree

Binary search trees with the same data

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 61: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The ADT Binary Search Tree

Binary search trees with the same data

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 62: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

The ADT Binary Search Tree

Binary search trees with the same data

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 63: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Search Tree Operations

• Test whether binary search tree is empty.• Get height of binary search tree.• Get number of nodes in binary search tree.• Get data in binary search tree’s root.• Insert new item into binary search tree.• Remove given item from binary search tree.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 64: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Binary Search Tree Operations

• Remove all entries from binary search tree.• Retrieve given item from binary search tree.• Test whether binary search tree contains

specific entry.• Traverse items in binary search tree in

– Preorder– Inorder– Postorder.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 65: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Searching a Binary Search Tree

• Search algorithm for binary search tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 66: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Searching a Binary Search Tree

• An array of names in sorted order

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 67: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Creating a Binary Search Tree

Empty subtree where the search algorithm terminates when looking for Frank

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 68: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Traversals of a Binary Search Tree

• Algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 69: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Deleting a Leaf Node

Delete Z

69

Page 70: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Delete R

Deleting a Node with One Child

70

Page 71: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Deleting a Node with Two Children

Delete Q

71

Page 72: Chapter 15 Trees CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Efficiency of Binary Search Tree Operations

• The Big O for the retrieval, insertion, removal, and traversal operations of the ADT binary search tree

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013