+ All Categories
Home > Documents > Introduction to Computer Science Lecture 7: Data Abstractions

Introduction to Computer Science Lecture 7: Data Abstractions

Date post: 10-Dec-2021
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
82
Introduction to Computer Science Lecture 7: Data Abstractions Tian-Li Yu Taiwan Evolutionary Intelligence Laboratory (TEIL) Department of Electrical Engineering National Taiwan University [email protected] Slides made by Tian-Li Yu, Jie-Wei Wu, and Chu-Yu Hsu Tian-Li Yu Data Abstractions 1 / 82
Transcript
Page 1: Introduction to Computer Science Lecture 7: Data Abstractions

Introduction to Computer ScienceLecture 7: Data Abstractions

Tian-Li Yu

Taiwan Evolutionary Intelligence Laboratory (TEIL)Department of Electrical Engineering

National Taiwan University

[email protected]

Slides made by Tian-Li Yu, Jie-Wei Wu, and Chu-Yu Hsu

Tian-Li Yu Data Abstractions 1 / 82

Page 2: Introduction to Computer Science Lecture 7: Data Abstractions

Data Structure Fundamentals

Arrays- Homogeneous- Heterogeneous

Lists- Storage

Contiguous lists (arrays)Linked lists

- OperationsStacks: FILOQueues: FIFO

Trees- Binary search tree

Binary heaps

Tian-Li Yu Data Abstractions 2 / 82

Page 3: Introduction to Computer Science Lecture 7: Data Abstractions

Data Structure Concepts

Abstraction vs. real dataDynamic vs. static structuresPointers: locating data

- Program counter→ instruction pointer

Data structure implementation

Tian-Li Yu Data Abstractions 3 / 82

Page 4: Introduction to Computer Science Lecture 7: Data Abstractions

Homogeneous Arraysx x+1 x+2 x+3 x+4 x+5 x+6 x+7Address

Memory calls

