﻿1
00:00:00,390 --> 00:00:03,810
‫So now we're going to build our hash table constructor.

2
00:00:04,230 --> 00:00:11,070
‫So I'm going to bring up an address space here, except I'm going to make a change to the address space.

3
00:00:11,070 --> 00:00:17,460
‫Instead of having eight items, I'm going to remove the item on the bottom and we're only going to have

4
00:00:17,460 --> 00:00:18,300
‫seven.

5
00:00:18,720 --> 00:00:24,240
‫I want to do this to emphasize that your address space, whatever size that you make it, you should

6
00:00:24,240 --> 00:00:26,280
‫make it a prime number.

7
00:00:26,520 --> 00:00:33,630
‫And the reason for that is if you have a prime number of addresses, your key value pairs will be distributed

8
00:00:33,630 --> 00:00:34,860
‫more randomly.

9
00:00:35,010 --> 00:00:37,740
‫In other words, you'll have fewer collisions.

10
00:00:38,010 --> 00:00:45,300
‫So we'll start our hash table class off like this, and we're going to have a couple of member variables.

11
00:00:45,510 --> 00:00:53,100
‫And I like to explicitly make these private, even though they will be private by default and our first

12
00:00:53,100 --> 00:00:59,370
‫variable will call size, we'll set that to be equal to seven and that's going to be the size of the

13
00:00:59,370 --> 00:01:05,400
‫array that we're using for our address space because we are assigning the value here.

14
00:01:05,490 --> 00:01:07,940
‫We're going to make it a constant.

15
00:01:07,950 --> 00:01:14,280
‫And because it is a constant, a common naming convention is to make the variable name.

16
00:01:14,280 --> 00:01:21,050
‫All caps and static means that we're only going to store one of these numbers sevens in memory.

17
00:01:21,060 --> 00:01:27,630
‫So if we create two hash tables, let's say we call them my hash table one and my hash table two.

18
00:01:27,960 --> 00:01:35,550
‫Both of those instances of hash table will use this one, number seven, and now we'll create that address

19
00:01:35,550 --> 00:01:36,450
‫space.

20
00:01:36,480 --> 00:01:38,520
‫I'm going to call it data map.

21
00:01:38,520 --> 00:01:41,850
‫And the size is going to be this size here.

22
00:01:41,940 --> 00:01:47,280
‫And the data map is going to hold pointers to nodes like this.

23
00:01:47,670 --> 00:01:53,970
‫So I called this video constructor, but technically, this hash table class does not have a constructor.

24
00:01:54,180 --> 00:02:00,450
‫All we're doing is using these two lines to create this data map, address space.

25
00:02:00,810 --> 00:02:06,420
‫Remember, technically, a constructor would be a separate function with the name of the class.

26
00:02:06,750 --> 00:02:13,800
‫But we don't need that here because this is all we're going to do when we create a new hash table.

27
00:02:14,130 --> 00:02:18,180
‫So the other thing we need to be able to create is nodes.

28
00:02:18,450 --> 00:02:24,090
‫So these nodes are going to be a little bit different than what we saw in the linked list section.

29
00:02:24,270 --> 00:02:27,930
‫These nodes are going to have key and value.

30
00:02:27,930 --> 00:02:34,560
‫And next in the linked list section, the nodes just had value index, but they didn't have key.

31
00:02:34,770 --> 00:02:41,670
‫So in our node class, there are going to be three member variables, key value and next.

32
00:02:41,940 --> 00:02:46,920
‫And in the constructor we will pass it a key and a value.

33
00:02:47,100 --> 00:02:51,720
‫This key and value are going to be these variables here.

34
00:02:51,930 --> 00:02:55,860
‫And we'll set this key and this value equal to those.

35
00:02:55,860 --> 00:03:01,890
‫And this key and this value will be the member variables for the class.

36
00:03:02,190 --> 00:03:06,810
‫And then the last thing is we'll set next to be equal to null pointer.

37
00:03:07,200 --> 00:03:12,630
‫So we've written the code in the hash table class to create the address space and then with the node

38
00:03:12,630 --> 00:03:16,110
‫class, the ability to create nodes.

39
00:03:16,380 --> 00:03:20,070
‫So now let's flip over to VS code and take a look at this.

40
00:03:20,850 --> 00:03:28,410
‫So there is our node class there and I'm going to scroll up and this is the code that we just wrote

41
00:03:28,410 --> 00:03:30,030
‫for the hash table class.

42
00:03:30,150 --> 00:03:35,700
‫And just like I have done previously, I've created a function to be able to print out whatever the

43
00:03:35,700 --> 00:03:37,650
‫data structure is that we're building.

44
00:03:37,680 --> 00:03:40,080
‫I'm going to call this one print table.

45
00:03:40,530 --> 00:03:46,920
‫I won't walk through the detail of this code, but you can access this code and the resources tab for

46
00:03:46,920 --> 00:03:50,100
‫this video and then I'll scroll up again.

47
00:03:50,580 --> 00:03:55,500
‫So with this line here, we create a new hash table, and then with this line here, we'll print that

48
00:03:55,500 --> 00:04:01,500
‫table out and I'll run this and you can see our addresses here zero through six.

49
00:04:01,500 --> 00:04:05,730
‫We haven't added any key value pairs yet, so these are all empty.

50
00:04:06,450 --> 00:04:11,580
‫But it does look like we have a working hash table constructor.

