symbol table is a data structure for storing key-value pairs, which supports two kinds of operation insertion and lookup, which is to put a new set of key-value pairs into the table and then get corresponding values according to the given key, which is similar in common dictionary principle in programming language. Symbol table is a typical abstract data structure, in the life of the application of a lot of scenes, can be opened according to the key, the IP address of the domain name resolution query, dictionary and book introduction and page, key and value is inseparable, through key we can quickly find the value we need.
Order Lookup for unordered lists
Mainly through node nodes to store data, the previous blog has about the implementation of the list, the details can refer to the previous blog, the code has comments to explain too much, the code is as follows:
@interface basenode:nsobject@property (strong,nonatomic) nsstring *key; @property (Strong, nonatomic) nsstring *value; @property (strong,nonatomic) basenode *next;-(void) SetupData: ( NSString *) Key value: (NSString *) value Next: (Basenode *) next; @end
Implementation of BASENODE.M:
@implementation basenode-(void) SetupData: (NSString *) key value: (NSString *) value Next: (Basenode *) next{ Self.key=key; Self.value=value; Self.next=next;} @end
Order the code in the lookup class:
@implementation sequencetable-(void) put: (NSString *) key value: (NSString *) value{ //Traverse node for (Basenode * X=self.first; X!=null; X=x.next) { if ([X.key Isequaltostring:key]) { x.value=value;return; } } The new node is inserted at the front and the structure is similar to the stack basenode *tempnode=self.first; Self.first=[[basenode Alloc]init]; [Self.first Setupdata:key value:value Next:tempnode];} -(NSString *) Get: (NSString *) key{ //traversal array There is a return value, otherwise return NULL for (Basenode *node=self.first; node!=null; Node=node.next) { if ([key IsEqualToString:node.key]) { return node.value; } } return NULL;} @end
Specific invocation:
Sequencetable *table=[[sequencetable Alloc]init]; [Table put:@ "Flyelephant" value:@ "Http://www.cnblogs.com/xiaofeixiang"]; [Table put:@ "Technology Exchange Group" value:@ "228407086"]; NSLog (@ "Technology Exchange Group:%@", [Table get:@ "Technology Exchange Group"]);
When inserting the first to look for, look for when you need to find from the list header, so the average insertion and lookup time complexity is O (n), in order to be efficient we can turn the symbol table into an orderly binary search.
binary search for ordered arrays
To make the array orderly we do a binary lookup to greatly reduce the number of times the array is compared, the time complexity of the lookup becomes O (LgN), the time complexity of the insertion becomes O (n), and the code is as follows:
@interface Binarysearchtable:nsobject@property (strong,nonatomic) nsmutablearray *keys; @property ( strong,nonatomic) Nsmutablearray *values; @property (assign,nonatomic) nsinteger count;-(void) Put: (NSString *) key value: (NSString *) value;-(NSString *) Get: (NSString *) key;-(Nsinteger) rank: (NSString *) key;@ End
The implementation code is as follows:
@implementation binarysearchtable-(Nsmutablearray *) keys{if (!_keys) {_keys=[[nsmutablearray Alloc]initwithcap ACITY:1]; } return _keys;} -(Nsmutablearray *) values{if (!_values) {_values=[[nsmutablearray alloc]initwithcapacity:1]; } return _values;} Add newly entered key-value pair-(void) put: (NSString *) key value: (NSString *) value{Nsinteger index=[self Rank:key]; If there is a key-value pair, update the value if (Index<self.count&&[self.keys[index] isequaltostring:key]) {Self.values[index]=valu E;return; } for (Nsinteger i=self.count; i>index; i--) {self.keys[i]=self.keys[i-1]; SELF.VALUES[I]=SELF.VALUES[I-1]; } Self.keys[index]=key; Self.values[index]=value; self.count=self.count+1;} -(NSString *) Get: (NSString *) key{//Lookup key, return value if present, otherwise return null Nsinteger index=[self Rank:key]; if (Index<self.count&&[self.keys[index] isequaltostring:key]) {return self.values[index]; } return NULL; -(Nsinteger) rank: (NSString *) key{//If there is a key to return the position of the key, it can be understood as the number of the table less than its key nsinteger low=0,high=self.count-1; while (Low<=high) {Nsinteger mid=low+ (high-low)/2; Nsinteger Diff=[key Integervalue]-[self.keys[mid] integervalue]; if (diff>0) {low=mid+1; } else if (diff<0) {high=mid-1; }else{return mid; }} return low;} @end
Test code:
Sequencetable *table=[[sequencetable Alloc]init]; [Table put:@ "Flyelephant" value:@ "Http://www.cnblogs.com/xiaofeixiang"]; [Table put:@ "Technology Exchange Group" value:@ "228407086"]; NSLog (@ "Technology Exchange Group:%@", [Table get:@ "Technology Exchange Group"]); Binarysearchtable *binarytable=[[binarysearchtable Alloc]init]; [Binarytable put:@ "3" value:@ "Flyelephant"]; [Binarytable put:@ "9" value:@ "Keso"]; [Binarytable put:@ "value:@" blog Park "]; [Binarytable put:@ "0" value:@ "228407086"]; NSString *temp=[binarytable get:@ "3"]; NSLog (@ "blog Park:%@", temp);
Effect:
Algorithm-Symbol table implementation (sequential lookup and binary lookup)