Reading[1] Reading[3

Reading[2] Reading[4]

Machine’s memory

Conceptual array

Entry from 2th column in Row 3

ROW 1 ROW 2 ROW 3 ROW 4

ROW 1

ROW 2

ROW 3

ROW 4

Address polynomial: x + c · (i − 1) + (j − 1)

High-dimensional arrays: array of (array of . . .) arrays.

Tian-Li Yu Data Abstractions 4 / 82

Page 5: Introduction to Computer Science Lecture 7: Data Abstractions

Array Addresses

x = 1000

x[0] = 1000 x[0][0]1000

x[0][1]1004

x[0][2]1008

x[0][3]1012

x[1] = 1016 x[1][0]1016

x[1][1]1020

x[1][2]1024

x[1][3]1028

x[2]=1032 x[2][0]1032

x[2][1]1036

x[2][2]1040

x[2][3]1044

int x[3][4]

x[1][2] 1000+1*4*4+2*4 1016 (x[1])+ 8 1024

sizeof(int): 4

Tian-Li Yu Data Abstractions 5 / 82

Page 6: Introduction to Computer Science Lecture 7: Data Abstractions

Heterogeneous Arrays

Addresses: x

Employee.Name Employee.Age Employee.SkillRating

Employee

x+25 x+26

a. Array stored in a contiguous block

Pointers

Employee.Name

Employee.Age

Employee.SkillRating

b. Array components stored in separate locations

Using pointers to locate separate dataTian-Li Yu Data Abstractions 6 / 82

Page 7: Introduction to Computer Science Lecture 7: Data Abstractions

Template Functions & Classes

int Add(const int a, const int b) {return (a+b);

}

template <class T>T Add(const T& a, const T& b) {

return (a+b);}

Complex<double> var;

template <class T>class Complex {public:

Complex (const T&, const T&);private:

T re;T im;

};

Tian-Li Yu Data Abstractions 7 / 82

Page 8: Introduction to Computer Science Lecture 7: Data Abstractions

Lists, Stacks, and Queues

Jill

Bob

Devon

Maurice

Head

Tail

List

a. A list of names

Stack

Top

Bottom

b. A stack of books

Queue

Tail

Entrance

Head

c. A queue of people

Tian-Li Yu Data Abstractions 8 / 82

Page 9: Introduction to Computer Science Lecture 7: Data Abstractions

List

Linear List as C++ Abstract Class

template <class T>class linearList{public:virtual ˜linearList() {};virtual bool empty() const = 0;

//return true iff list is emptyvirtual int size() const = 0;

//return the number of elements in listvirtual T& get(int index) const = 0;

//return element whose index is indexvirtual int indexOf(const T& element) const = 0;

//return the index of first occurrence of elementvirtual void erase(int index) = 0;

//remove the element whose index is indexvirtual void insert(int index, const T& element) = 0;

//insert element so that its index is indexvirtual void output(ostream& out) const = 0;

//insert list into stream out}

Tian-Li Yu Data Abstractions 9 / 82

Page 10: Introduction to Computer Science Lecture 7: Data Abstractions

List

Storing Lists

Contiguous list (array)- Pros: easy to implement, excellent choice for static use.- Cons: time consuming for dynamic use, fragment may occur

without carefully implementation.

Linked list- Head pointer: Indicating the start.- NIL pointer (NULL pointer): Indicating the end.

Tian-Li Yu Data Abstractions 10 / 82

Page 11: Introduction to Computer Science Lecture 7: Data Abstractions

List

Singly Linked Lists: Memory Layout

Layout of L = (a,b , c,d,e) using an array representation.

a b c d e

A linked representation uses an arbitrary layout.

c a e d b

Tian-Li Yu Data Abstractions 11 / 82

Page 12: Introduction to Computer Science Lecture 7: Data Abstractions

List

Linked Representation

Use a variable firstNode to get to the first element aPointer (or link) in e is NULL

c a e d b

firstNode

Tian-Li Yu Data Abstractions 12 / 82

Page 13: Introduction to Computer Science Lecture 7: Data Abstractions

List

Normal Way To Draw a Linked List

a b c d e

NULL

firstNode

link or pointer field of node

data field of node

Tian-Li Yu Data Abstractions 13 / 82

Page 14: Introduction to Computer Science Lecture 7: Data Abstractions

List

list::get(int index)

Start from the first node.get(2)

- desiredNode = firstNode → next → next ; // get to the 3rd node- return desiredNode → element ;

a b c d e

NULL

firstNode

Tian-Li Yu Data Abstractions 14 / 82

Page 15: Introduction to Computer Science Lecture 7: Data Abstractions

List

list::erase(0)

Special case: need to change firstNode

a b c d e

NULL

firstNode

Tian-Li Yu Data Abstractions 15 / 82

Page 16: Introduction to Computer Science Lecture 7: Data Abstractions

List

list::erase(2)

1 First get the beforeNode2 Identify the deleteNode3 Then change pointer in beforeNode

a b c d e

NULL

firstNode

deleteNode

beforeNode

(1) (2)

(3)

Tian-Li Yu Data Abstractions 16 / 82

Page 17: Introduction to Computer Science Lecture 7: Data Abstractions

List

list::insert(0, ‘f’)

Get a node, set its data and link fieldsUpdate firstNode

a bf c d e

NULL

firstNode

newNode

Tian-Li Yu Data Abstractions 17 / 82

Page 18: Introduction to Computer Science Lecture 7: Data Abstractions

List

list::insert(3, ‘f’)

1 Find beforeNode2 Create a node and set its data/link fields.3 Link beforeNode to newNode

a b c d e

NULL

firstNode

newNode

beforeNode

(1)

(2)

(3)f

Tian-Li Yu Data Abstractions 18 / 82

Page 19: Introduction to Computer Science Lecture 7: Data Abstractions

List

Variations

List with a header node (dummy).

a b c d e

NULL

headerNode

Circular list

a b c d e

firstNode

Tian-Li Yu Data Abstractions 19 / 82

Page 20: Introduction to Computer Science Lecture 7: Data Abstractions

List

Doubly Linked Circular List with Header

STL class std::list- Doubly linked circular list with header node.- Has many more methods than our list.

a b c d e

headerNode

Tian-Li Yu Data Abstractions 20 / 82

Page 21: Introduction to Computer Science Lecture 7: Data Abstractions

List

STL list

#include <list>size()push front(), push back()pop front(), pop back()http://www.cplusplus.com/reference/stl/list/

iterator- Standard way to traverse a STL container

list<int> a;....for (list<int>::iterator it= a.begin(); it != a.end(); ++it) {

cout << *it << ” ”;}

Tian-Li Yu Data Abstractions 21 / 82

Page 22: Introduction to Computer Science Lecture 7: Data Abstractions

Stack & Queue

Stack & Queue Implementations

Special cases of linked list- Stack: Recording the stack point- Queue: Recording head and tail

Contiguous list- Stack: Array with a stack point- Circular queue

Tian-Li Yu Data Abstractions 22 / 82

Page 23: Introduction to Computer Science Lecture 7: Data Abstractions

Stack & Queue

Stacks & Queues Implementations (contd.)

Reserved block of memory cells

Space for growth

Stack pointer

Stack’s base

Stack entries

Head

pointer

Tail

pointer

Head

pointer

Tail

pointer

A

B

C

Head

pointer

Tail

pointer

B

C

D

Head

pointer

Tail

pointer

C

D

E

Tian-Li Yu Data Abstractions 23 / 82

Page 24: Introduction to Computer Science Lecture 7: Data Abstractions

Stack

Abstract Stack Class

template <class T>class stack{public:virtual ˜stack() {};virtual bool empty() const = 0;

//return true iff stack is emptyvirtual int size() const = 0;

//return the number of elements in stackvirtual T& top() = 0;

//return reference to the top elementvirtual void pop() = 0;

//remove the top elementvirtual void push(const T& element) = 0;

//insert element at the top of the stack}

Tian-Li Yu Data Abstractions 24 / 82

Page 25: Introduction to Computer Science Lecture 7: Data Abstractions

Stack

Derive from Array

Stack top is either left end or right end.empty()→ arrayList::empty()→ Θ(1)

size()→ arrayList::size()→ Θ(1)

top()→ get(0) or get(size() - 1)→ Θ(1)

a b c d e

Tian-Li Yu Data Abstractions 25 / 82

Page 26: Introduction to Computer Science Lecture 7: Data Abstractions

Stack

Derive from Array (contd.)

When top is left end- push( element)→ insert(0, element)→ Θ(n)- pop()→ erase(0)→ Θ(n)

When top is right end- push( element)→ insert(size(), element)→ Θ(1)- erase(size()-1)→ Θ(1)

Tian-Li Yu Data Abstractions 26 / 82

Page 27: Introduction to Computer Science Lecture 7: Data Abstractions

Stack

Derive from Linked List

Stack top is either left end or right end.empty()→ list::empty()→ Θ(1)

size()→ list::size()→ Θ(1)

a b c d e

NULL

firstNode

Tian-Li Yu Data Abstractions 27 / 82

Page 28: Introduction to Computer Science Lecture 7: Data Abstractions

Stack

Derive from Linked List (contd.)

When top is right endtop()→ get(size()-1)→ Θ(n)push( element)→ insert(size(), element)→ Θ(n)pop()→ erase(size()-1)→ Θ(n)

When top is left endtop()→ get(0)→ Θ(1)push( element)→ insert(0, element)→ Θ(1)pop()→ erase(0)→ Θ(1)

Tian-Li Yu Data Abstractions 28 / 82

Page 29: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching

( ( ( a + b ) * c + d − e ) / ( f + g ) − ( h + j ) )

0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 20 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

APP

Output pairs (u, v) such that the left parenthesis at position u is matched with theright parenthesis at v

- (2,6), (1,13), (15,19), (21,25), (0,26)

Also report missing parentheses- (a+b))*((c+d)- (0,4), right parenthesis at 5 has no matching left parenthesis, (8,12), left parenthesis at 7

has no matching right parenthesis

Tian-Li Yu Data Abstractions 29 / 82

Page 30: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Algorithm

Scan expression from left to rightWhen a left parenthesis is encountered, push its position to the stackWhen a right parenthesis is encountered, pop matching position from stack

Tian-Li Yu Data Abstractions 30 / 82

Page 31: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Example

( ( ( a + b ) * c + d − e ) / ( f + g ) − ( h + j ) )

0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 20 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

210

ActionsPush 0Push 1Push 2

Output

Tian-Li Yu Data Abstractions 31 / 82

Page 32: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Example (contd.)

( ( ( a + b ) * c + d − e ) / ( f + g ) − ( h + j ) )

0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 20 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

10

ActionsPush 0Push 1Push 2Pop 2→ Output (2,6)

Output(2,6)

Tian-Li Yu Data Abstractions 32 / 82

Page 33: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Example (contd.)

( ( ( a + b ) * c + d − e ) / ( f + g ) − ( h + j ) )

0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 20 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

0

ActionsPush 0Push 1Push 2Pop 2→ Output (2,6)Pop 1→ Output (1,13)

Output(2,6)(1,13)

Tian-Li Yu Data Abstractions 33 / 82

Page 34: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Example (contd.)

( ( ( a + b ) * c + d − e ) / ( f + g ) − ( h + j ) )

0 1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 20 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6

0

ActionsPush 0Push 1Push 2Pop 2→ Output (2,6)Pop 1→ Output (1,13)Push 15Pop 15→ Output (15,19)Push 21Pop 21→ Output (21,25)Pop 0→ output (0,26)

Output(2,6)(1,13)(15,19)(21,25)(0,26)

Tian-Li Yu Data Abstractions 34 / 82

Page 35: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Example (contd.)

( a + b ) ) * ( ( c + d )

0 1 2 3 4 5 6 7 8 9 1 1 10 1 2

ActionsPush 0Pop 0→ Output (0,4)Pop→ Empty stack!!!

Output(0,4)“right parenthesis at 5has no matching leftparenthesis”

Tian-Li Yu Data Abstractions 35 / 82

Page 36: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Parentheses Matching: Example (contd.)

( a + b ) ) * ( ( c + d )

0 1 2 3 4 5 6 7 8 9 1 1 10 1 2

7

ActionsPush 0Pop 0→ Output (0,4)Pop→ Empty stack!!!Push 7Push 8Pop 8→ Output (8,12)7 still remains!!!

Output(0,4)“right parenthesis at 5has no matching leftparenthesis”(8,12)“left parenthesis at 7has no matching rightparenthesis”

Tian-Li Yu Data Abstractions 36 / 82

Page 37: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Post-order Calculator

3 + 4 * 5→ 3 4 5 * +(3 + 4) * 5→ 3 4 + 5 *

Algorithm- For an operand token, push it into stack- For an operator token, pop tokens, operate, then push back the result.

Tian-Li Yu Data Abstractions 37 / 82

Page 38: Introduction to Computer Science Lecture 7: Data Abstractions

Stack Applications

Post-order Calculator: Example

3 4 + 5 *

Push 3 Push 4Pop 4Pop 3Push 3+4

Push 5Pop 5Pop 7Push 7*5

343 7

57 35

3 4 5 * +

Push 3 Push 4 Push 5Pop 5Pop 4Push 4*5

Pop 20Pop 3Push 3+20

343

543

203 23

Tian-Li Yu Data Abstractions 38 / 82

Page 39: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Abstract Queue Class

template <class T>class queue{public:virtual ˜queue() {};virtual bool empty() const = 0;

//return true iff queue is emptyvirtual int size() const = 0;

//return the number of elements in queuevirtual T& front() = 0;

//return reference to the front elementvirtual T& back() = 0;

//return reference to the back elementvirtual void pop() = 0;

//remove the front elementvirtual void push(const T& element) = 0;

//add element at the back of the queue}

Tian-Li Yu Data Abstractions 39 / 82

Page 40: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Derive from Array

When front is right end & rear is left end- empty()→ queue::empty()→ Θ(1)- size()→ queue::size(0)→ Θ(1)- front()→ get(size() - 1)→ Θ(1)- back()→ get(0)→ Θ(1)- pop()→ erase(size() - 1)→ Θ(1)- push( element)→ insert(0, element)→ Θ(n)

a b c d e

FrontRear

Tian-Li Yu Data Abstractions 40 / 82

Page 41: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Derive from Array (contd.)

When front is left end & rear is right end- empty()→ queue::empty()→ Θ(1)- size()→ queue::size(0)→ Θ(1)- front()→ get(0)→ Θ(1)- back()→ get(size()-1)→ Θ(1)- pop()→ erase(0)→ Θ(n)- push( element)→ insert(size(), element)→ Θ(1)

a b c d e

Front Rear

Tian-Li Yu Data Abstractions 41 / 82

Page 42: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Can We Do Better?

To perform each operation in Θ(1) time (excluding array doubling), we need acustomized array representation.

Circular.front

rear

Tian-Li Yu Data Abstractions 42 / 82

Page 43: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Push

1 Move Rear clockwise. rear = (rear + 1) % arrayLength2 Then put into queue[rear ]

front

rear

front

rear

Tian-Li Yu Data Abstractions 43 / 82

Page 44: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Pop

1 Move Front clockwise. front = (front + 1) % arrayLength2 Then erase queue[front ]

front

rear

front

rear

Tian-Li Yu Data Abstractions 44 / 82

Page 45: Introduction to Computer Science Lecture 7: Data Abstractions

Queue

Empty & Full Queue

An empty queue.

front

rear

A full queue.

frontrear

Both front == rear .

Define an integer variable size.- Following each push do + + size.- Following each pop do − − size.- Queue is empty iff (size == 0).- Queue is full iff (size == arrayLength).

Tian-Li Yu Data Abstractions 45 / 82

Page 46: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Nature Lover’s View Of A Tree

Root

Leaves

Branches

Tian-Li Yu Data Abstractions 46 / 82

Page 47: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Computer Scientist’s View

Root

Leaves

Branches

Tian-Li Yu Data Abstractions 47 / 82

Page 48: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Linear Lists And Trees

Linear lists are useful for serially ordered data.- (e0,e1,e2, ...,en−1)- Days of week.- Months in a year.- Students in this class.

Trees are useful for hierarchically ordered data.- Employees of a corporation.

President, vice presidents, managers, and so on.

Tian-Li Yu Data Abstractions 48 / 82

Page 49: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Hierarchical Data and Trees

The element at the top of the hierarchy is the root.

Elements next in the hierarchy are the children of the root.

Elements next in the hierarchy are the grandchildren of the root, and so on.

Elements that have no children are leaves.

Tian-Li Yu Data Abstractions 49 / 82

Page 50: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Example Tree

National Taiwan

University

Engineering College

CE Department

ME Department

EECS College

EE Department

CSIE Department

PE

Tian-Li Yu Data Abstractions 50 / 82

Page 51: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Definition

Recursive definition.

A tree t is a finite non-empty set of elements.

One of these elements is called the root.

The remaining elements, if any, are partitioned into trees, which are called thesubtrees of t .

Tian-Li Yu Data Abstractions 51 / 82

Page 52: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Example Tree

National Taiwan

University

Engineering College

CE Department

ME Department

EECS College

EE Department

CSIE Department

PE

NationalTaTT iwan

UUniveerssittyyy

PEEngineeringgg

College

CEDepartmenttt

MEDepartmenttt

Root

EECSCollege

EEDepartmenttt

CSIEDepartmenttt

Root

Root

Tian-Li Yu Data Abstractions 52 / 82

Page 53: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Leaves

National Taiwan

University

Engineering College

CE Department

ME Department

EECS College

EE Department

CSIE Department

PE

Tian-Li Yu Data Abstractions 53 / 82

Page 54: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Parent, Children, Siblings, Ancestors, Descendants

National Taiwan

University

Engineering College

CE Department

ME Department

EECS College

EE Department

CSIE Department

PE

Sibling

Parent-Child

Ancestor-

Descendant

Tian-Li Yu Data Abstractions 54 / 82

Page 55: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Levels

Level 1

Level 2

Level 3

National Taiwan

University

Engineering College

CE Department

ME Department

EECS College

EE Department

CSIE Department

PE

Tian-Li Yu Data Abstractions 55 / 82

Page 56: Introduction to Computer Science Lecture 7: Data Abstractions

Tree

Node Degree = Number Of Children

National Taiwan

University

Engineering College

CE Department

ME Department

EECS College

EE Department

CSIE Department

PE

3

2 2 0

0000

Tree Degree = Max Node Degree ( =3 )

Tian-Li Yu Data Abstractions 56 / 82

Page 57: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Tree

Binary Trees

Finite non-empty collection of elements.

A binary tree has a root element.

The remaining elements (if any) are partitioned into two binary trees.

These are called the left and right subtrees.

Tian-Li Yu Data Abstractions 57 / 82

Page 58: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Tree Storage

Storing Binary Trees without Pointers

Conceptual tree

A

B

D E

C

F

Actual storage organization

A

1

B

2

C

3

D

4

E

5 6

F

7

Root node Nodes in 2nd level

of tree

Nodes in 3rd level

of tree

Tian-Li Yu Data Abstractions 58 / 82

Page 59: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Tree Storage

May Waste Lots of Memories...

Conceptual tree

A

B

E

C

D

Actual storage organization

A

1

B

2

C

3 4 5 6

D

7 8 9 10 11 12 13 14

E

15

Root

node

Nodes in 2nd

level of tree

Nodes in 3rd level

of tree

Nodes in 4th level of tree

Tian-Li Yu Data Abstractions 59 / 82

Page 60: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Tree Storage

Storing Binary Trees with Pointers

Conceptual tree

A

B

D E

C

F

Actual storage organization

A

B

C NIL

D NIL NIL F NIL NIL

E NIL NIL

Root pointer

Tian-Li Yu Data Abstractions 60 / 82

Page 61: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree

Definition of Binary Search Tree (BST)

A binary tree.Each node has a (key , value) pair.For every node x, all keys in the left subtree of x are smaller than that in xFor every node x, all keys in the right subtree of x are greater than that in x

Operations- Traversal.- Search, insertion, deletion.- If the tree is balanced, insertion and search takes only Θ(log n) of time, where n is the

number of nodes.

Tian-Li Yu Data Abstractions 61 / 82

Page 62: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Traversal

Traverse in Order

F

J

I

E G

HD

B

A C

1. Print the left

branch in alphabetical

order

2.Print the

root node

3.Print the right

branch in

alphabetical order

A, B, C, D, E, G, H, I, JF,

Tian-Li Yu Data Abstractions 62 / 82

Page 63: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Traversal

Pre-Order and Post-Order

F

J

I

E G

HD

B

A C

Pre-order: (1) root (2) left (3) rightF D B A C E H G J I

Post-order: (1) left (2) right (3) rootA C B E D G I J H F

Tian-Li Yu Data Abstractions 63 / 82

Page 64: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Traversal

Pre-Order and Post-Order

In-order of a BST is always like sorting ascended.

A BST is uniquely decided given its pre-order or post-order traversal (deciding theroot and then splitting nodes into left and right).

A binary tree is uniquely decided given its pre-order (or post-order) and in-ordertraversals.

Tian-Li Yu Data Abstractions 64 / 82

Page 65: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Search

Search

Very similar to binary search (may not be half-half).

G

D

B

A C JH

M

L

IE

K

Tian-Li Yu Data Abstractions 65 / 82

Page 66: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Insertion

Insertion

A. Search for the new entry until its absence is detected.

H

?

P

J

G K

NE

B

B. This is the position in which the new entry should be attached.

H

P

J

G

M

K

NE

B

Tian-Li Yu Data Abstractions 66 / 82

Page 67: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Leaf

Erase a leaf element whose key is 7.

Tian-Li Yu Data Abstractions 67 / 82

Page 68: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Degree-1 Node

Erase a leaf element whose key is 40.

Tian-Li Yu Data Abstractions 68 / 82

Page 69: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Degree-2 Node

Erase an element whose key is 10.

Tian-Li Yu Data Abstractions 69 / 82

Page 70: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Degree-2 Node (contd.)

Swap it with its successor (or predecessor).- The minimum node of the right subtree (keep going left).- Or the parent if itself is a left child.

Tian-Li Yu Data Abstractions 70 / 82

Page 71: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Degree-2 Node (contd.)

Its successor has degree of 1 or 0.So simply cut and reconnect the rest of the tree.

Tian-Li Yu Data Abstractions 71 / 82

Page 72: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Degree-2 Node (contd.)

Erase an element whose key is 20.

Tian-Li Yu Data Abstractions 72 / 82

Page 73: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Search Tree Deletion

Delete A Degree-2 Node (contd.)

Erase an element whose key is 18.

Tian-Li Yu Data Abstractions 73 / 82

Page 74: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap

Priority Queue

A stack pops the newest element.A queue pops the oldest element.What if we want to pop the most important element?If we associate elements with priorities, we’d like to pop the element with the highestpriority.

That data structure that accomplishes this task is called a priority queue.A binary heap is one method to implement a priority queue.

Tian-Li Yu Data Abstractions 74 / 82

Page 75: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Definition

Binary Min Heap

A complete binary tree.- A binary tree in which every level,

except possibly the last, is completelyfilled, and all nodes are as far left aspossible.

A min tree.- The key of each parent is no greater

than any of its child.

A min heap with 10 nodes.

Tian-Li Yu Data Abstractions 75 / 82

Page 76: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Storage

Storing Binary Heap by Array

The most common way to store a binaryheap is by using an array.

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]2 4 3 6 7 9 3 8 6 10

Traverse with index i:Go to parent Left child Right child Sibling(i − 1)/2 2 ∗ i + 1 2 ∗ i + 2 even: i − 1, odd: i + 1

Tian-Li Yu Data Abstractions 76 / 82

Page 77: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Push

Push ‘1’

The new element is alwaysinserted as the last element.Then float up as needed.

Tian-Li Yu Data Abstractions 77 / 82

Page 78: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Push

Push ‘7’

The new element is alwaysinserted as the last element.Then “float” up as needed.

Tian-Li Yu Data Abstractions 78 / 82

Page 79: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Pop

Pop

Pop the root (smallest key).Replace it with the lastelement.Then “sink” down by choosingthe “smaller” path.

Tian-Li Yu Data Abstractions 79 / 82

Page 80: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Pop

Pop (contd.)

Pop the root (smallest key).Replace it with the lastelement.Then “sink” down by choosingthe “smaller” path.

Tian-Li Yu Data Abstractions 80 / 82

Page 81: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Pop

Pop Operation in Array

Tian-Li Yu Data Abstractions 81 / 82

Page 82: Introduction to Computer Science Lecture 7: Data Abstractions

Binary Min Heap Complexity

Complexity of Heap

For n elements, the height of the tree is Θ(log n).Time complexity for both push and pop: Θ(log n).We may accomplish sorting (called HeapSort) by keeping popping from a min heap:Θ(n log n).We didn’t show it, but modifying a key also costs Θ(log n).

Tian-Li Yu Data Abstractions 82 / 82


Recommended