This document contains a Prolog program for performing operations on binary trees, including inserting and deleting nodes, and traversing trees using pre-order, in-order, post-order, and breadth-first ordering. It provides clauses for these operations and asks the reader to test the clauses, write a clause to combine two trees, and write a clause to find the path from the root to a given element.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online from Scribd
This document contains a Prolog program for performing operations on binary trees, including inserting and deleting nodes, and traversing trees using pre-order, in-order, post-order, and breadth-first ordering. It provides clauses for these operations and asks the reader to test the clauses, write a clause to combine two trees, and write a clause to find the path from the root to a given element.
This document contains a Prolog program for performing operations on binary trees, including inserting and deleting nodes, and traversing trees using pre-order, in-order, post-order, and breadth-first ordering. It provides clauses for these operations and asks the reader to test the clauses, write a clause to combine two trees, and write a clause to find the path from the root to a given element.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online from Scribd
This document contains a Prolog program for performing operations on binary trees, including inserting and deleting nodes, and traversing trees using pre-order, in-order, post-order, and breadth-first ordering. It provides clauses for these operations and asks the reader to test the clauses, write a clause to combine two trees, and write a clause to find the path from the root to a given element.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online from Scribd
Download as doc, pdf, or txt
You are on page 1of 2
BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE
Second Semester 2007-2008
EA C461 Artificial Intelligence
Lab 1: Tree Traversal in PROLOG
Document Prepared By:
Mukesh Kumar Rohil, CS & IS Group BITS, Pilani – 333031 (Rajasthan), India rohil@bits-pilani.ac.in
Given below is a program containing some operations which we can
perform on a binary-tree. Do the following:
1. Test each of the clauses for a sample binary tree.
2. Write a clause to combine two binary trees and get one binary tree. 3. Write a clause so as to find a path from root to a given element if the element is a node of the tree.
/* insert_leaf(X, Tree, Tree1) is true if Tree1 is the result of
inserting */ /* the element X as a leaf in the ordered binary tree Tree. If the */ /* element X is already in the tree, the tree is unchanged. */ insert_leaf(X, void, tree(X,void,void)):-!. insert_leaf(X, Tree, Tree):- Tree=tree(X,_,_), !. insert_leaf(X, tree(Y,L,R), tree(Y,L1,R)):- X < Y, !, insert_leaf(X, L, L1). insert_leaf(X, tree(Y,L,R), tree(Y,L,R1)):- insert_leaf(X, R, R1).
/* delete_node(X, Tree, Tree1) is true if Tree1 is the result of
deleting */ /* the element X from the ordered binary tree Tree. */ delete_node(X, tree(X,L,void), L):-!. delete_node(X, tree(X,L,R), Tree):-!, left_rest(R, Y, R1), Tree=tree(Y,L,R1). delete_node(X, tree(Y,L,R), tree(Y,L1,R)):- X < Y, !, delete_node(X, L, L1). delete_node(X, tree(Y,L,R), tree(Y,L,R1)):- delete_node(X, R, R1).
/* left_rest(Tree, Left, Rest) is true if Left is the leftmost element
in */ /* the binary tree Tree, and Rest is the rest of the tree. */ left_rest(tree(X,void,R), X, R):-!. left_rest(tree(X,L,R), Y, tree(X,L1,R)):-left_rest(L, Y, L1). /* pre_order(Tree, L) is true if L is a pre-order traversal of the binary */ /* tree Tree. */ pre_order(T, L):-pre_order_dl(T, L, []).
/* breadth_order(BinaryTree, List) is true if List is the level-by-
level */ /* traversal of BinaryTree. */ /* This procedure uses a queue implemented as a difference list with a */ /* counter. It can be used backwards, that is, it can be used to */ /* enumerate, by backtracking, every BinaryTree for which List is the */ /* level-by-level traversal. For a list of n elements, the number of */ /* binary trees is the n-th Catalan number - see "Data Structures and */ /* Program Design in C" by Kruse, Leung and Tondo. */ breadth_order(Tree, List):- breadth_order_1(s(zero), [Tree|Trees], Trees, List).