Data Structure is the specific method for sorting out the data in a system with the goal that it could be utilized efficiently. These can implement at least one specific abstract data types (ADT), which indicate the operations that can be performed on the data structure and the computational unpredictability of those operations. Copy the link given below and paste it in new browser window to get more information on Data Structure & Algorithms:- www.transtutors.com/homework-help/computer-science/data-structure-and-algorithms.aspx
The document discusses different data structures and their implementations and applications. It covers arrays, linked lists, stacks, queues, binary trees, and binary search. The key points are:
- Arrays allow fast access but have fixed size; linked lists can grow dynamically but access is slower.
- Binary trees allow fast (O(log n)) search, insertion, and deletion operations due to their hierarchical structure.
- Stacks and queues are useful for modeling LIFO and FIFO data access with applications like function calls and job scheduling.
- Binary search runs in O(log n) time by recursively dividing the search space for sorted data.
Binary trees are a data structure where nodes are connected in a parent-child relationship. Each node can have up to two children. Binary search trees have the additional property that the value of each node's left child is less than its own value, and the value of its right child is greater. This structure allows efficient search, insertion, and deletion operations. Basic tree operations include traversing the tree in different orders (pre-order, in-order, post-order) and searching for a node by its value. New nodes are inserted by finding the correct position based on value comparison to existing nodes.
This document discusses different data structures and their uses. It describes arrays, linked lists, trees, stacks, queues, and hash tables. For each scenario presented, it asks which data structure would be best. Scenario 1 involves eliminating every third person in a row, scenario 2 displaying recent missed calls, scenario 3 maintaining a student database with various operations, and scenario 4 prioritizing people in an office. The document provides examples and explanations of different data structures and their pros and cons for accessing, inserting, and deleting data. It emphasizes choosing the right data structure based on the needed operations.
This document provides an introduction to data structures. It defines data structures as a way of organizing data so that it can be used efficiently. The document then discusses basic terminology, why data structures are important, how they are studied, and how they are classified as simple or compound, and linear or non-linear. It proceeds to describe common data structures like arrays, stacks, queues, linked lists, trees, and graphs, and how they support basic operations. The document concludes by discussing how to select an appropriate data structure based on the problem constraints and required operations.
This document discusses different types of data structures. It defines data structures as representations of logical relationships between data elements and as mathematical models for organizing data. It describes primitive data structures like integers and floats that are directly operated on by machine instructions. Non-primitive structures like arrays and lists are more sophisticated structures derived from primitive ones. Linear data structures connect elements sequentially in memory, while nonlinear structures do not. Common linear structures discussed are stacks, which use LIFO insertion and deletion, and queues, which use FIFO insertion and deletion. Nonlinear structures include trees and graphs.
This document discusses topics related to data structures and algorithms. It covers structured programming and its advantages and disadvantages. It then introduces common data structures like stacks, queues, trees, and graphs. It discusses algorithm time and space complexity analysis and different types of algorithms. Sorting algorithms and their analysis are also introduced. Key concepts covered include linear and non-linear data structures, static and dynamic memory allocation, Big O notation for analyzing algorithms, and common sorting algorithms.
This document introduces data structures and their classifications. It defines data structure as a structured way of organizing data in a computer so it can be used efficiently. Data structures are classified as simple, linear, and non-linear. Linear structures like arrays, stacks, and queues store elements in a sequence while non-linear structures like trees and graphs have non-sequential relationships. The document discusses common operations on each type and provides examples of different data structures like linked lists, binary trees, and graphs. It concludes by noting data structures should be selected based on the nature of the data and requirements of operations.
Data Structure is the specific method for sorting out the data in a system with the goal that it could be utilized efficiently. These can implement at least one specific abstract data types (ADT), which indicate the operations that can be performed on the data structure and the computational unpredictability of those operations. Copy the link given below and paste it in new browser window to get more information on Data Structure & Algorithms:- www.transtutors.com/homework-help/computer-science/data-structure-and-algorithms.aspx
The document discusses different data structures and their implementations and applications. It covers arrays, linked lists, stacks, queues, binary trees, and binary search. The key points are:
- Arrays allow fast access but have fixed size; linked lists can grow dynamically but access is slower.
- Binary trees allow fast (O(log n)) search, insertion, and deletion operations due to their hierarchical structure.
- Stacks and queues are useful for modeling LIFO and FIFO data access with applications like function calls and job scheduling.
- Binary search runs in O(log n) time by recursively dividing the search space for sorted data.
Binary trees are a data structure where nodes are connected in a parent-child relationship. Each node can have up to two children. Binary search trees have the additional property that the value of each node's left child is less than its own value, and the value of its right child is greater. This structure allows efficient search, insertion, and deletion operations. Basic tree operations include traversing the tree in different orders (pre-order, in-order, post-order) and searching for a node by its value. New nodes are inserted by finding the correct position based on value comparison to existing nodes.
This document discusses different data structures and their uses. It describes arrays, linked lists, trees, stacks, queues, and hash tables. For each scenario presented, it asks which data structure would be best. Scenario 1 involves eliminating every third person in a row, scenario 2 displaying recent missed calls, scenario 3 maintaining a student database with various operations, and scenario 4 prioritizing people in an office. The document provides examples and explanations of different data structures and their pros and cons for accessing, inserting, and deleting data. It emphasizes choosing the right data structure based on the needed operations.
This document provides an introduction to data structures. It defines data structures as a way of organizing data so that it can be used efficiently. The document then discusses basic terminology, why data structures are important, how they are studied, and how they are classified as simple or compound, and linear or non-linear. It proceeds to describe common data structures like arrays, stacks, queues, linked lists, trees, and graphs, and how they support basic operations. The document concludes by discussing how to select an appropriate data structure based on the problem constraints and required operations.
This document discusses different types of data structures. It defines data structures as representations of logical relationships between data elements and as mathematical models for organizing data. It describes primitive data structures like integers and floats that are directly operated on by machine instructions. Non-primitive structures like arrays and lists are more sophisticated structures derived from primitive ones. Linear data structures connect elements sequentially in memory, while nonlinear structures do not. Common linear structures discussed are stacks, which use LIFO insertion and deletion, and queues, which use FIFO insertion and deletion. Nonlinear structures include trees and graphs.
This document discusses topics related to data structures and algorithms. It covers structured programming and its advantages and disadvantages. It then introduces common data structures like stacks, queues, trees, and graphs. It discusses algorithm time and space complexity analysis and different types of algorithms. Sorting algorithms and their analysis are also introduced. Key concepts covered include linear and non-linear data structures, static and dynamic memory allocation, Big O notation for analyzing algorithms, and common sorting algorithms.
This document introduces data structures and their classifications. It defines data structure as a structured way of organizing data in a computer so it can be used efficiently. Data structures are classified as simple, linear, and non-linear. Linear structures like arrays, stacks, and queues store elements in a sequence while non-linear structures like trees and graphs have non-sequential relationships. The document discusses common operations on each type and provides examples of different data structures like linked lists, binary trees, and graphs. It concludes by noting data structures should be selected based on the nature of the data and requirements of operations.
This document provides an overview of different searching techniques including sequential search, binary search, tree searching using binary search trees, hashing techniques, and general search trees. It defines key search terminology and summarizes algorithms for common search operations like search, insertion and deletion in various data structures that support efficient searching like binary search trees, B-trees, and hash tables. Examples are provided to illustrate search techniques and how they work.
This document introduces key concepts related to data structures. It defines data as numbers, alphabets and symbols that represent information. Data can be atomic like integers or composite like dates with multiple parts. Data types refer to the kind of data variables can hold, like integers or characters. Abstract data types are collections of data and operations that can manipulate the data, like structures in C. Data objects store values in a program. Data structures organize data so items can be stored and retrieved using a fixed technique, like arrays. Data structures can be primitive types available in languages or non-primitive types derived from primitive ones. They can also be linear, non-linear, static with memory allocated at load time, or dynamic with memory allocated during execution
This document provides an introduction and overview of data structures. It discusses how data structures organize data in computer memory and some common data structures like lists, arrays, stacks, queues, trees and graphs. It explains that programmers select appropriate data structures based on the nature of the data and operations needed. The document also covers topics like abstract data types, logical vs physical forms of data, algorithms, and problems versus programs.
This document discusses different data structures and their classifications. It defines data structures as ways of organizing data that consider both the stored elements and their relationships. Data structures can be linear (processed sequentially like arrays and linked lists) or non-linear (where insertion and deletion cannot be done linearly, like trees and graphs). Data structures are also classified as primitive (basic types operated on by machines) and non-primitive (more complex structures derived from primitive ones, like arrays, lists and files). Common operations on data structures include traversing, searching, inserting and deleting.
This document discusses elementary data organization, including primitive and non-primitive data types, data structures, and common data structure operations. It defines data as values assigned to entities, and information as meaningful, processed data. Primitive data types directly supported by machines are listed. Non-primitive data types require additional processing. Data structures arrange data in memory and include common examples like arrays and linked lists. Operations on data structures include traversing, searching, inserting, deleting, sorting, and merging. Data structures are classified as linear or non-linear based on how elements are arranged.
The document discusses trees and binary trees as data structures. It defines what a tree is, including parts like the root, parent, child, leaf nodes. It then defines binary trees as trees where each node has no more than two children. Binary search trees are introduced as binary trees where all left descendants of a node are less than or equal to the node and all right descendants are greater. The document concludes by discussing how to build a binary search tree class with Node objects.
The document discusses different methods of file and data storage in computers. It describes how data is organized and stored in both main memory and secondary storage. There are three main file organization structures - sequential, indexed, and hashed. Sequential files store records sequentially and can only be accessed in order. Indexed files use an index to map keys to record locations for faster retrieval. Hashed files apply a hash function to map keys directly to storage locations.
1. A data structure organizes and collects data in a way that allows for efficient operations on that data through relationships between data elements.
2. Data structures implement abstract data types which specify the operations and complexity of manipulating the data.
3. Common terminology includes data, data types, variables, records, programs, and abstract data types.
The document defines data structures and different types of data structures including linear and non-linear data structures. It provides examples of primitive and non-primitive data as well as physical and logical data structures. It also describes arrays, including one-dimensional, two-dimensional, and multi-dimensional arrays. The storage and addressing of these different array types is explained.
Mca ii dfs u-1 introduction to data structureRai University
This document provides an introduction to data structures. It defines data structures as a way of organizing and storing data in a computer so that it can be used efficiently. The document discusses different types of data structures including primitive, non-primitive, linear and non-linear structures. It provides examples of various data structures like arrays, linked lists, stacks, queues and trees. It also covers important concepts like time complexity, space complexity and Big O notation for analyzing algorithms. Common operations on data structures like search, insert and delete are also explained.
Data may be organized in many different ways; the logical or mathematical model of a particular organization of data is called "Data Structure". The choice of a particular data model depends on two considerations:
It must be rich enough in structure to reflect the actual relationships of the data in the real world.
The structure should be simple enough that one can effectively process the data when necessary.
Data Structure Operations
The particular data structure that one chooses for a given situation depends largely on the nature of specific operations to be performed.
The following are the four major operations associated with any data structure:
i. Traversing : Accessing each record exactly once so that certain items in the record may be processed.
ii. Searching : Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions.
iii. Inserting : Adding a new record to the structure.
iv. Deleting : Removing a record from the structure.
Primitive and Composite Data Types
Primitive Data Types are Basic data types of any language. In most computers these are native to the machine's hardware.
Some Primitive data types are:
Integer
The document discusses linear and non-linear data structures. It defines a data structure as a way of organizing data to be used effectively. Linear data structures like arrays, stacks, queues, and linked lists arrange data sequentially, allowing single traversal. Non-linear structures like trees and graphs arrange data hierarchically, requiring multiple traversals. Linear structures are easier to implement but use memory inefficiently, while non-linear structures use memory efficiently but are harder to implement. Examples and properties of various linear and non-linear data structures are provided.
This document discusses data structures and algorithms. It defines data types and data structures, and provides examples of common data structures like arrays, linked lists, stacks, queues, and trees. It also discusses operations on data structures like traversing, searching, inserting, and deleting. Algorithms are used to manipulate the data in data structures. The time and space complexity of algorithms are also introduced. Overall, the document provides an overview of key concepts related to data structures and algorithms.
The document discusses various sorting algorithms including exchange sorts like bubble sort and quicksort, selection sorts like straight selection sort, and tree sorts like heap sort. For each algorithm, it provides an overview of the approach, pseudocode, analysis of time complexity, and examples. Key algorithms covered are bubble sort (O(n2)), quicksort (average O(n log n)), selection sort (O(n2)), and heap sort (O(n log n)).
This document discusses various sorting algorithms:
1) Insertion sort works by inserting elements into a sorted list one by one in linear time but has quadratic worst-case time complexity.
2) Shell sort improves on insertion sort by allowing elements far apart to be compared and improved to O(n^7/6) average time.
3) Merge sort works by recursively dividing the list into halves and merging the sorted halves together, providing O(n log n) time.
4) Quicksort and heapsort also provide O(n log n) time by partitioning elements.
5) Distribution sorts like radix and bucket sort use the element values to distribute into buckets in linear time if buckets are small.
Bsc cs ii dfs u-1 introduction to data structureRai University
This document provides an introduction to data structures. It defines data structures as a way of organizing and storing data in a computer so it can be used efficiently. The document discusses different types of data structures including primitive, non-primitive, linear and non-linear structures. It provides examples of common data structures like arrays, linked lists, stacks, queues and trees. It also covers important concepts like time and space complexity analysis and Big O notation for analyzing algorithm efficiency.
Data structure comes with a number of algorithms. It works with different types of data and structures and organizes data to fulfill a specific purpose. It deals with algorithms, Algorithm design, Algorithm analysis, Graph algorithms, Equivalence relations, Hash functions, hash tables, Theory of computation, linked lists, stacks, queues, searching and sorting techniques, graph data structure, trees, recursion of algorithms. It is technical way of storing data by using some specific techniques in order to use data efficiently.
This document discusses data structures and binary trees. It begins with basic terminology used in trees such as root, node, degree, levels, and traversal methods. It then explains different types of binary trees including strictly binary trees and complete binary trees. Next, it covers binary tree representations using arrays and linked lists. Finally, it discusses operations that can be performed on binary search trees including insertion, searching, deletion, and traversing nodes using preorder, inorder and postorder traversal methods.
In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. https://apkleet.com
<a href="https://apkleet.com" >games apk </a>
The document discusses data structures and algorithms. It begins by defining data structures as organized data and allowed operations. It then discusses arrays, linked lists, binary trees, and their implementations. Arrays allow fast access but fixed size, while linked lists and trees can grow dynamically but have slower access. Binary tree operations like search, insert, and delete have logarithmic time complexity due to the tree height being logarithmic in the number of nodes. The document provides examples of various data structure implementations and traversals.
we are innovative,we are different,we are genius so they call us idiots
Visit us for movies,videos,documentaries,sports,funny pics and many more join www.globalidiots.com
This document provides an overview of different searching techniques including sequential search, binary search, tree searching using binary search trees, hashing techniques, and general search trees. It defines key search terminology and summarizes algorithms for common search operations like search, insertion and deletion in various data structures that support efficient searching like binary search trees, B-trees, and hash tables. Examples are provided to illustrate search techniques and how they work.
This document introduces key concepts related to data structures. It defines data as numbers, alphabets and symbols that represent information. Data can be atomic like integers or composite like dates with multiple parts. Data types refer to the kind of data variables can hold, like integers or characters. Abstract data types are collections of data and operations that can manipulate the data, like structures in C. Data objects store values in a program. Data structures organize data so items can be stored and retrieved using a fixed technique, like arrays. Data structures can be primitive types available in languages or non-primitive types derived from primitive ones. They can also be linear, non-linear, static with memory allocated at load time, or dynamic with memory allocated during execution
This document provides an introduction and overview of data structures. It discusses how data structures organize data in computer memory and some common data structures like lists, arrays, stacks, queues, trees and graphs. It explains that programmers select appropriate data structures based on the nature of the data and operations needed. The document also covers topics like abstract data types, logical vs physical forms of data, algorithms, and problems versus programs.
This document discusses different data structures and their classifications. It defines data structures as ways of organizing data that consider both the stored elements and their relationships. Data structures can be linear (processed sequentially like arrays and linked lists) or non-linear (where insertion and deletion cannot be done linearly, like trees and graphs). Data structures are also classified as primitive (basic types operated on by machines) and non-primitive (more complex structures derived from primitive ones, like arrays, lists and files). Common operations on data structures include traversing, searching, inserting and deleting.
This document discusses elementary data organization, including primitive and non-primitive data types, data structures, and common data structure operations. It defines data as values assigned to entities, and information as meaningful, processed data. Primitive data types directly supported by machines are listed. Non-primitive data types require additional processing. Data structures arrange data in memory and include common examples like arrays and linked lists. Operations on data structures include traversing, searching, inserting, deleting, sorting, and merging. Data structures are classified as linear or non-linear based on how elements are arranged.
The document discusses trees and binary trees as data structures. It defines what a tree is, including parts like the root, parent, child, leaf nodes. It then defines binary trees as trees where each node has no more than two children. Binary search trees are introduced as binary trees where all left descendants of a node are less than or equal to the node and all right descendants are greater. The document concludes by discussing how to build a binary search tree class with Node objects.
The document discusses different methods of file and data storage in computers. It describes how data is organized and stored in both main memory and secondary storage. There are three main file organization structures - sequential, indexed, and hashed. Sequential files store records sequentially and can only be accessed in order. Indexed files use an index to map keys to record locations for faster retrieval. Hashed files apply a hash function to map keys directly to storage locations.
1. A data structure organizes and collects data in a way that allows for efficient operations on that data through relationships between data elements.
2. Data structures implement abstract data types which specify the operations and complexity of manipulating the data.
3. Common terminology includes data, data types, variables, records, programs, and abstract data types.
The document defines data structures and different types of data structures including linear and non-linear data structures. It provides examples of primitive and non-primitive data as well as physical and logical data structures. It also describes arrays, including one-dimensional, two-dimensional, and multi-dimensional arrays. The storage and addressing of these different array types is explained.
Mca ii dfs u-1 introduction to data structureRai University
This document provides an introduction to data structures. It defines data structures as a way of organizing and storing data in a computer so that it can be used efficiently. The document discusses different types of data structures including primitive, non-primitive, linear and non-linear structures. It provides examples of various data structures like arrays, linked lists, stacks, queues and trees. It also covers important concepts like time complexity, space complexity and Big O notation for analyzing algorithms. Common operations on data structures like search, insert and delete are also explained.
Data may be organized in many different ways; the logical or mathematical model of a particular organization of data is called "Data Structure". The choice of a particular data model depends on two considerations:
It must be rich enough in structure to reflect the actual relationships of the data in the real world.
The structure should be simple enough that one can effectively process the data when necessary.
Data Structure Operations
The particular data structure that one chooses for a given situation depends largely on the nature of specific operations to be performed.
The following are the four major operations associated with any data structure:
i. Traversing : Accessing each record exactly once so that certain items in the record may be processed.
ii. Searching : Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions.
iii. Inserting : Adding a new record to the structure.
iv. Deleting : Removing a record from the structure.
Primitive and Composite Data Types
Primitive Data Types are Basic data types of any language. In most computers these are native to the machine's hardware.
Some Primitive data types are:
Integer
The document discusses linear and non-linear data structures. It defines a data structure as a way of organizing data to be used effectively. Linear data structures like arrays, stacks, queues, and linked lists arrange data sequentially, allowing single traversal. Non-linear structures like trees and graphs arrange data hierarchically, requiring multiple traversals. Linear structures are easier to implement but use memory inefficiently, while non-linear structures use memory efficiently but are harder to implement. Examples and properties of various linear and non-linear data structures are provided.
This document discusses data structures and algorithms. It defines data types and data structures, and provides examples of common data structures like arrays, linked lists, stacks, queues, and trees. It also discusses operations on data structures like traversing, searching, inserting, and deleting. Algorithms are used to manipulate the data in data structures. The time and space complexity of algorithms are also introduced. Overall, the document provides an overview of key concepts related to data structures and algorithms.
The document discusses various sorting algorithms including exchange sorts like bubble sort and quicksort, selection sorts like straight selection sort, and tree sorts like heap sort. For each algorithm, it provides an overview of the approach, pseudocode, analysis of time complexity, and examples. Key algorithms covered are bubble sort (O(n2)), quicksort (average O(n log n)), selection sort (O(n2)), and heap sort (O(n log n)).
This document discusses various sorting algorithms:
1) Insertion sort works by inserting elements into a sorted list one by one in linear time but has quadratic worst-case time complexity.
2) Shell sort improves on insertion sort by allowing elements far apart to be compared and improved to O(n^7/6) average time.
3) Merge sort works by recursively dividing the list into halves and merging the sorted halves together, providing O(n log n) time.
4) Quicksort and heapsort also provide O(n log n) time by partitioning elements.
5) Distribution sorts like radix and bucket sort use the element values to distribute into buckets in linear time if buckets are small.
Bsc cs ii dfs u-1 introduction to data structureRai University
This document provides an introduction to data structures. It defines data structures as a way of organizing and storing data in a computer so it can be used efficiently. The document discusses different types of data structures including primitive, non-primitive, linear and non-linear structures. It provides examples of common data structures like arrays, linked lists, stacks, queues and trees. It also covers important concepts like time and space complexity analysis and Big O notation for analyzing algorithm efficiency.
Data structure comes with a number of algorithms. It works with different types of data and structures and organizes data to fulfill a specific purpose. It deals with algorithms, Algorithm design, Algorithm analysis, Graph algorithms, Equivalence relations, Hash functions, hash tables, Theory of computation, linked lists, stacks, queues, searching and sorting techniques, graph data structure, trees, recursion of algorithms. It is technical way of storing data by using some specific techniques in order to use data efficiently.
This document discusses data structures and binary trees. It begins with basic terminology used in trees such as root, node, degree, levels, and traversal methods. It then explains different types of binary trees including strictly binary trees and complete binary trees. Next, it covers binary tree representations using arrays and linked lists. Finally, it discusses operations that can be performed on binary search trees including insertion, searching, deletion, and traversing nodes using preorder, inorder and postorder traversal methods.
In computer science, a data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data. https://apkleet.com
<a href="https://apkleet.com" >games apk </a>
The document discusses data structures and algorithms. It begins by defining data structures as organized data and allowed operations. It then discusses arrays, linked lists, binary trees, and their implementations. Arrays allow fast access but fixed size, while linked lists and trees can grow dynamically but have slower access. Binary tree operations like search, insert, and delete have logarithmic time complexity due to the tree height being logarithmic in the number of nodes. The document provides examples of various data structure implementations and traversals.
we are innovative,we are different,we are genius so they call us idiots
Visit us for movies,videos,documentaries,sports,funny pics and many more join www.globalidiots.com
1. The document defines and provides examples of various data structures including lists, stacks, queues, trees, and their properties.
2. Key concepts covered include linear and non-linear data structures, common tree types, tree traversals, and operations on different data structures like insertion, deletion, and searching.
3. Examples are provided to illustrate concepts like binary search trees, tree representation and traversal methods.
Master of Computer Application (MCA) – Semester 4 MC0080Aravind NC
This document describes several sorting algorithms and asymptotic analysis techniques. It discusses bubble sort, selection sort, insertion sort, shell sort, heap sort, merge sort, and quick sort as sorting algorithms. It then explains asymptotic notation such as Big-O, Big-Omega, and Theta to describe the time complexity of algorithms. Finally, it asks questions about Fibonacci heaps, binomial heaps, Strassen's matrix multiplication algorithm, and formalizing a greedy algorithm.
Splay trees are a self-adjusting binary search tree data structure. They perform basic operations like insertion, lookup, and removal in O(log n) amortized time by splaying nodes up the tree after access. This has the effect of moving frequently accessed nodes closer to the root over time. The document discusses the differences between splay trees and AVL trees, how splaying is done through different rotation cases, and how amortized analysis shows splay trees have O(log n) performance on average across operations.
This document discusses tree data structures and binary search trees. It begins with definitions of trees and their components like nodes, edges, and root/child relationships. It then covers tree traversals like preorder, inorder and postorder. Next, it defines binary search trees and their properties for storing and retrieving data efficiently. It provides examples of inserting, searching for, and deleting nodes from binary search trees. Searching and insertion are straightforward, while deletion has three cases depending on the number of child nodes.
Hi,Based on the Data We can Use diffrent type of Data Structure..pdfaradhana9856
Hi,
Based on the Data We can Use diffrent type of Data Structure.
Heap:
Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses
because Quicksort is better in practice. Nevertheless, the Heap data structure itself is enormously
used. Following are some uses other than Heapsort.
Priority Queues: Priority queues can be efficiently implemented using Binary Heap because it
supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time.
Binomoial Heap and Fibonacci Heap are variations of Binary Heap. These variations perform
union also in O(logn) time which is a O(n) operation in Binary Heap. Heap Implemented priority
queues are used in Graph algorithms like Prim’s Algorithm and Dijkstra’s algorithm.
1) Arrays
Arrays are a very simple data structure, and may be thought of as a list of a fixed length.
An array might also be thought of as a pre-bound pad of paper. It has a fixed number of pages,
each page holds information, and is in a predefined location that never changes.
Arrays can be used for Less storage data.if we know the fixed size.
2.Linked Lists
A linked list is a data structure that can hold an arbitrary number of data items, and can easily
change size to add or remove items. A linked list, at its simplest, is a pointer to a data node. Each
data node is then composed of data (possibly a record with several data values), and a pointer to
the next node. At the end of the list, the pointer is set to null.
By nature of its design, a linked list is great for storing data when the number of items is either
unknown, or subject to change. However, it provides no way to access an arbitrary item from the
list, short of starting at the beginning and traversing through every node until you reach the one
you want. The same is true if you want to insert a new node at a specific location.
3.Queues
A queue is a data structure that is best described as \"first in, first out\". A real world example of
a queue is people waiting in line at the bank. As each person enters the bank, he or she is
\"enqueued\" at the back of the line. When a teller becomes available, they are \"dequeued\" at
the front of the line.
4.Trees
Trees are a data structure consisting of one or more data nodes. The first node is called the
\"root\", and each node has zero or more \"child nodes\". The maximum number of children of a
single node, and the maximum depth of children are limited in some cases by the exact type of
data represented by the tree.
One of the most common examples of a tree is an XML document. The top-level document
element is the root node, and each tag found within that is a child. Each of those tags may have
children, and so on. At each node, the type of tag, and any attributes, constitutes the data for that
node. In such a tree, the hierarchy and order of the nodes is well defined, and an important part
of the data itself. Another good example of a tree is a written outline. The entire outline itself is .
The document discusses data structures and algorithms related to trees. It begins by defining key terms related to trees such as root, parent, child, leaf, etc. It then discusses binary trees and binary search trees, including their representations, basic operations like search and insert, and algorithms for those operations. It also covers tree traversal methods like preorder, inorder and postorder traversal. Finally, it discusses heaps and Huffman coding.
The document discusses data structures and linked lists. It defines data structures as logical ways of organizing and storing data in computer memory for efficient use. Linked lists are introduced as a linear data structure where elements are connected via pointers and can grow/shrink dynamically. Algorithms for traversing, inserting, and deleting nodes from singly linked lists using both recursion and iteration are provided with pseudocode.
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
This document discusses dictionaries and binary search trees. It describes the basic operations for dictionaries like search, insert, delete, min, max, successor, and predecessor. It then explains how to implement these operations using binary search trees. The time complexity of each operation is O(h) where h is the height of the tree, which can be O(lg n) for a balanced tree but O(n) in the worst case for an unbalanced tree. Balanced search trees like red-black trees and AVL trees help guarantee O(lg n) time for operations.
This document discusses binary trees and binary search trees. It defines key tree terms like root, child, parent, leaf nodes. It explains tree traversal methods like preorder, inorder and postorder. It also covers basic BST operations like insertion, search and deletion of nodes. Code snippets are provided to implement these operations and traversal methods on a sample binary search tree. Examples of traversing a sample tree in different orders are given.
This document introduces binary trees and provides a series of practice problems of increasing difficulty related to binary trees. It begins with an introduction to binary tree structure and terminology. It then provides 14 binary tree problems with descriptions and hints for solving each problem. The problems involve tasks like counting nodes, finding minimum/maximum values, printing trees in different orders, checking for paths with a given sum, and printing all root-to-leaf paths. Sample solutions are provided in subsequent sections in C/C++ and Java languages.
This document introduces binary trees and provides sample code for basic operations like lookups and inserts. It discusses the structure of binary trees, with nodes containing left/right pointers and data. Binary search trees require that all left subtree nodes are less than the parent and right greater. Sample C/C++ code is given for lookup and insert functions that demonstrate the recursive traversal and modification of the tree. The document aims to prepare the reader to solve practice problems of increasing difficulty involving binary trees and pointers.
Coding Assignment 3CSC 330 Advanced Data Structures, Spri.docxmary772
Coding Assignment 3
CSC 330: Advanced Data Structures, Spring 2019
Released Monday, April 15, 2019
Due on Canvas on Wednesday, May 1, at 11:59pm
Overview
In this assignment, you’ll implement another variant of a height-balancing tree known as a
splay tree. The assignment will also give you an opportunity to work with Java inheritance;
in particular, the base code that you’ll amend is structured so that your SplayTree class
extends from an abstract class called HeightBalancingTree, which gives a general template
for how a height-balancing tree should be defined.
As always, please carefully read the entire write-up before you begin coding your submission.
Splay Trees
As mentioned above, a splay tree is another example of a height-balancing tree — a binary
search tree that, upon either an insertion or deletion, modifies the tree through a sequence
of rotations in order to reduce the overall height of the tree.
However, splay trees differ from the other height-balancing trees we’ve seen (AVL trees,
red-black trees) in terms of the type of guarantees that they provide. In particular, recall
that both AVL trees and red-black trees maintain the property that after any insertion or
deletion, the height of the tree is O(log n), where n is the number of elements in the tree.
Splay trees unfortunately do not provide this (fairly strong) guarantee; namely, it is possible
for the height of a splay tree to become greater than O(log n) over a sequence of insertions
and deletions.
Instead, splay trees provide a slightly weaker (though still meaningful) guarantee known as
an amortized bound, which is essentially just a bound on the average time of a single opera-
tion over the course of several operations. In the context of splay trees, one can show that
over the course of, say, n insertions to build a tree with n elements, the average time of each
of these operations is O(log n) (but again, keeping in mind it is possible for any single one
of these operations to take much longer than this).
Showing this guarantee is beyond the scope of this course (although the details of the analy-
sis can be found in your textbook). Instead, in this assignment, we will just be in interested
1
r splay:
N
root
root
2
1
1
2
l splay:
N
1
2
rr splay:
N
N
N
ll splay:
rl splay:
1
2
N
lr splay:
Figure 1: Illustration of the six possible cases for on a given step of a splay operation.
in writing an implementation of a splay tree in Java that is structured using inheritance.
Splay Tree Insertions and Deletions
To insert or delete an element from the tree, splay trees use the same approach as the other
height-balancing trees we’ve discussed in class — first we insert/deletion an element using
standard BST procedures, and then perform a “height-fixing” procedure that rebalances the
tree. Thus, what distinguishes each of these height-balancing trees from one another is how
they define their height-fixing procedures.
To fix the tree after both inser.
This document discusses the key concepts and operations related to linked lists. It describes the different types of linked lists including singly linked lists, doubly linked lists, circular linked lists, and circular doubly linked lists. It provides algorithms for common linked list operations like insertion, deletion, and traversal. Memory allocation and various applications of linked lists are also covered.
This document provides an overview of clustering techniques, including supervised vs. unsupervised learning, clustering concepts, non-hierarchical clustering like k-means, and hierarchical clustering like hierarchical agglomerative clustering. It discusses clustering applications, algorithms like k-means and hierarchical agglomerative clustering, and evaluation metrics like cluster silhouettes. Key clustering goals are to partition unlabeled data into clusters such that examples within a cluster are similar and different between clusters.
This document provides an introduction to data structures. It discusses primitive and non-primitive data structures and their classifications. Linear data structures like arrays, stacks, queues and linked lists are covered, along with non-linear structures like trees and graphs. Common operations on data structures like traversing, searching, inserting and deleting are also summarized. Finally, the document introduces abstract data types and provides examples of common ADT specifications for lists, stacks and queues.
Von Gleichungen zu Funktionen
Überblick über ganzrationale Funktionen
Koordinatensysteme für Graphen ganzrationaler Funktionen mit Excels Liniendiagrammen erstellen
Einfache Analyse ganzrationaler Funktionen anhand deren Graphen
Von linearen zu quadratischen Gleichungssystemen
Verschiedene Möglichkeiten quadratische Gleichungssysteme grafisch zu lösen
Koordinatensysteme für quadratische Graphen mit Excels Liniendiagrammen erstellen und Gleichungen damit grafisch lösen
Grafische Interpretation der Lösungen von Normalparabel-Gerade Kombinationen
Diagramme
Einführung linearer Gleichungssysteme mit zwei Unbekannten
Rechnerische und grafische Lösung linearer Gleichungssysteme
Wertetabellen mit Excel erstellen
Koordinatensysteme und lineare Graphen mit Excels Liniendiagrammen erstellen und Gleichungen damit grafisch lösen
Mit großen Tabellen arbeiten
Sortieren und Filtern
Objekte einfügen
Formeln und rechnen mit Excel, insbesondere Zahlen und Textbearbeitung
Relative und absolute Zellbezüge
Funktionen: SUMME(), ANZAHL(), MIN(), MAX(), MITTELWERT(), JETZT(), HEUTE(), ZUFALLSZAHL(), PI() und SUMMEWENN()
Mathematische Probleme in Tabellenform
Geschichtliche Entwicklung
Grundlegende Konzepte und Begriffe in Excel
Selektion, Dateneingabe und Datentypen
Zellformatierung und Inhaltsformatierung
This document discusses the history and evolution of graphical user interfaces (GUIs) in web browsers. It begins by explaining the concepts of vector-based and raster-based graphics. It then describes early approaches using bare HTML pages, which felt like navigation rather than a true application. Plugins were introduced to provide richer content but had problems with resources, installation, and security. Finally, the document introduces HTML5 Canvas as a solution for continuous, event-driven drawing without plugins, allowing single-page web applications.
The document discusses different approaches to drawing graphics on a computer screen, including raster-based and vector-based graphics. It focuses on raster-based drawing APIs and their evolution from native platform-specific APIs to platform-independent APIs in web browsers. Key developments included the use of browser plugins, problems with plugins, and the introduction of HTML5 Canvas which enables interactive drawing in browsers without plugins.
- Wires and Bulbs
- Batch Processing
- Terminal and Mainframe
- From the Command-Line to Killer Applications
- Vector Displays and Raster Displays
- Color Displays
- The Mouse and the Takeoff of Interactivity
- The Desktop Metaphor
- Wires and Bulbs
- Batch Processing
- Terminal and Mainframe
- From the Command-Line to Killer Applications
- Vector Displays and Raster Displays
- Color Displays
- The Mouse and the Takeoff of Interactivity
- The Desktop Metaphor
This document discusses new features in C# 4 including home-brew dynamic dispatch using the DynamicObject class. It allows implementing custom dynamic behavior by overriding methods like TryInvokeMember. The document also covers hosting scripting languages with the Dynamic Language Runtime (DLR), including IronPython, IronRuby, and IronScheme. Dynamic dispatch enables seamless collaboration and controlled isolation between .NET and DLR-based languages.
This document discusses new features in C# 4 related to dynamic coding. It describes how dynamic typing allows easier COM automation without needing interop types. Anonymous type instances can now be passed to methods using dynamic parameters. The document also discusses dynamic objects in JavaScript and how ExpandoObjects in .NET allow objects to have dynamically added and modified properties at runtime similar to dynamic objects in JavaScript.
This document discusses the Dynamic Language Runtime (DLR) and dynamic coding features in C# 4. It provides an overview of the DLR and how it allows interoperability between statically and dynamically typed languages on the .NET framework. The DLR transforms dynamic operations in C# into calls to the DLR at compile time and handles dynamic dispatch at runtime. It uses expression trees to represent operations in a language-agnostic way and caches binding results for improved performance.
This document discusses new features related to dynamic programming in C# 4. It begins by explaining why dynamic typing is an important new feature in .NET 4 due to increasing needs for interoperability. It then provides an overview of dynamic typing concepts like late binding and duck typing. The document shows how these concepts are implemented in VB for dynamic programming and how C#4 introduces dynamic typing capabilities through the new "dynamic" keyword and Dynamic Language Runtime (DLR) while still being a statically typed language. It discusses the basic syntax for using dynamics in C#4 and some restrictions.
This document summarizes new generic types and features in C# 4, including Lazy<T> for deferred initialization, tuples for ad-hoc data structures, and generic variance. Generic variance allows covariant and contravariant conversions between generic types to promote substitutability. It was enabled for interfaces and delegates in C# 4 through explicit variance declarations like "out T" and "in T". This improves type safety over arrays, which allow unsafe covariance in C#.
This document discusses LINQ (Language Integrated Query) features in C#, including introducing LINQ to Objects, basic query expressions for projection and selection, and anonymous types. It provides examples of how LINQ to Objects maps to extension methods, functional and declarative expressibility using LINQ, details about the LINQ translation process from query expressions to method calls, and examples of using anonymous types.
The document discusses LINQ and C# algorithms. It compares the C++ STL algorithm model to the .NET algorithm model using IEnumerable<T> sequences and extension methods. Key points include:
- The .NET model uses deferred execution via IEnumerable<T> sequences and iterator blocks, avoiding intermediate collections and allowing multiple operations to be chained.
- Extension methods allow algorithms to be expressed via method chaining in a declarative way rather than using loops.
- IEnumerable<T> sequences are more readable, uniform, and composable than the STL model due to chaining and deferred execution.
This document summarizes new C# 3.0 features including implicit typing, lambda expressions, and extension methods. Implicit typing allows declaring variables without an explicit type using the 'var' keyword. Lambda expressions provide a concise way to pass code as arguments using the '=>' operator. Extension methods allow adding methods to existing types without modifying them by defining methods in static classes that take the extended type as the first parameter.
Sudheer Mechineni, Head of Application Frameworks, Standard Chartered Bank
Discover how Standard Chartered Bank harnessed the power of Neo4j to transform complex data access challenges into a dynamic, scalable graph database solution. This keynote will cover their journey from initial adoption to deploying a fully automated, enterprise-grade causal cluster, highlighting key strategies for modelling organisational changes and ensuring robust disaster recovery. Learn how these innovations have not only enhanced Standard Chartered Bank’s data infrastructure but also positioned them as pioneers in the banking sector’s adoption of graph technology.
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slackshyamraj55
Discover the seamless integration of RPA (Robotic Process Automation), COMPOSER, and APM with AWS IDP enhanced with Slack notifications. Explore how these technologies converge to streamline workflows, optimize performance, and ensure secure access, all while leveraging the power of AWS IDP and real-time communication via Slack notifications.
Unlocking Productivity: Leveraging the Potential of Copilot in Microsoft 365, a presentation by Christoforos Vlachos, Senior Solutions Manager – Modern Workplace, Uni Systems
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
20 Comprehensive Checklist of Designing and Developing a WebsitePixlogix Infotech
Dive into the world of Website Designing and Developing with Pixlogix! Looking to create a stunning online presence? Look no further! Our comprehensive checklist covers everything you need to know to craft a website that stands out. From user-friendly design to seamless functionality, we've got you covered. Don't miss out on this invaluable resource! Check out our checklist now at Pixlogix and start your journey towards a captivating online presence today.
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfMalak Abu Hammad
Discover how MongoDB Atlas and vector search technology can revolutionize your application's search capabilities. This comprehensive presentation covers:
* What is Vector Search?
* Importance and benefits of vector search
* Practical use cases across various industries
* Step-by-step implementation guide
* Live demos with code snippets
* Enhancing LLM capabilities with vector search
* Best practices and optimization strategies
Perfect for developers, AI enthusiasts, and tech leaders. Learn how to leverage MongoDB Atlas to deliver highly relevant, context-aware search results, transforming your data retrieval process. Stay ahead in tech innovation and maximize the potential of your applications.
#MongoDB #VectorSearch #AI #SemanticSearch #TechInnovation #DataScience #LLM #MachineLearning #SearchTechnology
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIVladimir Iglovikov, Ph.D.
Presented by Vladimir Iglovikov:
- https://www.linkedin.com/in/iglovikov/
- https://x.com/viglovikov
- https://www.instagram.com/ternaus/
This presentation delves into the journey of Albumentations.ai, a highly successful open-source library for data augmentation.
Created out of a necessity for superior performance in Kaggle competitions, Albumentations has grown to become a widely used tool among data scientists and machine learning practitioners.
This case study covers various aspects, including:
People: The contributors and community that have supported Albumentations.
Metrics: The success indicators such as downloads, daily active users, GitHub stars, and financial contributions.
Challenges: The hurdles in monetizing open-source projects and measuring user engagement.
Development Practices: Best practices for creating, maintaining, and scaling open-source libraries, including code hygiene, CI/CD, and fast iteration.
Community Building: Strategies for making adoption easy, iterating quickly, and fostering a vibrant, engaged community.
Marketing: Both online and offline marketing tactics, focusing on real, impactful interactions and collaborations.
Mental Health: Maintaining balance and not feeling pressured by user demands.
Key insights include the importance of automation, making the adoption process seamless, and leveraging offline interactions for marketing. The presentation also emphasizes the need for continuous small improvements and building a friendly, inclusive community that contributes to the project's growth.
Vladimir Iglovikov brings his extensive experience as a Kaggle Grandmaster, ex-Staff ML Engineer at Lyft, sharing valuable lessons and practical advice for anyone looking to enhance the adoption of their open-source projects.
Explore more about Albumentations and join the community at:
GitHub: https://github.com/albumentations-team/albumentations
Website: https://albumentations.ai/
LinkedIn: https://www.linkedin.com/company/100504475
Twitter: https://x.com/albumentations
Building RAG with self-deployed Milvus vector database and Snowpark Container...Zilliz
This talk will give hands-on advice on building RAG applications with an open-source Milvus database deployed as a docker container. We will also introduce the integration of Milvus with Snowpark Container Services.
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024Neo4j
Neha Bajwa, Vice President of Product Marketing, Neo4j
Join us as we explore breakthrough innovations enabled by interconnected data and AI. Discover firsthand how organizations use relationships in data to uncover contextual insights and solve our most pressing challenges – from optimizing supply chains, detecting fraud, and improving customer experiences to accelerating drug discoveries.
In the rapidly evolving landscape of technologies, XML continues to play a vital role in structuring, storing, and transporting data across diverse systems. The recent advancements in artificial intelligence (AI) present new methodologies for enhancing XML development workflows, introducing efficiency, automation, and intelligent capabilities. This presentation will outline the scope and perspective of utilizing AI in XML development. The potential benefits and the possible pitfalls will be highlighted, providing a balanced view of the subject.
We will explore the capabilities of AI in understanding XML markup languages and autonomously creating structured XML content. Additionally, we will examine the capacity of AI to enrich plain text with appropriate XML markup. Practical examples and methodological guidelines will be provided to elucidate how AI can be effectively prompted to interpret and generate accurate XML markup.
Further emphasis will be placed on the role of AI in developing XSLT, or schemas such as XSD and Schematron. We will address the techniques and strategies adopted to create prompts for generating code, explaining code, or refactoring the code, and the results achieved.
The discussion will extend to how AI can be used to transform XML content. In particular, the focus will be on the use of AI XPath extension functions in XSLT, Schematron, Schematron Quick Fixes, or for XML content refactoring.
The presentation aims to deliver a comprehensive overview of AI usage in XML development, providing attendees with the necessary knowledge to make informed decisions. Whether you’re at the early stages of adopting AI or considering integrating it in advanced XML development, this presentation will cover all levels of expertise.
By highlighting the potential advantages and challenges of integrating AI with XML development tools and languages, the presentation seeks to inspire thoughtful conversation around the future of XML development. We’ll not only delve into the technical aspects of AI-powered XML development but also discuss practical implications and possible future directions.
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
Communications Mining Series - Zero to Hero - Session 1DianaGray10
This session provides introduction to UiPath Communication Mining, importance and platform overview. You will acquire a good understand of the phases in Communication Mining as we go over the platform with you. Topics covered:
• Communication Mining Overview
• Why is it important?
• How can it help today’s business and the benefits
• Phases in Communication Mining
• Demo on Platform overview
• Q/A
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...SOFTTECHHUB
The choice of an operating system plays a pivotal role in shaping our computing experience. For decades, Microsoft's Windows has dominated the market, offering a familiar and widely adopted platform for personal and professional use. However, as technological advancements continue to push the boundaries of innovation, alternative operating systems have emerged, challenging the status quo and offering users a fresh perspective on computing.
One such alternative that has garnered significant attention and acclaim is Nitrux Linux 3.5.0, a sleek, powerful, and user-friendly Linux distribution that promises to redefine the way we interact with our devices. With its focus on performance, security, and customization, Nitrux Linux presents a compelling case for those seeking to break free from the constraints of proprietary software and embrace the freedom and flexibility of open-source computing.
2. 2
2
●
Collections – Part V
– Implementation Strategies of Map/Dictionary-like associative Collections
●
BST-based Implementation
●
Hashtable-based Implementation
– A 20K Miles Perspective on Trees in Computer Science
– Equality
– Multimaps
– Set-like associative Collections
TOC
3. 3
3
●
In the last lecture we learned about associative collections.
– Associative collections allow associating keys with values.
●
Example: In white pages names are associated to phone numbers.
– Associative collections allow to lookup a value for a certain key, e.g. getting the phone number of a specific person.
– Associative collections could be implemented with two arrays as shown before, but this would be very inefficient for common cases.
●
In this lecture we're going to understand how associative collectionsare implemented.
●
We'll start with SortedDictionary, which was introduced in the last lecture. We already know about SortedDictionary that
– it allows only one value per key,
– that it organizes its keys by equivalence,
– that a .Net type provides equivalence by implementing IComparable or delegating equivalence to objects implementing IComparer.
●
Ok, the organization of keys is based on equivalence, but when a key is in aSortedDictionary, where does it have to "be"?
– I.e. where and how will it be stored?
– We're going to understand this in this lecture!
Implementation of Associative Collections
4. 4
4
●
In opposite to indexed/sequential collections, items got and set by analyzing their relationships in associative collections.
●
The idea of SortedDictionary is to keep the contained items sorted by key whilst adding items!
– So, its way of key organization is the analysis of the sort order of keys of the items.
●
To make this happen, SortedDictionary is implemented with a tree-like storage organization for the keys.
– When we analyzed algorithms we learned that the most efficient sorting algorithms can be visualized and analyzed with trees.
●
I.e. those implementing "divide and conquer", like mergesort and quicksort.
●
Before we discuss SortedDictionary's tree-based implementation, we're going to talk about alternative implementations.
●
Sidebar: Java's implementation of a sorted associative collection is calledTreeMap. It implements the interface Map.
Associative Collections – What's in a SortedDictionary?
5. 5
5
●
SortedDictionary could be implemented with a sorted list.
– Finding would be fast (O(logn)), but inserting would be costly (O(n), because of resizing).
– It is simple to code.
●
SortedDictionary could be implemented with a sorted linked list.
– Inserting would be fast if the location is known (O(1)), but finding would be costly
(O(n), finding involves the iteration of a linked list).
– More memory per item is required (e.g. one item plus a few references, whereby
(sorted) lists don't (just one item, due to continuous memory)).
●
Problems of these approaches:
– For lists continuos memory is the problem. → It makes the data structure rigid. → Resizing required.
– For linked lists finding items is a problem due to the loose structure. → "Linked iteration"
●
Let's finally discuss how we come from sorted lists and sorted linked lists to the real implementationof SortedDictionary.
"Helen"
"James""Gil""Clark""Ed"
"Clark" "Ed" "Gil" "James"
null
"Helen"
SortedDictionary – two alternative Implementations
6. 6
6
●
When we examine a sorted linked list we'll find an interesting way of thinking about finding items.
– If we had a reference to the middle item, we could determine the "direction" or half to insert a new item to keep the linked list sorted.
– Therefor it would be nice to allow moving into both directions, so we should use a sorted doubly linked list (3rd
alternative)!
– The idea of referencing only the middle item of an already sorted doubly linked list can be recursively applied to the sorted halves.
– The resulting structure is a tree, on which the real implementation of SortedDictionary is built upon!
"Clark" "Ed" "Helen" "James""Gil"
"Clark" "Ed" "Helen" "James""Gil"
"Bud" "Will"
null
"Will"
null
"Bud"
null
null"Clark"
"Ed"
null
"Helen"
"James"null null
"Gil"
"Will"
nullnullnull
"Bud"
null
"Kyle"
"Kyle"
"Kyle"
"Kyle"
SortedDictionary – Coming to the real Implementation
7. 7
7
●
Trees represent one of the many data structures available in cs (others are graphs, relational, heap, stack etc.)
– Similar to collections data structures can be categorized in different ways, e.g. a tree is a special form of a graph.
●
Basic features of trees:
– Trees are recursive branching structures consisting of linked nodes.
– Trees have only one root node.
●
In cs the root of a tree is written on the top at graphical representations.
– A node can be reached by exactly one path from the root.
●
This is only true for linear paths, in xpath there exist various expressions to select any node from the root.
●
Trees represent data that has a hierarchy. This is very common in computer business.
– In the last example the hierarchy was the order: left => less than root, right => greater than root.
– Directory and file structures.
– A function call hierarchy: a call stack.
– "Has a" and "is a" associations in object-oriented systems.
– The structure of an XML file.
Trees in Computer Science (cs)
8. 8
8
●
The final solution we found to handle sorted collections is a special form of a tree, abinary search tree (BST).
– The implementation of SortedDictionary is based on a BST (BST-based).
– The core strategy of BSTs is to maintaining pointers/references to the middle of subtrees.
●
Here some general terminology on trees (seen from "James"):
– "James" is a node or cell. The arrows can be called edges.
– "James" has two children.
●
Below "James" there is a subtree of two children.
– "James"' parent is "Gil".
– "Gil" is the root of the tree.
●
In computer science trees are evolving upside down, having the root at the top.
– "Will" is a leaf, i.e. it has no children at all.
●
A node having only one child is sometimes called half-leaf.
– "Clark" and "James" are siblings.
●
The BST is called "binary", because each node has at most two children.
– A binary search can be issued on a BST in a very simple manner.
null"Clark"
"Ed"
null
"Helen"
"James"null null
"Gil"
"Will"
nullnullnull
"Bud"
null
Trees – Terminology
● There also exist ternary, n-ary etc. trees.
9. 9
9
●
Basically all operations on trees involve recursion!
●
The iteration of a tree is called traversal. It means to visit every node in the tree from a certain starting node.
– (1) Do something with the current node.
– (2) If the left (or right node) is not null make it the current node and continue with (1). → Recurse!
●
There are three classical ways to traverse (binary) trees:
– Inorder: visit the left node, then the root and finally the right node.
– Postorder: visit the left node, then the right node and finally the root.
– Preorder/depth-first: visit the root, then the left node and finally the right node.
●
The most important traversal for BSTs is inorder, because it visits the nodes in sorted order.
– Notice, how the way of traversal could be encapsulated behind the iterator design pattern!
public class Node { // Mind the recursive definition!
public string Value { get; set;}
public Node Left { get; set;}
public Node Right { get; set;}
}
public static void PrintTreePostorder(Node root) {
if (null != root) {
PrintTreePostorder(root.Left); // Recurse!
PrintTreePostorder(root.Right);// Recurse!
Console.WriteLine(root.Value);
}
}
public static void PrintTreeInorder(Node root) {
if (null != root) {
PrintTreeInorder(root.Left); // Recurse!
Console.WriteLine(root.Value);
PrintTreeInorder(root.Right); // Recurse!
}
}
public static void PrintTreePreorder(Node root) {
if (null != root) {
Console.WriteLine(root.Value);
PrintTreePreorder(root.Left); // Recurse!
PrintTreePreorder(root.Right); // Recurse!
}
}
Traversal of Trees
● The deletion of all nodes in a BST needs to be
done postorder.
● Among others there also exists "level-order",
which is also called "breadth-first", it iterates all
nodes beginning at the root from left to right.
10. 10
10
●
So SortedDictionary is implemented with a BST.
– The string that was used in each node in former examples, plays the role of SortedDictionary's search key.
– The nodes of SortedDictionary have one more field: the value that is associated with the search key.
●
Using a BST in the implementation allows fast implementations to find items.
– E.g. the methods Add() and ContainsKey() apply a binary search on SortedDictionary's BST to locate items.
– The involved search algorithms are implemented recursively.
●
Complexity.
– The costs for finding one item only depends on the height of the tree.
– We've already discussed that a tree's height can be retrieved by log n.
– This is valid for perfectly balanced trees.
– Other costs like swapping points will not be
taken into consideration as always.
– This makes the complexity for finding (and
related operations) O(log n) for BSTs.
●
Max. ten comparisons to find an item in 1000 items.
null"Clark"
"Ed"
null
"Helen"
"James"null null
"Gil"
"Will"
nullnullnull
"Bud"
null
"Kyle"
"Kyle"
(Node)
8758 value
null root
* left
* right
"Gil" key
SortedDictionary – final Words
● Looking at the key we know on which half an item
has to be inserted or found. (left => less than root;
right => greater than root, this makes divide and
conquer work).
● BSTs can be kept balanced internally to maintain
logarithmic behavior, this yields an additional cost
factor.
11. 11
11
Associative Collections – A completely different Strategy
● Up to now we discussed associative collections using equivalence to manage keys.
– We used a BST and got logarithmic cost for inserting and finding elements. - This is pretty fast!
– An interesting point is that the organization of BSTs is concretely imaginable/understandable/drawable.
● Now we're going to discuss associative collections using equality to manage keys, which is really cool!
– Instead of keeping the associative collection sorted, we manage a set of collections that collect items having something in common.
– These individual collections are then called buckets.
● E.g. an associative collection telephoneDictionary: for each bucket the contained strings
will have the same first character.
– (This is exactly how "real" dictionaries work, where there are tabs collecting words starting with
the same character. The tabs play the role of the buckets.)
– This type of associative collection is called hashtable.
– A so called hash-function maps a key to a hash-code.
● In this case the hash-function maps a string (= the key) to its first letter (= the hash-code).
– The hash-function determines, in which bucket to find or insert a key (or key-value-pair).
● When the bucket is determined, a specific operation takes only place on the items in that bucket. telephoneDictionary
(Bucket)
"Bud""B"
"Ben"
(Bucket)
"Gil""G"
(Bucket)
"Will""W"
"Walt"
3821
9427
7764
4689
1797
public class Bucket {
public string HashCode { get; set; }
public IList<KeyValuePair<string, int>> Items { get; set; }
}
12. 12
12
Associative Collections – Hashtable-based Implementations - Keys
● OK, but how will an implementation using a hashtable help? - To understand it, let'sdissect hashtable starting with the keys.
● The idea of a hashtable is to have a hash-function that calculates a position in a table from the input item.
– How can we have a function that is able to do that for an item?
– The idea is to have a method of an item-object, more specifically the key, that calculates this position.
● In the frameworks Java and .Net each UDT inherits a base type (Object in both frameworks), forming a cosmic hierarchy.
– In Java a UDT can override Object's method hashCode() in order to calculate the hash-code.
– In .Net a UDT can override Object's method GetHashCode() in order to calculate the hash-code.
– Now we have hash-functions/methods to "hash" a name string to the int value of the first letter (incl. some error handling).
– We can use instances of NameKey as keys for hashtables!
// C#
public class NameKey {
public string Name { get; set; }
public override int GetHashCode() {
// Return int-value of first letter.
return string.IsNullOrEmpty(Name) ? 0 : Name[0];
}
}
// Java
public class NameKey { // (members hidden)
public String name; // (getter/setter hidden)
@Override
public int hashCode() {
// Return int-value of first letter.
return (null == getName() || getName().isEmpty()) ? 0 : getName().charAt(0);
}
}
● Java's and .Net's inherited equals()/Equals()
(inherited from Object esp. for .Net reference
types) will just compare references. It means that
equals()/Equals() will only return true, if the same
object is compared to itself, so the inherited
implementations do an identity comparison.
● Java's inherited hashCode() (inherited from
Object) just returns the object's address in
memory. .Net's inherited GetHashCode()
(inherited from Object i.e. for .Net reference types)
uses some number that is guaranteed to be
unique within the .Net AppDomain.
13. 13
13
Associative Collections – Hashtable-based Implementations -
.Net's Dictionary
● In .Net we can now use NameKey as type for the key in an IDictionary that is implemented in terms of a hashtable.
– The hashtable-based implementation of IDictionary is simply called Dictionary! It can (of course) be used like any other IDictionary:
– When GetHashCode() is overridden in the key's type, the hashtable will automatically work:
● When those items are added into the telephoneDictionary, following will happen:
– (1) For the key NameKey{ Name = "Bud" } the hash-code is retrieved. GetHashCode() returns 66.
– (2) In the hashtable-array (here an int[]) the entry on the index 66 is fetched:
● If there is an empty entry at 66, it will be set to the value 3821,
● otherwise the value at the existing entry at 66 will be overwritten with the value 3821.
– Another key (like NameKey{ Name = "Gil" }) yields another hash-code and another index
in the hashtable.
– The operations in (2) are O(1) operations, because accessing indexed collections
(like arrays) on indexes only cost O(1).
● Oversimplification! There's more and it's getting more contrived! And we've to understand it to get the whole picture!
IDictionary<NameKey, int> telephoneDictionary = new Dictionary<NameKey, int>();
telephoneDictionary[new NameKey{ Name = "Bud" }] = 3821; // GetHashCode() returns 66.
telephoneDictionary[new NameKey{ Name = "Gil" }] = 7764; // GetHashCode() returns 71.
telephoneDictionary
hashTable : int[]
382166
......
776471
......
......
38213821
adding these objects
(NameKey)
"Bud"
3821
(NameKey)
"Gil"
7764
● What we discuss here is a sparse array implementation
of a hashtable, in which many slots of the table remain
empty, whilst only few slots of the table with the indexes
matching the hash-code are filled. Real-world
implementations of hashtable are much more compact
(usually the index is calculated as "index = hash-code
% hashtable-length"), but we're not going to discuss
them in this course, because the mere idea is the same.
● We didn't discuss the implementation of
hashcode()/GetHashCode() in great depth, esp. if many
fields are to be taken into consideration in the hash-
code. Many suggestions involve the combination of the
hash-codes with xor operations in order to get a good
distribution including the multiplication of magic
constants to yield prime numbers, which may result in
yet a better distribution, or retrieving hash-codes of
different field-types in different ways. That's all fine, but
many of these tips rely on special assumptions of the
platform and the implementation. Using a simple xor
combination is OK for most cases and magic should
only be taken into consideration, if performance- (i.e.
lookup-) problems are present. Don't be too clever!
14. 14
14
Associative Collections – Hashtable-based Implementations
– Hash Collisions and Buckets
● But there is a problem: what if we try adding keys having the same hash-code, e.g. names with the same first letter?
– The thing that happens here is called hash collision. To handle hash collisions in a hashtable we have to review the idea of Buckets.
● Actually a hashtable is an array of Bucket objects (Not just an int[]!).
– For each hash-code there exists one Bucket.
– The hash-code is the index of the hashtable, where that
hash-code's Bucket resides.
– Within a Bucket all inserted items with a key having the
same hash-code are collected as key-value-pairs.
● Hm... wait! If a hash collision leads to putting items
into the same Bucket, how can the associated value
be retrieved from the hashtable in a definite way?
– In other words: the hashtable story is even more complex!
telephoneDictionary[new NameKey{ Name = "Bud" }] = 3821; // GetHashCode() returns 66.
telephoneDictionary[new NameKey{ Name = "Ben" }] = 9427; // GetHashCode() also returns 66!
telephoneDictionary
hashTable : Bucket[]
(Bucket)66
......
(Bucket)71
......
......
(Bucket)
(Key : NameKey)
"Bud"
(Key : NameKey)
"Ben"
66 hashCode
(Bucket)
(Key : NameKey)
"Gil"
71 hashCode
(Value : int)
3821
(Value : int)
9427
(Value : int)
7764
adding these objects
(NameKey)
"Bud"
3821
(NameKey)
"Ben"
9427
15. 15
15
Associative Collections – Hashtable-based Implementations
– Equality
● As a matter of fact hash collisions can not be avoided, because any objects could have the same hash-code!
– E.g. two names could have the same first letter. This fact is not under our control!
● The answer is that all the key-value-pairs of a hash-code-matching Bucket need to be searched for a certain key.
– The keys of a hash-code-matching Bucket are linearly searched and compared for equality to find the exactly matching key.
– But what means "exactly matching key" in opposite to a (just) hash-code-matching key?
– We have to prepare our key-type to provide another method to check for the exact equality of two keys!
– Similar to objects providing hash-codes, Java and .Net want us overriding the method equals()/Equals() inherited from Object:
● Let's discuss how this works!
public class NameKey { // (members hidden)
public override int GetHashCode() { /* pass */ }
public override bool Equals(object other) {
// Simple and naïve implementation of Equals():
NameKey otherNameKey = (NameKey)other;
return Name == otherNameKey.Name;
}
}
public class NameKey { // (members hidden)
@Override
public int hashCode() { /* pass */ }
@Override
public boolean equals(Object other) {
// Simple and naïve implementation of equals():
NameKey otherNameKey = (NameKey) other;
return getName().equals(otherNameKey.getName());
}
}
16. 16
16
Associative Collections – Hashtable Implementations –
Implementing Equality
● .Net: Mechanics of comparing two NameKey objects with the method Equals():
– Equals() has a parameter that is of the very base type Object (static type).
– We assume the dynamic type behind the passed argument is NameKey (only NameKeys can be equality-compared to each other).
● We can cast the parameter other to NameKey blindly. (And this can be a problem, which we'll discuss shortly.)
– When we've the NameKey behind the parameter, we equality-compare the argument's Name property against this' Name property.
● Mind that we used the operator== to compare the Name properties (well, we could have used Equals() as well); those are of type string.
● Equals() makes a deeper (more expensive) comparison than GetHashCode(), the latter only deals with Name's first letter:
public class NameKey { // (members hidden)
public override bool Equals(object other) {
NameKey otherNameKey = (NameKey)other;
return this.Name == otherNameKey.Name;
}
}
// Both hash-codes evaluate to 66, but these objects are not equal!
NameKey key1 = new NameKey{ Name = "Bud" };
NameKey key2 = new NameKey{ Name = "Ben" };
bool hashCodesAreTheSame = key1.GetHashCode() == key2.GetHashCode();
// evaluates to true
bool areNotEqual = key1.Equals(key2);
// evaluates to false
public class NameKey { // (members hidden)
public override bool Equals(object other) {
NameKey otherNameKey = (NameKey)other;
return this.Name == otherNameKey.Name;
}
}
public class NameKey { // (members hidden)
public override int GetHashCode() {
return string.IsNullOrEmpty(this.Name) ? 0 : this.Name[0];
}
}
17. 17
17
Associative Collections – Hashtable-based Implementations –
Rules of Equality
● We'll not discuss all facets of the implementation of GetHashCode() and Equals(), but here are the most important rules:
– Two objects having the same hash-code need not to return true for calling Equals()!
– But, if two objects having the same dynamic type return true for calling Equals(), then those need to have the same hash-code!
– GetHashCode() and Equals() have to return the same results for the "structurally" same objects unless one of them is modified.
– GetHashCode() and Equals() should be implemented to work very fast.
– Neither GetHashCode() nor Equals() are allowed to throw exceptions!
● If null is passed to Equals() the result has to be false.
● Finally GetHashCode() and Equals() for NameKey could be implemented like so to fulfill these rules:
public class NameKey { // (members hidden)
public override int GetHashCode() {
return string.IsNullOrEmpty(Name) ? 0 : Name[0];
}
public override bool Equals(object other) { // Stable implementation of Equals()
if (this == other) {
return true;
}
if (null != other && GetType() == other.GetType())) {
return Name == ((NameKey)other).Name;
}
return false;
}
}
- checks for identity
- checks for nullity (to avoid exceptions)
- checks the dynamic type of this and the other object
- the cast is type safe
- the Name properties of both objects are equality-compared
18. 18
18
Associative Collections – Hashtable-based Implementations
– The Lookup Algorithm
● All right! Now we have the methods GetHashCode() and Equals() in place. But how do hashtables use these tools to work?
● Let's assume following content in the hashtable-based Dictionary telephoneDictionary:
– Then we'll lookup/search the phone number of "Bud":
● The lookup will initiate following algorithm basically:
– Dictionary will call GetHashCode() on the indexer's (i.e. operator[]) argument 'new NameKey{ Name = "Bud" }' and the result is 66.
– Dictionary will get the Bucket at the hashtable's index 66. This Bucket has two entries!
– Dictionary will call 'Equals(new NameKey{ Name = "Bud" })' against each key of the key-value-pairs in the just returned Bucket.
– The value of the key-value-pair for which the key was equal to 'new NameKey{ Name = "Bud" })' will be returned.
● Read these steps for multiple times and make sure you understood those!Now we have to discuss some details...
– The algorithms to insert or update a key-value-pair work the same way as for lookups!
telephoneDictionary
hashTable : Bucket[]
(Bucket)66
......
......
(Bucket)
(Key : NameKey)
"Bud"
(Key : NameKey)
"Ben"
66 hashCode
(Value : int)
3821
(Value : int)
9427
searching this key
(NameKey)
"Bud"
IDictionary<NameKey, int> telephoneDictionary = new Dictionary<NameKey, int> {
{ new NameKey { Name = "Bud" }, 3821 },
{ new NameKey { Name = "Ben" }, 9427 }
} ;
int no = telephoneDictionary[new NameKey { Name = "Bud" }];
19. 19
19
Associative Collections – Hashtable-based Implementation – best
Case Complexity
● Although the algorithm to lookup keys is complex, the analysis of a hashtable's complexity is reallysimple!
● The best case yields a complexity of O(1)! - Wow!
– If every item can be associated to exactly one distinct Bucket each, we have
one item in every Bucket: an 1 : 1 association between items and Buckets.
– This means that every item has a distinct hash-code and thus a distinct
index in the hashtable.
– As the hashtable is an array and arrays can access their items by index
with O(1) complexity, we have the best case for hashtable!
● In the best case, accessing a hashtable, means accessing an array by
index with constant complexity.
– This is better than O(log n) for tree-based associative collections!
– This is the best we can get for collections!
●
But there is also a worst case!
telephoneDictionary
hashTable : Bucket[]
(Bucket)66
......
(Bucket)71
......
......
(Bucket)
(Key : NameKey)
"Gil"
71 hashCode
(Value : int)
7764
(Bucket)
(Key : NameKey)
"Bud"
66 hashCode
(Value : int)
3821
(Bucket)
(Key : NameKey)
"Will"
87 hashCode
(Value : int)
4689
(Bucket)87
......
20. 20
20
Associative Collections – Hashtable-based Implementations –
worst Case Complexity
● The worst case yields a complexity of O(n)! - Oh no!
– If every item can be associated to the same Bucket, we have
all items in only one single Bucket: an n : 1 association between items and
Buckets.
– This means that every item has the same hash-code and thus the same
index in the hashtable.
– As the only one Bucket needs to be search linearily to find the equal key,
the worst complexity boils down to the linear complexity O(n)!
● In the worst case the workload is moved to a single Bucket that must
be searched in a linear manner.
telephoneDictionary
hashTable : Bucket[]
(Bucket)66
......
......
(Bucket)
(Key : NameKey)
"Bud"
(Key : NameKey)
"Ben"
66 hashCode
(Value : int)
3821
(Value : int)
9427
(Key : NameKey)
"Betty"
(Value : int)
8585
● Java's java.util.concurrent.ConcurrentHashMap is
able to switch the storage of the buckets from a List-
implementation to a sorted tree implementation, if
the buckets grow too large (i.e. too many keys with
the same hashcode). The benefit is that searching a
bucket will be more efficient then (O(n) for List, but
O(log n) for sorted trees).
21. 21
21
Associative Collections – Hashtable-based Implementations –
Controlling Performance – Part I
● An interesting point when working with .Net's Dictionary is how we can influence the performance as developers:
– We can override GetHashCode() and Equals() for our own UDTs being used as keys and this is what we are going to discuss now.
● Concretely we have a problem with the UDT NameKey: we'll get the same hash-code for keys having the same first letter!
– However, we can override GetHashCode(), so that a "deeper" or in other words "more distinct" hash-code will be produced.
– .Net's string is able to produce its own hash-code that is calculated concerning the whole string-value and not only the first letter:
● We already know that each .Net type inherits Object and can override GetHashCode() and Equals().
– A type implementing equality in the .Net framework needs overriding GetHashCode() and Equals()!
●
Think: GetHashCode() => level one equality, Equals() => level two equality.
– Many types of the .Net framework provide useful overrides of GetHashCode() and Equals() to implement equality.
– (GetHashCode() is not only used with Dictionary's but also in other places of the .Net framework.)
– (A type implementing equivalence in the .Net framework needs implementing IComparable or another type implementing IComparer.)
public class NameKey {
public string Name {get; set;}
public override int GetHashCode() {
// Return int-value of first letter.
return string.IsNullOrEmpty(Name) ? 0 : Name[0];
}
}
public class NameKey {
public string Name {get; set;}
public override int GetHashCode() {
// Return the hash-code of the string Name.
return string.IsNullOrEmpty(Name) ? 0 : Name.GetHashCode();
}
}
● switch-case with strings in C# and Java uses the
strings' hash-code to do the comparisons.
● It should be mentioned that some methods of list-
types also use equality to function, e.g. methods
like Contains(), Remove() or IndexOf().
22. 22
22
NO! Stop it! We're not going to discuss it here! As a matter of fact implementing equality correctly in .Net and Java is
not simple and it is often done downright wrong and potentially dangerous!
I have one simple tip: Don't be too clever and follow the rules!
Ok, we'll discuss it in depth in a future lecture.
***Under Construction – Equality – Under Construction***
● An interesting point is that each object in Java/.Net implements equals()/Equals() and hashcode()/GetHashCode().
– But overrides of those methods need to follow certain rules. Let's discuss those.
23. 23
23
Associative Collections – Hashtable-based Implementations –
Controlling Performance – Part II
● The last implementation of NameKey does basically delegate equality fully to its property Name, which is of type string.
– Hm! - To drive this point home: we can get rid of the type NameKey and use string instead as key-type! Hey!
● As a matter of fact, it is often not required to program extra UDTs as key types!
– The present .Net types are often sufficient to be used as keys for hashtables. Most often int and string are used as key types.
– Nevertheless, .Net (and Java) developers have to know, how equality needs to be implemented correctly!
● In principle the implementation pattern and even the idiom for equality works the same way in Java!
// A dictionary that associates strings (names) to ints (phone numbers):
IDictionary<string, int> telephoneDictionary = new Dictionary<string, int>();
// Adding two string-int key-value-pairs:
telephoneDictionary["Bud"] = 3821;
telephoneDictionary["Gil"] = 7764;
// Looking up the phone number of "Bud":
int no = telephoneDictionary["Bud"];
24. 24
24
●
Without any further explanation it should be clear that maintaining a collection having only unique items is very painful.
– E.g. collecting only the surnames from a deck of invitations. It could be done by filling a list w/o duplicates:
●
There is a variant of associative collections that just avoids the presence of duplicates. Those are often called sets.
– Sets are associative collections like dictionaries, in which the keys are the values!
– In Java/.Net we can use BST-based (TreeSet/SortedSet) and hashtable-based (HashSet/HashSet) implementations of sets.
– In C++ we can use set either the BST-based std::set (<set>) or the hashtable-based std::unordered_set (C++11: <unordered_set>).
// Java
List<String> allSurnames = Arrays.asList("Taylor", "Miller", "Taylor", "Miller");
List<String> uniqueSurnames = new ArrayList<>();
for (String surName : allSurnames) {
if (!uniqueSurnames.contains(surName)) { // Filters unique items out.
uniqueSurnames.add(surName);
}
}
for (String surName : uniqueSurnames) {
System.out.println(surName);
}
// > Taylor
// > Miller
// Implementation using a Set:
Set<String> uniqueSurnames2 = new HashSet<>();
for (String surName : allSurnames) {
uniqueSurnames2.add(surName);
}
for (String surName : uniqueSurnames2) {
System.out.println(surName);
}
Associative Collections – Sets
● As for dictionaries (and all associative collections)
the cause using sets is to exploit its self-
organization (keeping items unique), instead of
organizing it ourselves.
25. 25
25
●
Set collections can really represent mathematical sets, this also includes operations on sets (e.g. via .Net's ISet interface):
●
Subsets
●
Union
// C#/.Net
ISet<int> A = new HashSet<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ISet<int> B = new HashSet<int>{ 3, 6, 8 };
bool BSubSetOfA = B.IsSubsetOf(A);
// >true
bool BProperSubSetOfA = B.IsProperSubsetOf(A);
// >true
Associative Collections – Operations on Sets – Part I
// Java
Set<Integer> A = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
Set<Integer> B = new HashSet<>(Arrays.asList(3, 6, 8));
boolean BSubSetOfA = A.containsAll(B);
// >true
// Java doesn't provide a test for _proper_ subsets.
A⊆B A⊂B A={1,2,3,4,5,6,7,8,9};B={3,6,8}
A B
A∪B A={1,2,3,4,5,6};B={4,5,6,7,8,9};{1,2,3,4,5,6}∪{4,5,6,7,8,9}={1,2,3,4,5,6,7,8,9}
ISet<int> A = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
A.UnionWith(B); // The set A will be _modified_!
// >{1, 2, 3, 4, 5, 6, 7, 8, 9}
// LINQ's Union() extension method will create a _new_
// sequence:
ISet<int> A2 = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B2 = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
IEnumerable<int> A2UnionB2 = A2.Union(B2);
// >{1, 2, 3, 4, 5, 6, 7, 8, 9}
Set<Integer> A = new TreeSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
Set<Integer> B = new TreeSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));
boolean AWasModified = A.addAll(B); // The set A will be _modified_!
// >true
System.out.println(A);
// >[1, 2, 3, 4, 5, 6, 7, 8, 9]
A B
● A proper subset means, that a set is a subset of
another set, but both sets are not equal!
● Subsets can also be expressed with LINQ, but
usually the type ISet and its implementors should
be used, because it leads to more expressive
code:
// C#/.Net/LINQ
ISet<int> A = new HashSet<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ISet<int> B = new HashSet<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ISet<int> C = new HashSet<int>{ 3, 6, 8 };
// Subset:
bool BIsSubsetOfA = !B.Except(A).Any();
// >true
bool CIsSubsetOfA = !C.Except(A).Any();
// >true
// Proper Subset:
bool BIsProperSubSetOfA = A.Except(B).Any();
// > false
bool CIsProperSubSetOfA = A.Except(C).Any();
// > true
26. 26
26
●
Difference
●
Symmetric difference
// C#/.Net
ISet<int> A = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
A.ExceptWith(B); // The set A will be _modified_!
// >{1, 2, 3}
// LINQ's Except() method will create a _new_ sequence:
ISet<int> A2 = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B2 = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
IEnumerable<int> A2ExceptB2 = A2.Except(B2);
// >{1, 2, 3}
Associative Collections – Operations on Sets – Part II
A B
A ∖B
// Java
Set<Integer> A = new TreeSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
Set<Integer> B = new TreeSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));
A.removeAll(B); // The set A will be _modified_!
System.out.println(A);
// >[1, 2, 3]
A={1,2,3,4,5,6};B={4,5,6,7,8,9};{1,2,3,4,5,6}∖{4,5,6,7,8,9}={1,2,3}
A▵B(:=( A∖ B)∪(B∖ A)) A={1,2,3,4,5,6};B={4,5,6,7,8,9};{1,2,3,4,5,6}▵{4,5,6,7,8,9}={1,2,3,7,8,9}
A B
ISet<int> A = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
A.SymmetricExceptWith(B); // The set A will be _modified_!
// >{1, 2, 3, 7, 8, 9}
// Using LINQ we can create a _new_ sequence:
ISet<int> A2 = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B2 = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
IEnumerable<int> A2SymmetricExceptB2 =
A2.Except(B2).Union(B2.Except(A2));
// >{1, 2, 3, 7, 8, 9}
Set<Integer> A = new TreeSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
Set<Integer> B = new TreeSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));
Set<Integer> A2 = new TreeSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
A.removeAll(B); // The sets A and B will be _modified_!
B.removeAll(A2);
A.addAll(B);
System.out.println(A);
// >[1, 2, 3, 7, 8, 9]
27. 27
27
●
Intersection
Associative Collections – Operations on Sets – Part III
A∩B A={1,2,3,4,5,6};B={4,5,6,7,8,9};{1,2,3,4,5,6}∩{4,5,6,7,8,9}={4,5,6}
A B
// C#/.Net
ISet<int> A = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
A.IntersectWith(B); // The set A will be _modified_!
// >{4, 5, 6}
// LINQ's Intersect() method will create a _new_ sequence:
ISet<int> A2 = new SortedSet<int>{ 1, 2, 3, 4, 5, 6 };
ISet<int> B2 = new SortedSet<int>{ 4, 5, 6, 7, 8, 9 };
IEnumerable<int> A2IntersectionWithB2 = A2.Intersect(B2);
// >{4, 5, 6}
// Java
Set<Integer> A = new TreeSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));
Set<Integer> B = new TreeSet<>(Arrays.asList(4, 5, 6, 7, 8, 9));
A.retainAll(B); // The set A will be _modified_!
System.out.println(A);
// >[4, 5, 6]
28. 28
28
●
Inserting values having already present keys in an associative collectionoverwrites or updates present values.
– Sometimes it is not desired. E.g. mind a telephoneDictionary, in which a name can have more than one phone number!
●
Some collection frameworks provide associative collections that can handlemultiplicity.
– In C++ we can use std::multimap (<map>) and std::multiset (<set>).
●
In other frameworks (Java, .Net etc.) multi-associative collections need to be explicitly implemented or taken from 3rd
party.
– 3rd
party sources: Apache Commons (Java)
// C++11
std::multimap<std::string, int> telephoneDictionary {
{ "Ben", 9427 }, // Mind: two values with the same key are added here.
{ "Ben", 4367 },
{ "Jody", 1781 }, // Mind: three values with the same key are added here.
{ "Jody", 9032 },
{ "Jody", 8038 }
};
// It is required to use STL iterators, because std::multimap provides no subscript operator:
for (auto item = telephoneDictionary.begin(); item != telephoneDictionary.end(); ++item) {
std::cout<<"Name: "<<item->first<<", Phone number: "<<item->second<<std::endl;
}
Special associative Collections – Multiplicity of Keys
telephoneDictionary
9427"Ben" 4367
1781"Jody" 9032 8038
29. 29
29
●
Key
– Refrain from modifying key objects managed in associative collections.
●
Sorted/equivalence-based associative collections:
– TreeMap/TreeSet (Java), SortedDictionary/SortedSet (.Net), std::set/std::map (C++),
– The internal organization of keys is equivalence-based:
●
Equivalence-based means that key objects are organized due to their relative/natural order. This is often implemented with a BST.
●
The key-type needs implementing Comparable (Java) or IComparable (.Net) or operator< (C++ STL) for semantically correct "natural order".
●
Or a Comparator (Java) or IComparer (.Net) or a comparison functor (C++ STL) needs to be specified for the associative collection that implements
"natural ordering" for the key-type.
– Searching/inserting/removing items can be done very fast (O(logn)), because binary searches are used (BST).
– Sorted associative collections are unordered!
●
The iteration will yield the items in sorted order (BST in-order). The order, which items have been added/inserted/removed doesn't matter!
– => Use these associative collections, if sorting of keys or the control of key-comparison (e.g. reverse order) is needed!
Things to ponder about Associative Collections – Part I
30. 30
30
●
Equality-based associative collections:
– HashMap/HashSet (Java), Dictionary/HashSet (.Net), NSDictionary/NSSet (Cocoa), std::unordered_map/std::unordered_set (C++11),
associative arrays (JavaScript)
– The internal organization of keys is equality-based:
●
By equality. I.e. by hash-codes and the result of the equality check (methods hashCode()equals() (Java) or GetHashCode()/Equals() (.Net)).
●
The key-type needs implementing hashCode()/equals() (Java) or GetHashCode()/Equals() (.Net) for semantically correct equality.
●
Or an IHashCodeProvider/IEqualityComparer (.Net) needs to be specified for the associative collection that implements equality for the key-type.
●
In C++11 STL a hasher functor should be specified for the associative collection that provides hash-codes for the key-type.
– Searching/inserting/removing items can be done O(1) no extra search operation is required, the hash-code is used as an index.
– These associative collections are unordered!
●
The iteration order makes no guarantees about how items are yielded. It can change completely when items are added.
●
In most cases equality-based associative collection should be our first choice, those are potentially most efficient.
– => Use these associative collections, if sorting of keys or the control of key-comparison doesn't matter!
●
Ordered associative collections:
– LinkedHashMap (Java) and OrderedDictionary (.Net) will iterate in that order, in which the items were put into the collection.
●
LinkedHashMap is the type Groovy uses to implement map literals.
Things to ponder about Associative Collections – Part II
31. 31
31
●
Old and new collections:
– Java:
●
The old Hashtable is not null-aware on keys, adding null will throw a NullPointerException. Better use HashMap/HashSet (Java 1.2 or newer).
●
Hashtable and HashMap return null, if a requested key has no value (i.e. key is not present).
– .Net:
●
The old object-based Hashtable should not be used for new code using .Net 1 or newer. Better use Dictionary/SortedDictionary (.Net 2 or newer).
●
Be aware that Hashtable returns null, if a key has no value (i.e. key is not present). Dictionary/SortedDictionary will throw a KeyNotFoundException.
●
Keys with the value null:
– Java:
●
TreeMap/TreeSet don't allow keys having the value null, then a NullPointerException will be thrown.
●
HashMap/HashSet can digest keys having the value null.
– .Net:
●
Dictionary/SortedDictionary don't allow keys having the value null, then an ArgumentNullException will be thrown.
●
HashSet/SortedSet can digest keys having the value null.
●
It was never mentioned explicitly: There are basically no constraints on the types of the values in associative collections!
Things to ponder about Associative Collections – Part III