Download presentation
Presentation is loading. Please wait.
Published byBethany Harmon Modified over 9 years ago
1
Linked Data Structures: Chapter 9, Slide 1 Linked data structures - versatile data structures to model complex real world situations and entities. List, stacks, trees, graphs etc. just to mention some. Consider a simple binary search tree for characters: Analyze chapter9_1 program:chapter9_1 Average time complexity to search for an item is O(n log n) Organization: Binary tree, left child has value less than the parent, the right child has value greater than the parent.
2
Chapter 9, Slide 2 J E K B DA NULL Depth first traversal will produce alphabetically ordered sequence: A B D E J K
3
Chapter 9, Slide 3 The nature of “links” -- usually pointers, but any reference will do: Analyze chapter9_2 program:chapter9_2 The references are array indexes (arrays are modeled on memory!). This will work fine for a tree with up to 100 nodes. A natural way to serialize a binary tree. Not necessarily are linked data structures created on the heap only: Analyze chapter9_3 program that is a recursive descent parser for a list of characters separated by commas and builds a binary search tree on the stack. chapter9_3 Though, it is not very practical. Most commonly, linked data structures are linked by pointers and are build on the heap.
4
Chapter 9, Slide 4 Pointer-based linked data structures are “flexible”, which is mostly good, however it is bad for “moving” the structure elsewhere in memory, or “transmitting” it over a communication channel, or “recording” it to auxiliary memory. compaction: we say that a linked data structure is compacted if it occupies a contiguous segment of memory and all pointers (addresses) are relative to the beginning of that segment. serialization: we say that a linked data structure is serialized if it occupies several contiguous segments of memory and all pointers (addresses) are relative to the beginning of that segment where the pointer is stored. Thus compaction is the extreme form of serialization. A serialized structure can easily by “moved” in memory just by “moving” the whole segment(s), “transmitted” byte by byte over a communication channel, or “recorded” to auxiliary memory and later restored.
5
Chapter 9, Slide 5 Illustration of serialization+allocation from arena: chapter9_4 program. chapter9_4 The “relativized” addresses are a pair of short integers, the first is segment+1 and the second is offset. Let us now visualize the arena after each stage of the process. First “building” the tree:
6
Chapter 9, Slide 6
7
Chapter 9, Slide 7
8
Chapter 9, Slide 8 The tree is build, now we start the relativization process:
9
Chapter 9, Slide 9 We deliberately designed the structure/class NODE so that it has size of 12 bytes, but 3 bytes are wasted on padding:
10
Chapter 9, Slide 10 We can compact the nodes with no space wasted: But then we cannot use p->lch or p->rch, we must have our custom-made access functions: analyze chapter9_5 program.chapter9_5
11
Chapter 9, Slide 11 After relativization: End of slides for chapter 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.