From 4f4fae8b8f85ab998ff95d41d2f40cd893752bed Mon Sep 17 00:00:00 2001
From: poulpe <poulpe@localhost.localdomain>
Date: Wed, 24 Mar 2021 15:14:23 +0100
Subject: [PATCH] [Update] Add is_present

---
 tests/test.c | 34 ++++++++++++++++++++++++++++++++++
 tree.c       | 19 +++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/tests/test.c b/tests/test.c
index 92cb42c..0d2eb54 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -55,6 +55,14 @@ static MunitResult test_tree_length()
   nd = bst_insert(nd,62);
   nd = bst_insert(nd,72);
   nd = bst_insert(nd,82);
+  nd = bst_insert(nd,12);
+  nd = bst_insert(nd,22);
+  nd = bst_insert(nd,32);
+  nd = bst_insert(nd,42);
+  nd = bst_insert(nd,2);
+  nd = bst_insert(nd,3);
+  nd = bst_insert(nd,4);
+  nd = bst_insert(nd,5);
   munit_assert_ptr(nd,!=,NULL);
   bst_destroy(nd);
   return MUNIT_OK;
@@ -70,6 +78,24 @@ static MunitResult test_tree_is_empty()
   return MUNIT_OK;
 }
 
+static MunitResult test_tree_is_present()
+{
+  node_t* nd = bst_create();
+  munit_assert_false(bst_is_present(nd,10));
+  nd = bst_insert(nd,12);
+  nd = bst_insert(nd,22);
+  nd = bst_insert(nd,32);
+  nd = bst_insert(nd,42);
+  nd = bst_insert(nd,2);
+  nd = bst_insert(nd,3);
+  nd = bst_insert(nd,4);
+  nd = bst_insert(nd,5);
+  nd = bst_insert(nd,10);
+  munit_assert_true(bst_is_present(nd,10));
+  bst_destroy(nd);
+  return MUNIT_OK;
+}
+
 
 static MunitTest test_suite_tests[] = {
   {
@@ -112,6 +138,14 @@ static MunitTest test_suite_tests[] = {
     MUNIT_TEST_OPTION_NONE,
     NULL
   },
+  {
+    (char*) "/test_tree_is_present",
+    test_tree_is_present,
+    NULL,
+    NULL,
+    MUNIT_TEST_OPTION_NONE,
+    NULL
+  },
   
   { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
 };
diff --git a/tree.c b/tree.c
index 44bf7ac..ccd259e 100644
--- a/tree.c
+++ b/tree.c
@@ -34,6 +34,25 @@ bool bst_is_empty(node_t *tree)
     return (tree == NULL)?true:false;
 }
 
+bool bst_is_present(node_t *tree, int val)
+{
+    if(tree == NULL){return false;}
+    if(tree->val == val)
+    {
+        return true;
+    }
+    else
+    {
+        if(val > tree->val)
+        {
+            return bst_is_present(tree->right,val);
+        }else
+        {
+            return bst_is_present(tree->left,val);
+        }
+    }
+}
+
 node_t *bst_insert(node_t *tree, int val)
 {
     if(tree == NULL)
-- 
GitLab