Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:04:57+00:00 2026-05-27T23:04:57+00:00

I want to write a program in Prolog that builds a list of all

  • 0

I want to write a program in Prolog that builds a list of all the even integers that reside in a b-tree.
This is what I’ve written so far. A predicate that counts all the elements in the tree. I don’t know where to scratch.

Domains
element=integer
tree=a(tree,element,tree);void
    list=integer*

Predicates
     nondeterm count(tree,element)
     nondeterm count_even(tree,list)

Clauses
    count(a(void,Number,void),1).
count(a(Esq,_,Dreta),Counter) :-
    count(Esq,Counter1),
    count(Dreta,Counter2),
    Counter=Counter1+Counter2+1.

Goal
       count(a(a(void,1,void),5,a(a(void,4,void),1,a(void,4,void))),N).

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:04:58+00:00Added an answer on May 27, 2026 at 11:04 pm

    Dunno anything about Visual Prolog, but in normal Prolog, I’d do something like the following…

    First, I’d denote an empty btree as the atom btree and represent a non-empty btree as a structure of arity 3, thus:

    btree( Payload, LeftChildren, RightChildren )
    

    where Payload is the data for the node (an integer apparently), with LeftChildren and RightChildren being the btrees representing, respectively, the left and right children of the current node.

    Traversing the tree to count those nodes with even-valued nodes is simple. The public predicate has arity 2, accepting the [bound] btree structure to be examined, and a bound or unbound value representing the count of even items. It calls an internal, recursive “helper” predicate that walks a tree and develops the count.

    count_even( T , N ) :- count_even( T , 0 , N ) .
    

    The internal predicate is simple as well. Having arity 3, the first argument is the tree to be examined, the second is an accumulator and the third is the final count (which won’t be unified until the very end). There are two possible cases.

    1. If the tree is empty, we have the final count:

      count_even( btree , N , N ) .
      
    2. If the tree is non-empty, we examine the current node, then recursively walk the left and right child trees, thusly:

      count_even( btree( V , L , R ) , T , N ) :-
        is_even( V , X ) ,
        T1 is T+X ,
        count_even( L , T1 , T2 ) ,
        count_even( R , T2 , N  )
        .
      

    We also need a trivial helper to tell us whether a particular value is even or odd:

    is_even( V , 1 ) :- 0 is V mod 2 , ! .
    is_even( V , 0 ) .
    

    It should be noted that the data structure you’re using is not a b-tree, per se: it is a binary tree.

    B-trees are something of a generalization of a height-balanced binary tree optimized for disk storage. Each node has a variable number of keys and a variable number of children (corresponding to the number of keys). For more information, see

    • http://en.wikipedia.org/wiki/B-tree
    • http://cis.stvincent.edu/html/tutorials/swd/btree/btree.html
    • http://www.dtic.upf.edu/~rramirez/TA/btrees.pdf

    Here’s a picture of a B-tree:

    B-Tree Visualization

    And a picture of a binary tree:

    Binary Tree Visualization

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time using map/reduce. I want to write a program that
I want to write a program that uses the GPL-licensed pandoc for Markdown processing.
I want to write a program in which plays an audio file that reads
I want to write a program that would print every combination of a set
I want to write a program in C/C++ that will dynamically read a web
I want to write a program that analyzes your fantasy baseball team and notifies
I want to write a program that sets cold to true if the user
I want to write a program to find all the com visible .NET classes,
I want to write a program that can turn on Caps lock key without
I want to write a program for iPhone 4 that uses its camera flash.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.