Title: Data Structures II
1Data Structures II
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Organization Chart
3Trees
4Trees
- Nodes
- Root
- Terminal (leaf)
- Parent, children, siblings
- Subtrees
- Depth
5Node in a Binary Tree
left
right
Data
6Linked Storage System
A
C
NIL
F
NIL
NIL
D
B
NIL
NIL
E
NIL
NIL
7Storage without Pointers
1
2
3
4
5
6
7
B
D
A
C
E
F
8Inefficient 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
9Binary Tree Package
- Search for the presence of an entry
- Print the list in alphabetical order
- Insert a new entry
10Ordered Tree
G
K
D
B
F
I
M
C
J
A
H
11Binary 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)
12Printing a Search Tree
F
H
D
J
G
B
E
C
A
I
A, B, C, D, E, F, G,
H, I, J
13Printing 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) )
14Inserting an Entry
H
N
E
P
K
B
G
J
H
N
E
P
K
B
G
M
J
15Inserting 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)
16Inserting 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) - )
17Customized Data Types
- struct
- char Name8
- int Age
- float SkillRating
- Employee
- typedef struct
- char Name8
- int Age
- float SkillRating
- EmployeeType
- EmployeeType Employee, DistManager, SalesRep1
18Abstract Data Type
- Data types
- A predetermined storage system
- A collection of predefined operations
- Abstract data types
- Define both storage system and operations
19An 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)
20Encapsulation
- 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
21ADA 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)
22Classes
- 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
23C 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
-
24Java 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
-
25Pointers 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
26Exercise
- Review problems
- 2, 5, 8, 14, 17, 21, 23, 26, 39, 42
- A short essay on any Social Issues problem