Docsity

Fundamentals of Computer Science: Binary Search Trees and Stacks, Slides of Computer Science

Partial preview of the text

Download Fundamentals of Computer Science: Binary Search Trees and Stacks and more Slides Computer Science in PDF only on Docsity!

Fundamental Structures of

Computer Science

Binary Search Trees

A Stack interface

public interface Stack { public void push(Object x); public void pop(); public Object top(); public boolean isEmpty(); public void clear(); }

Stacks are LIFO

a

b

c

d

e

Pop operation:

Last element that was pushed is the first to be popped.

A Queue interface

public interface Queue { public void enqueue(Object x); public Object dequeue(); public boolean isEmpty(); public void clear(); }

Queues are FIFO

back front

y^ k^ r^ q^ c^ m

Enqueue operation:

Queues are FIFO

back front y k r q c m

Enqueue operation:

Implementing stacks, 1

a

b

c

Linked representation. All operations constant time, or O(1).

Implementing stacks, 2

  • An alternative is to use an array-based

representation.

  • What are some advantages and disadvantages of an

array-based representation?

a b c

top

Now to Trees

CS is upside down

root

leaves

Arithmetic Expressions

Game trees

Tree Definitions

• A tree is a set of nodes and a set of directed

edges that connects pairs of nodes.

• A tree is a a Directed, Acyclic Graph (DAG)

with the following properties

• - one vertex is distinguished as the root;

no edges enter this vertex

• - every other vertex has exactly one

entering edge

Trees, more abstractly

• A tree is a directed graph with the following

characteristics:

  • There is a distinguished node called the root node.
  • Every non-root node has exactly one parent node

(the root has none).