Sie sind auf Seite 1von 4

VIEWING CODE

ABSTRACT DATA TYPES:


STACKS, QUEUES, & TREES
CSE 110: Introduction to Computer Science
Stony Brook University

Interface: What

properties (data and operations) a given block


of code has or can perform
What

Implementation: The

actual code that makes up the data and


operations from the interface
How

ABSTRACT DATA TYPES

other objects

Abstract

object whose role is to hold and manipulate

Data Type (ADT): A container whose properties


(data and operations) are specified independently of any
particular implementation

the code actually does it

ADT EXAMPLE: A LIST


A

Container: An

the code can do

list has several basic operations:

Add/insert

a value, remove a value, view a specific value,


print the list, count the number of values

We

dont really care how the code works, as long as it lets us


perform these operations
We

could implement (define) the list as an array or one of


multiple other forms

STACKS
A

stack is a LIFO (last-in, first-out) data structure

STACK EXAMPLE
Operation

Stack Contents (bottom to top)

Push(2)

We

push new values onto the top of the stack

Push(7)

2, 7

We

pop values off the top of the stack

Push(4)

2, 7, 4

Pop()

2, 7 (4 is removed)

Pop()

2 (7 is removed)

Push(5)

2, 5

Computer

example: used to keep track of method calls

Real-world

example: a pile of books

QUEUES
A

queue is a FIFO (first-in, first-out) data structure

New

values are added (enqueued) at the back

Values

are removed (dequeued) from the front

Computer

example: tracking active processes in the OS

Real-world

example: a checkout line at a store

QUEUE EXAMPLE
Operation

Queue Contents (front to back)

Enqueue(2)

Enqueue(7)

2, 7

Enqueue(4)

2, 7, 4

Dequeue()

7, 4 (2 is removed)

Dequeue()

4 (7 is removed)

Enqueue(5)

4, 5

Trees

Trees

Binary tree
Root node

A linked container with a unique starting node


called the root, in which each node is capable
of having two child nodes, and in which a
unique path (series of nodes) exists from the
root to every other node

Node with two children


Node with right child

Leaf node

A picture is worth a
thousands words
22

Node with left child

What is the unique path


to the node containing
5? 9? 7?

23

Binary Search Trees

Binary Search Tree

Binary search tree (BST)


A binary tree (shape property) that has the
(semantic) property that characterizes the
values in a node of a tree:

Each node
is the root
of a subtree
made up of
its left and
right children

The value in any node is greater than the


value in any node in its left subtree and less
than the value in any node in its right subtree

24

Prove that this


tree is a BST
25

Figure 8.7 A binary search tree

TREE TRAVERSAL
Order

in which we print out the values in a binary tree

Inorder: print
For

left subtree, current node, right subtree

a BST, this prints a sorted list of numbers

Preorder: print

current node, left subtree, right subtree

Postorder: print

left subtree, right subtree, current node

Das könnte Ihnen auch gefallen