# Binary trees

**URL:** <https://swi-prolog.discourse.group/t/binary-trees/280>\
**Category:** Help!\
**Created:** [February 14, 2019, 10:13am UTC](https://swi-prolog.discourse.group/t/binary-trees/280 "2019-02-14T10:13:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gretta](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/gretta/32/100_2.png) [@Gretta](https://swi-prolog.discourse.group/u/Gretta)\
**Post date:** [February 14, 2019, 10:13am UTC](https://swi-prolog.discourse.group/t/binary-trees/280/1 "2019-02-14T10:13:21Z")

</div>

Can anybody help me to solve these prolog exercises:  
A) Write a prolog program that given a binary tree whose nodes contain positive integers, checks whether the tree is odd, i.e. it contains only odd numbers. To check whether the number is odd, use the predicate odd(X)  
B) writer a prolog program that given a binary tree whose nodes contain positive integers returns a binary tree that is like the input tree but for the subtrees whose root nodes are eve numbers. Such subtrees should be replaced by leaf nodes containing the number 13

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [February 14, 2019, 11:39am UTC](https://swi-prolog.discourse.group/t/binary-trees/280/2 "2019-02-14T11:39:10Z")

</div>

On page 72 of the book I recommended (page 113 of the PDF) you will find the section “3.4 Binary Trees”. It is a very good starting point.

Otherwise it almost looks like you are just posting your homework assignment and expecting someone to do it for you.

---

<div class="post-metadata">

**Author:** ![deluxeroot](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@deluxeroot](https://swi-prolog.discourse.group/u/deluxeroot)\
**Post date:** [February 23, 2020, 12:04pm UTC](https://swi-prolog.discourse.group/t/binary-trees/280/3 "2020-02-23T12:04:50Z")

</div>

Which book? Please I need the PDF.

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [February 23, 2020, 12:31pm UTC](https://swi-prolog.discourse.group/t/binary-trees/280/4 "2020-02-23T12:31:54Z")

</div>

I guess “The Art of Prolog” (second edition) by Sterling and Shapiro. The PDF could be downloaded from the book’s website. I hope this is still the case.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 23, 2020, 12:32pm UTC](https://swi-prolog.discourse.group/t/binary-trees/280/5 "2020-02-23T12:32:01Z")

</div>

It is also here if you need it.

[Useful Prolog references](https://swi-prolog.discourse.group/t/useful-prolog-references/1089)

See: “The Art of Prolog” By Leon S. Sterlingand and Ehud Y. Shapiro ([site](https://mitpress.mit.edu/books/art-prolog-second-edition)) ([pdf](https://www.dropbox.com/s/5pilbnmlx5nypmh/1408.pdf?dl=1))

---

<div class="post-metadata">

**Author:** ![deluxeroot](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@deluxeroot](https://swi-prolog.discourse.group/u/deluxeroot)\
**Post date:** [February 23, 2020, 12:39pm UTC](https://swi-prolog.discourse.group/t/binary-trees/280/6 "2020-02-23T12:39:10Z")

</div>

Thanks. I have exam of Prolog and there will be questions about lists and binary tree and guessing the output of the given program. Is this book enough for me?

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 23, 2020, 12:42pm UTC](https://swi-prolog.discourse.group/t/binary-trees/280/7 "2020-02-23T12:42:39Z")

</div>

> [@deluxeroot](#):
>
> Is this book enough for me?

That is hard to say. I don’t know what you know and I don’t know what will be on the test.

That is a question for the instructor of the class.
