Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a predicate called depth(E, Tree, N) that is true if and only if element E is at depth N in the given binary search

image text in transcribed
image text in transcribed
Write a predicate called depth(E, Tree, N) that is true if and only if element E is at depth N in the given binary search tree Tree in (Root, Left, Right] format. Here's more explanation: A binary search tree can be represented as either an empty list, (), or a list of the form (Root, Left, Right] where Root is a number and Left and Right are themselves binary search trees in which all of the items in Left are less than or equal to Root and all items in Right are greater than Root. For example, here's a predicate that defines a small binary search tree represented in this way: tree([42, (5, 1), (), [47, 0], [50, 0), []]]). The depth of an item in the tree is 0 if its at the root, 1 if its parent is the root, 2 if its parent's parent is at the root, and so forth. Here are some examples using the tree defined above: 7- tree (T), depth (42, T, N). T - [42, (5, ti, 0], [47, 1), (50, [], []]]], N=0; % any more? false ?- tree (T), depth (50, T, N). T = [42, (5, [], 0], [47, [], [50, [], []]], N = 2;% any more? false. You may assume that we'll only test the depth predicate on items that are in the tree and that each item in the tree only appears once. % some "nice" prolog settings... :- set_prolog_flag( prompt_alternatives_on, groundness ). : - set_prolog_flag(toplevel_print_options, [quoted(true), portray(true), attributes (portray), max_depth (999), priority (699)]). : - set prolog_flag(answer_write_options, [quoted(true), portray(true), attributes (portray), max_depth(999), priority (699)]). % We start you with the base case. % STARTER % depth(E, [E, _, _], 0). $ You finish the following cases. % YOU DO % %%%%%%%%%% depth(E, [Root, Left, Right), N) :- fail. depth(E, [Root, Left, Right), N) :- fail

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions