From 7edfb9455c04208aba63877c3e1a043e9f48f147 Mon Sep 17 00:00:00 2001 From: NehaKeshan <39170739+NehaKeshan@users.noreply.github.com> Date: Wed, 27 Mar 2024 20:11:11 -0400 Subject: [PATCH] Update README.md Updated the 18.6 exercise with the code written in class during lecture. --- lectures/18_trees_I/README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lectures/18_trees_I/README.md b/lectures/18_trees_I/README.md index c1a6f12..a17a8a7 100644 --- a/lectures/18_trees_I/README.md +++ b/lectures/18_trees_I/README.md @@ -104,10 +104,34 @@ public: 1. Write a templated function to find the smallest value stored in a binary search tree whose root node is pointed to by p. -2. Write a function to count the number of odd numbers stored in a binary tree (not necessarily a binary search +```cpp +template +const T& find_smallest (TreeNode * root) +{ + if(root->left == NULL) + { + return root->value; + } + return find_smallest(root->left); +} +``` + +3. Write a function to count the number of odd numbers stored in a binary tree (not necessarily a binary search tree) of integers. The function should accept a TreeNode pointer as its sole argument and return an integer. Hint: think recursively! +```cpp +int count_odd (TreeNode* int) +{ + if(node == nullptr) + { + return 0; + } + return abs(node->value % 2) + count_odd(node->left) + count_odd(node->right); +} + +``` + ## 18.7 ds_set and Binary Search Tree Implementation - A partial implementation of a set using a binary search tree is provided in this [ds_set_starter.h](ds_set_starter.h). We will continue to study this implementation in Lab 10 & the next lecture.