Data Structures II - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Data Structures II

Description:

Search for the presence of an entry. Print the list in alphabetical order. Insert a new entry ... (assign CurrentPointer the value in the current node's left ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 27
Provided by: shyhka
Category:

less

Transcript and Presenter's Notes

Title: Data Structures II


1
Data Structures II
  • Shyh-Kang Jeng
  • Department of Electrical Engineering/
  • Graduate Institute of Communication Engineering
  • National Taiwan University

2
Organization Chart
3
Trees
4
Trees
  • Nodes
  • Root
  • Terminal (leaf)
  • Parent, children, siblings
  • Subtrees
  • Depth

5
Node in a Binary Tree
left
right
Data
6
Linked Storage System
A
C
NIL
F
NIL
NIL
D
B
NIL
NIL
E
NIL
NIL
7
Storage without Pointers
1
2
3
4
5
6
7
B
D
A
C
E
F
8
Inefficient Storage
A
C
B
D
E
1
2
3
4
5
6
7
8
A
B
C
D
9
10
11
12
13
14
15
E
9
Binary Tree Package
  • Search for the presence of an entry
  • Print the list in alphabetical order
  • Insert a new entry

10
Ordered Tree
G
K
D
B
F
I
M
C
J
A
H
11
Binary Search
  • procedure BinarySearch(Tree, TargetValue)
  • assign CurrentPointer the value in the root
    pointer of Tree
  • assign Found the value false
  • while (Found is false and CurrentPointer is not
    NIL) do
  • case 1. TargetValue current node
  • (assign Found the value true)
  • case 2. TargetValue lt current node
  • (assign CurrentPointer the value in the current
    nodes left child pointer)
  • case 3. TargetValue gt current node
  • (assign CurrentPointer the value in the current
    nodes
  • right child pointer)
  • if(Foundfalse) then (declare the search a
    failure)
  • else (declare the search a
    success)

12
Printing a Search Tree
F
H
D
J
G
B
E
C
A
I
A, B, C, D, E, F, G,
H, I, J
13
Printing a Tree in Order
  • procedure PrintTree(Tree)
  • if ( Tree is not empty ) then
  • (apply PrintTree(left branch in tree)
  • print the root node of tree
  • apply PrintTree(right branch in tree) )

14
Inserting an Entry
H
N
E
P
K
B
G
J
H
N
E
P
K
B
G
M
J
15
Inserting an Entry (1)
  • procedure Insert(Tree, TargetValue)
  • assign CurrentPointer the value in the root
    pointer of Tree
  • assign Found the value false
  • while (Found is false and CurrentPointer is not
    NIL) do
  • case 1. TargetValue current node
  • (assign Found the value true)
  • case 2. TargetValue lt current node
  • (assign PreviousPointer the value of
    CurrentPointer
  • assign CurrentPointer the value in the current
    nodes left child pointer)
  • case 3. TargetValue gt current node
  • (assign PreviousPointer the value of
    CurrentPointer
  • assign CurrentPointer the value in the current
    nodes
  • right child pointer)

16
Inserting an Entry (2)
  • if (Foundfalse) then
  • (Create a new node containing TargetValue
  • case 1, CurrentPointer root pointer
  • (connect the new node as the root node)
  • case 2, TargetValue lt previous node
  • (connect the new node as the left child of the
    previous node)
  • case 3, TargetValue gt previous node
  • (connect the new node as the right
    child of the previous node)
  • )

17
Customized Data Types
  • struct
  • char Name8
  • int Age
  • float SkillRating
  • Employee
  • typedef struct
  • char Name8
  • int Age
  • float SkillRating
  • EmployeeType
  • EmployeeType Employee, DistManager, SalesRep1

18
Abstract Data Type
  • Data types
  • A predetermined storage system
  • A collection of predefined operations
  • Abstract data types
  • Define both storage system and operations

19
An Abstract Data Type in Ada
  • package STackPackage is
  • type StackOfIntegers is
  • record
  • StackEntries array(1..25) of integer
  • StackPointer integer
  • end record
  • procedure push(Value in integer Stack in out
  • StackOfIntegers)
  • procedure pop(Value out integer Stack in out
  • StackOfIntegers)
  • end StackPackage
  • StackOne StackOfIntegers
  • Push(106, StackOne)

20
Encapsulation
  • The package is constructed in such a manner that
    its internal structure can be accessed only by
    means of the approved package procedures
  • The integrity of these data types is protected
    from poorly conceived modifications

21
ADA Abstract Data Type with Encapsulation
package STackPackage is type StackOfIntegers is
private procedure push(Value in integer
Stack in out StackOfIntegers)
procedure pop(Value out integer Stack in out
StackOfIntegers) private type
StackOfIntegers is record
StackEntries array(1..25) of integer
StackPointer integer end record end
StackPackage StackOne StackOfIntegers Push(106,
StackOne)
22
Classes
  • Both abstract data types and classes bundles data
    structures with the procedures that manipulate
    those structures
  • Classes are more flexible
  • Procedures only
  • Data structures only
  • Both procedures and data structures or neither
  • Can be defined in terms of other classes through
    inheritance

23
C Class
  • const int MaxStack 25
  • class StackOfIntegers
  • int StackPointer
  • int StackEntriesMaxStack // Data structures
    are private
  • public // Access to following methods is
    public
  • StackOfIntegers() // This is a constructor.
    It
  • StackPointer 0 // initializes the stack as
    empty
  • void push(int Entry)
  • if( StackPointer lt MaxStack )
  • StackEntriesStackPointer Entry
  • int pop(void)
  • if (StackPointer gt 0) return StackEntries--Stac
    kPointer
  • else return 0

24
Java Class
  • class StackOfIntegers
  • private final int MaxStack 25 // Data
    structures are private
  • private int StackPointer
  • private int StackEntries
  • StackOfIntegers() // This is
    a constructor. It
  • StackEntries new int MaxStack //
    establishes an array of integers
  • StackPointer 0 // to hold the stack
  • public void push(int Entry)
  • if( StackPointer lt MaxStack )
  • StackEntriesStackPointer Entry
  • public int pop(void)
  • if (StackPointer gt 0) return
    StackEntries--StackPointer
  • else return 0

25
Pointers in Machine Language
  • Machine language defined in Appendix C
  • Load data (immediate addressing)
  • 2RXY
  • Load address (direct addressing)
  • 1RXY
  • Load through pointer (indirect addressing)
  • DRXY
  • DR0S
  • Save through pointer
  • ER0S

XY/S
R
26
Exercise
  • Review problems
  • 2, 5, 8, 14, 17, 21, 23, 26, 39, 42
  • A short essay on any Social Issues problem
Write a Comment
User Comments (0)
About PowerShow.com