Download presentation
Presentation is loading. Please wait.
1
ENEE150 Discussion 13 Section 0101 Adam Wang
2
Overview Project Quiz
3
Project 4: Binary Search Tree
Due December 9th (next Saturday) You will write a bunch of functions that implement the functionality of a BST Download parse.c and tree-search.c online Modify them to use your new BST
4
Implementation
5
Keys/Data The key is what you use to find where to store the data in the tree The data is the actual information we want to store Think of the key as the address and the data as the house Hard to compare houses, but we can easily sort by addresses These fields are void * because ANY type of data can be stored in them
6
The main BST_T structure
Holds a function pointer to a “compare” function The idea is we use this to tell which keys come first and which keys come last That way we can store any kind of data and still take advantage of the searching/sorting of the BST
7
Pbst_t new_bst(int (*cmp_func)(void *, void *))
Return a new dynamically allocated BST structure The parameter passed in is the pointer to the compare function *This function will be written in tree-search.c / parse.c* Then you pass it into THIS function to create the BST Just malloc a Pbst_t and initialize all its fields and return it
8
void free_bst(Pbst_t bst)
Frees all the memory in the BST You definitely want to write a helper function that recursively frees the actual tree Post order traversal: recursively free the left subtree, then the right subtree, then finally the parent node Don’t forget to free the key and data in addition to the actual node Don’t forget to free the entire Pbst_t structure and the cur_walk array
9
void insert_bst(Pbst_t bst, void *key, void *data)
Malloc a new node, and insert its key and data Set the left and right fields to null! You can do this iteratively or recursively (harder) Yeung has code online on how to do this iteratively If you do it recursively, you’d need to make another helper function Need to use that compare function to check keys: int compare = (*(bst->cmp_func))(new->key, cur->key); If the key already exists, don’t do anything (but free the new node!) Update the height / num_items too
10
void *find_bst(Pbst_t bst, void *key, int *depth)
Returns the data associated with the key passed in Similar code to insert Also need to increment the depth parameter passed in as you search This will keep track of the number of “tries” it takes to find the key If not found return NULL
11
void stat_bst(Pbst_t bst, int *num_items, int *height)
Set the num_items and height parameters passed in to the ones stored in the bst 2 lines of code Notice we pass in pointers because we want the changes to be permanent
12
void *start_bst_walk(Pbst_t bst)
This function and the next are probably the most difficult parts of the project “Iterate” over the nodes in ascending order, starting with the smallest When you call next_bst_walk, it should return the next node in line We will accomplish this using an array: bst-> cur_walk will be a pointer array of nodes in the correct order Just traverse through the array to print the correct one Bst->cur_walk_index keeps track of which node to spit out next
13
start_bst_walk Allocate memory for the array of Pnode_t’s using the num_items variable If an array already exists, free it first before malloc’ing a new one Also initialize the current index where to start the walk We will use a helper function to build the array
14
Helper function: set_nodes (Pnode_t *arr, Pnode_t node, int *index)
Takes in the newly allocated array, the root of the tree, and an integer pointer representing the index We will do an in order traversal to traverse through the nodes in order Recursively set nodes on the left Set nodes on the current node Recursively set nodes on the right Just assign the node to the proper index spot of the array and increment the index parameter We use a integer pointer because we want this index to be the same for all the function calls
15
void *next_bst_walk(Pbst_t bst)
Returns the next piece of data in the array Return null if already reached the end of the array Just increment the index pointer found in the bst and return the proper node in the array
16
tree-search.c Adds 10000 random values to a tree
Then allows user to search for a number in the tree and it returns whether it’s there We need to replace this tree with our BST Add “include bst.h” to the top Delete their find and insert functions
17
Comparison function SAME function header as the function pointer declaration of the Pbst_t, i.e. int cmp (void *x, void *y) For tree-search.c, this function compares integers Cast x and y to int * (we can cast a void * to anything) Return negative number if x < y, 0 if x = y, positive number if x > y Make sure this works for negative numbers too
18
create_data Instead of making a new cur node, dynamically allocate a key and data pointer In this case, key = data because we’re just storing numbers Use the rand() % NUM_VALUES to calculate the random # to store Call insert_bst to store this into the tree Outside for loop, call stat_bst to get the num_items and height values Print these out
19
In main Use the new_bst function to create a new Pbst_t variable
Call create_data In the while loop, instead of calling search we’ll call find_bst Pass in the bst, the number to find, and a variable to hold the depth If found, print the data + number of tries (depth) it took to find Else print data not found
20
parse.c Reads a file and prints the frequency of each word in the file
Originally this is done with a 2D array Now we’ll store the words into our BST along with the frequency Then print out all the data using our bst_walk methods Delete their find_word and insert_word functions Our key is the word name, and our data will be a new structure that holds the word and the frequency:
21
int word_cmp(void *x, void *y)
We need to make a comparison function that compares two strings This function just ends up calling strcmp Similar format as other compare function
22
void dump_dictionary (Pbst_t tree)
Call stat_bst to get the num_items and height, then print these two out: ex Number of unique words: 140 Height: 7 Call start_bst_walk to get the first word Use a while loop to print out all the words and their corresponding frequencies Output won’t exactly match the old output of parse.c
23
In Main Use new_bst to initialize the tree dictionary
Where it calls find_word, instead we’re going to use find_bst If the returned word is null, malloc a new Pword_record and store the word and an initialize frequency of 1 Also allocate memory for the key, which also gets stored the word strcpy is your friend here Then insert into the tree with insert_bst If not null, increment the frequency of the Pword_record returned Call dump_dictionary and free_bst after the while loop
24
Last quiz Implement a Queue
A queue is a list where data gets added to the BACK of the list, and removed from the FRONT of the list. wget ece.umd.edu/~aw97/quizzes/quiz4.c Submit under assignment 104: submit 2017 fall enee quiz4.c
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.