extension of linked list

29
Extension of linked list Zhen Jiang West Chester University [email protected]

Upload: ganesa

Post on 11-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Extension of linked list. Zhen Jiang West Chester University [email protected]. Outline. Double Cyclic Linked List Queue & Stack Binary Tree Expression Tree Networks. (Single) Linked list. node. NULL. Double Cyclic Linked List. node. NULL. Queue. node. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Extension of linked list

Extension of linked list

Zhen Jiang West Chester University

[email protected]

Page 2: Extension of linked list

Outline Double Cyclic Linked List Queue & Stack Binary Tree Expression Tree Networks

Page 3: Extension of linked list

(Single) Linked list

NULL

node

Page 4: Extension of linked list

Double Cyclic Linked List

NULL

node

Page 5: Extension of linked list

Queue

NULL

node

Insert a new item at the head

Delete an item from the tail

Page 6: Extension of linked list

Stack

NULL

node

Insert a new item at the head

Delete an item from the head

Page 7: Extension of linked list

Implementation of stack with template http://www.cs.wcupa.edu/~zjiang/

csc513_template.ppt

Page 8: Extension of linked list

Use of Stack Project 5

Reading information via file http://www.cs.wcupa.edu/~zjiang/

530_proj5_file.pdf Three stacks, one for each {}, (), and [].

Left => push/insert Right => pop/delete the closest left symbol

Evaluation of (postfix) expression

Page 9: Extension of linked list

5 4 + 3 * 5 4 3 + * 5 4 * 3 + 5 4 3 * +

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

Page 10: Extension of linked list

Read the formula from left to right Number => push Binary operator => 2 pops

Right number popped first Then, left number popped. Calculate result = <second> op <first> Push (result)

Until expression reading finishes and only one (final) result is left in stack

Page 11: Extension of linked list

Disorder of the link connection

NULL

node

Page 12: Extension of linked list

Tree Definition

Each node u has n(u) children Considering the parent-child

relationship is undirected, there is no cycle in a tree

There is only one node called the root in the entire tree. It has children only but no parent.

Page 13: Extension of linked list

Binary Tree Definition

Definition of tree (3) For each node u, n(u)<3 L<S<R

< is a relation between any two nodes in the tree

Page 14: Extension of linked list

Constructor Assume each node has a number

value so that we have the relation < Given a sequence: n1, n2, n3, …, ni,

… n1 -> set root unit (node) ni -> call insertion(node, ni)

Page 15: Extension of linked list

Insertion(p, n) If (p->v < n)

If p->R not empty Insertion (p->R, n)

else Set p->R

If (p->v > n) Similar to the case (p->v < n)

Page 16: Extension of linked list

Travel (print out all node values) Infix travel, L->S->R Postfix travel, L->R->S Prefix travel, S->L->R

Page 17: Extension of linked list

Infix travel If node is not empty, Infix_print

(node); Infix_print (p)

If (p->L) infix_print(p->L) Cout << p->v If(p->R) infix_print (p->R)

Page 18: Extension of linked list

Search If node is not empty, Search (node,

n); Search (p, n)

If (p->v == n) return true If (p->v < n) return Search (p->R, n) If (p->v > n) return Search (p->L, n) Terminating condition: if (!p) return false

Page 19: Extension of linked list

Deletion Delete the root node

If node is empty If node->L (or node->R) is empty

tmp = node->R (or node->L) Delete node node = tmp

Page 20: Extension of linked list

If neither node->L nor node->R is empty If node->L->R is empty

node -> v = node->L->v tmp = node->L->L Delete node->L node->L = tmp

Else tmp = node->L While (tmp->R->R) tmp = tmp->R node->v = tmp->R->V tmp2 = tmp->R->L Delete tmp->R tmp->R = tmp2

Page 21: Extension of linked list

See if you can find a certain value in the tree and delete it!

Delete (n) If (node->v == n) delete node; If (node->v < n)

deletion (node, node-R, n, 1) If (node->v > n)

deletion (node, node-L, n, 0)

Page 22: Extension of linked list

Deletion (parent, start, value, flag) If start is empty // not found, done! If start->v < value If start->v > value Else //start->v == value

If both start->L and start->R are empty Delete start If (flag)Parent ->R = NULL Else Parent -> L = NULL

If start->L (or start->R) is empty

Page 23: Extension of linked list

Deletion (parent, start, value, flag) Else //start->v == value

If start->L (or start->R) is empty If (flag) Parent-R = Start->R (or start->L) Else Parent->L = Start->R Delete start

If neither start->L nor start->R is empty

Page 24: Extension of linked list

If start->L->R is empty start -> v = start->L->v tmp = start->L->L Delete start->L start->L = tmp

Else tmp = start->L While (tmp->R->R) tmp = tmp->R start->v = tmp->R->V tmp2 = tmp->R->L Delete tmp->R tmp->R = tmp2

Page 25: Extension of linked list

Expression Tree Definition

Definition of binary tree (5) All leaves are numbers, and all

intermediate nodes are operators To simplify the discussion in this

class, we use binary operators only.

Page 26: Extension of linked list

Evaluation If node is not empty, R_E(node); R_E(p)

If p->v is not digit Lvalue = R_E(p->L); Rvalue = R_E(p->R); Return Lvalue <p->v> Rvalue;

Else Return p->v

Page 27: Extension of linked list

Print Postfix

Construction From infix format http://www.cs.wcupa.edu/~zjiang/

csc530_expressionTree.htm

Page 28: Extension of linked list

Network Graph table

http://www.cs.wcupa.edu/~zjiang/csc530_graph_table.pdf

Depth first search http://www.cs.wcupa.edu/~zjiang/csc530_depth.pdf

Width first search http://www.cs.wcupa.edu/zjiang/csc530_width.pdf

Shortest path construction http://www.cs.wcupa.edu/~zjiang/

csc530_shortestpath.pdf

Page 29: Extension of linked list

Spanning (travel) Project 7