Docsity

Tree Intro Slides CS 225, Slides of Computer Science

Partial preview of the text

Download Tree Intro Slides CS 225 and more Slides Computer Science in PDF only on Docsity!

Department of Computer Science

Data Structures

Tree Definitions

CS 225 September 14, 2026

Brad Solomon

MP_Lists out now!

MP submission on PL has two separate submissions

The extra credit portion will only test part 1

Completion of the extra credit portion by the following

Monday is worth 4 points

Learning Objectives

Review trees and binary trees

Discuss the tree ADT

Explore tree implementation details

Practice tree theory with recursive definitions and proofs

Review iterators with a practical example

Ø

Cur. Location Cur. Data Next ListNode * curr Curr->data Curr->next unsigned index data[index] index++ Some form of (x, y, z)

Iterators

We want to be able to loop through all elements for any underlying

implementation in a systematic way

Iterators

The actual iterator is defined as a class inside the outer class:

Iterator& operator ++()

1. It must be of base class std::iterator

2. It must implement at least the following operations:

const T & operator *()

bool operator !=(const Iterator &)

Iterators

Cube::Iterator it = myCube.begin(); while (it != myCube.end()) { std::cout << *it << " "; it++; }

class Cube { // ... (Skipping for space) class CubeIt { // ... (Skipping for space) CubeIt& operator++() { ++ix_; if (ix_ == cube_->x_dim) { ix_ = 0; ++iy_; if (iy_ == cube_->y_dim) { iy_ = 0; ++iz_; } } return *this;

Iterators template class List { class ListIterator : public std::iterator { public: ListIterator& operator++(); ListIterator& operator--() bool operator!=(const ListIterator& rhs); const T& operator*(); }; ListIterator begin() const; ListIterator end() const; };

Implementing the mandatory interface enables easy looping!

Trees A non-linear data structure defined recursively as a collection of nodes where each node contains a value and zero or more connected nodes. [In CS 225] a tree is also:

  1. Acyclic — No path from node to itself
  2. Rooted — A specific node is labeled root 1 2 3 4 5 6

A

S X

C

A

S X

C

S

C

A

X

Which of the following are binary trees?

A B C

Join Code: 225

Binary Tree Height

S 2

C

Height: The length of the longest path from root to leaf

What is the height of a tree with zero nodes?

Lets define additional terminology for different types of binary trees!

Binary Tree

Binary Tree: full

A

S X

C

A tree F is full if and only if:

A full tree is a binary tree where every node has either 0 or 2 children

Binary Tree: full

Which of the following are possible sizes (# of nodes) for…

… a full binary tree?

Join Code: 225

A) 2 Nodes

B) 5 Nodes

C) 7 Nodes

D) 8 Nodes

Binary Tree: perfect

A tree P is perfect if and only if:

Every internal node has 2 children and all leaves are at the same level.

A perfect tree is a binary tree where…

A

S X

C