Lecture 31 No computer use today. Reminder: Project 5 is due today. - - PowerPoint PPT Presentation

lecture 31
SMART_READER_LITE
LIVE PREVIEW

Lecture 31 No computer use today. Reminder: Project 5 is due today. - - PowerPoint PPT Presentation

Lecture 31 No computer use today. Reminder: Project 5 is due today. Project 6 has been posted to the course webpage. No one has indicated a conflict, so Practical Exam 2 is scheduled for next Wednesday, November 10, 5-7pm.


slide-1
SLIDE 1

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 1

Lecture 31

 No computer use today.  Reminder: Project 5 is due today.  Project 6 has been posted to the course

webpage.

 No one has indicated a conflict, so Practical

Exam 2 is scheduled for next Wednesday, November 10, 5-7pm.

 Questions?

slide-2
SLIDE 2

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 2

Outline

 Introduction to trees

 Tree terminology

 Binary trees  Binary tree representations

 Array  Linked nodes

slide-3
SLIDE 3

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 3

Introduction to Trees

 So far, all containers that we have looked at are

sequential and access is by position (either index or iterator).

 Many applications access data by value rather

than by position. For example, a phone book entry is accessed by a person's name. Could keep entries in a list and scan the list for the

  • name. As we have seen scanning a list is a

slow O(n) operation.

slide-4
SLIDE 4

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 4

Introduction to Trees

 Containers that are accessed by value are

called associative containers. Unfortunately, we will not get to the STL containers map, set, and hashmap in this class.

 However, we will look at the tree data structure

which is often used to implement associative containers.

slide-5
SLIDE 5

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 5

Tree Terminology

 A tree is a hierarchical data structure. It places

elements in nodes connect by branches that

  • riginate from a single root. For example, the
  • rganizational structure of the academic

programs at UE.

UE CAS CEHS CECS SOBA

ACCT & BA

NHS SED EXSS EECS MCE FL MATH NURS PT EE CoE CS SPAN FREN root node

slide-6
SLIDE 6

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 6

Tree Terminology

 The root node is the top level and has no

predecessor (or parent). It can have multiple successors (called children who are siblings to each other) to the next level.

 Each node contains a value and a set of 0 or

more links to children nodes. The links are called edges.

 A node with no children is called a leaf. All

  • ther non-root, non-leaf nodes are interior

nodes with at least one child.

slide-7
SLIDE 7

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 7

Tree Terminology

 Every node is the root of a subtree – the node

and all its descendants. (Its children and the children's children, etc.) A node's parent and the parent's parent, etc. are the node's ancestors.

 A path is a sequence of nodes from a node (N)

to the root (R): N = X0X1...Xk = R where k is the length of the path. Each node Xi+1 is the parent of Xi.

slide-8
SLIDE 8

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 8

Tree Terminology

 The length of a path defines the depth of the

  • node. The depth of a tree is the maximum

depth of any node in the tree. Sometimes the (depth of a tree +1) is called the height of the tree (though the textbook says that the height of a tree is the same as the depth of a tree).

slide-9
SLIDE 9

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 9

Tree Terminology

 Here is the UE organization chart again,

annotated with some of the tree terminology.

Path to CS: UE->CECS->EECS->CS; CS at depth 3

UE CAS CEHS CECS SOBA

ACCT & BA

NHS SED EXSS EECS MCE FL MATH NURS PT EE CoE CS SPAN FREN root; parent of CECS; ancestor to all leaf; child of EECS; descendant of CECS interior node subtree rooted at SOBA children of NHS; siblings to each other

slide-10
SLIDE 10

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 10

Binary Trees

 Organization charts are one of a few

applications that need trees where the nodes have more than 2 children. A file system with its directories would be another.

 Most applications only need a binary tree, a

tree where each node has at most 2 children.

slide-11
SLIDE 11

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 11

Binary Trees

 Formally, a binary tree is a finite set of nodes.

The set of nodes may be empty, called an empty tree. If the set is not empty, it meets the following rules:

  • 1. There is one special node called the root.
  • 2. Each node may be associated with up to two other

different nodes, called its left child and its right child.

  • 3. Each node, except the root, has exactly one parent;

the root has no parent.

  • 4. There is a path from every node following its parent

back to the root.

slide-12
SLIDE 12

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 12

Binary Trees

 A binary tree is said to be full, if every leaf has

the same depth and every node has two children.

 A binary tree is said to be complete, if every

level except the deepest level is full and the nodes in the deepest level are as far left as possible.

slide-13
SLIDE 13

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 13

Binary Trees

 Here are some binary trees:

full binary tree complete binary tree binary tree that is neither full nor complete

slide-14
SLIDE 14

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 14

Array Representation

 When a binary tree is complete, we can use a

simple array representation (a fixed-size array if the tree has a maximum size or a dynamic array if can grow indefinitely).

 Suppose we number the nodes starting at the

root and going from left to right at each level and then top to bottom. Call this number i. Then we store each node's value in an array at index i.

slide-15
SLIDE 15

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 15

Array Representation

 Here is a picture:

'A' 'L' 'G' 'O' 'R' 'I' 'T' 'H' 'M' 'S'

2 6 5 4 1 3 8 9 7

'A' 'L' 'G' 'O' 'R' 'I' 'T' 'H' 'M' 'S' [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] array

slide-16
SLIDE 16

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 16

Array Representation

 This representation is convenient for several

reasons:

 The root is always at array[0]  Suppose that the data for a node appears in

array[i]. The locations of the parent and children node can be computed.

 For a non-root node, the parent is always located at

array[(i-1)/2] (using integer division)

 The children (if they exist) are located at array[2i+1]

(left child) and array[2i+2] (right child)

slide-17
SLIDE 17

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 17

Array Representation

 The main problem with the array representation

is that if the tree is not complete, then there must be a way to indicate which elements of the array actually exist.

 The representation also can be very inefficient

if the tree is very deep with few nodes at each

  • level. The array would have to be very large,

but would be mostly empty.

 Solve these problems by implementing nodes

and edges directly.

slide-18
SLIDE 18

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 18

Linked Node Representation

 We can represent a tree node using a class

that has an attribute to hold the node value (data) and two tree node pointer attributes (leftChild and rightChild).

 The pointer attributes are used to link a node to

the nodes of its children. An entire tree is used represented as a pointer to the root node. The empty tree is represented using the null pointer.

 A partial picture of the previous example tree is

shown on the next slide.

slide-19
SLIDE 19

Friday, November 5 CS 215 Fundamentals of Programming II - Lecture 31 19

Linked Node Representation

'A'

data rightChild leftChild root

'L' 'G' 'O' 'R' 'I' 'T'