# Print and search a binary tree?

**URL:** <https://discuss.ocaml.org/t/print-and-search-a-binary-tree/1287>\
**Category:** Learning\
**Created:** [December 14, 2017, 6:43pm UTC](https://discuss.ocaml.org/t/print-and-search-a-binary-tree/1287 "2017-12-14T18:43:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cliff\_Karlsson](https://avatars.discourse-cdn.com/v4/letter/c/34f0e0/32.png) [@Cliff\_Karlsson](https://discuss.ocaml.org/u/Cliff_Karlsson)\
**Post date:** [December 14, 2017, 6:43pm UTC](https://discuss.ocaml.org/t/print-and-search-a-binary-tree/1287/1 "2017-12-14T18:43:02Z")

</div>

I have defined a tree like this:

type 'a btree =  
| EmptyTree  
| Node of ('a \* 'a btree \* 'a btree)  
;;

I am trying to define two functions, one that prints out every element of a tree. How do I do that when the tree can be a tree of ints but also something else like strings.

I also want to make a function that searches the tree for a specific value. Can anyone help me explain how to do this.

(\* val print\_tree: ('a -\> unit) -\> 'a btree -\> unit _)  
(_ val search\_tree: 'a -\> 'a btree -\> bool \*)

---

<div class="post-metadata">

**Author:** ![jeffsco](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jeffsco/32/45_2.png) [@jeffsco](https://discuss.ocaml.org/u/jeffsco)\
**Post date:** [December 14, 2017, 6:45pm UTC](https://discuss.ocaml.org/t/print-and-search-a-binary-tree/1287/2 "2017-12-14T18:45:29Z")

</div>

For the print function, you might think about what the first parameter (of type `'a -> unit`) is for.

For the search function, it would be best if you had a specific question. It would take the fun out of the assignment if somebody else just wrote the code for you.

---

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [December 14, 2017, 8:07pm UTC](https://discuss.ocaml.org/t/print-and-search-a-binary-tree/1287/3 "2017-12-14T20:07:36Z")

</div>

> [@jeffsco](#):
>
> It would take the fun out of the assignment if somebody else just wrote the code for you.

I strongly agree with that.
