Replace a tree node in a matched location of a binary tree

Gökhan Yu

How to replace a matched node in a binary tree with prolog? Properties of tree: it is not a binary search tree, but every element is unique, so replace operation will effect one element in tree at maximum.

initial tree definition:

tree('Q',
     tree('P',
          tree('R',
               empty,
               empty),
          tree('S',
               empty,
               empty)), 
     tree('T',
          empty,
          empty))

Let's say new node to replace node 'R' with tree('new', tree('child1', empty, empty), tree('child2', empty, empty)) expected result:

tree('Q',
     tree('P',
          tree(tree('new',
               tree('child1',
                    empty,
                    empty), 
               tree('child2',
                    empty,
                    empty)),
          tree('S',
                empty,
                empty)
               )),
     tree('T',
          empty,
          empty))

Current status of the code:

:- dynamic([tree/1]).

run:-
 retractall(tree(_)),
 assertz(tree(tree('Q', tree('P', tree('R', empty, empty), tree('S', empty, empty)), tree('T', empty, empty)))),
 retract(tree(T)),
 insert('newElement', T, NewTree),
 assertz(tree(NewTree)),
 tree(T),write(T),!.


insert(NewItem,empty,tree(NewItem,empty,empty)):- !.

insert(NewItem,tree(Element,Left,Right),tree(Element,NewLeft,Right)):-
   true, %match function needs to be here
   !,insert(NewItem,Left,NewLeft).

insert(NewItem,tree(Element,Left,Right),tree(Element,Left,NewRight)):-
    insert(NewItem,Right,NewRight).
CapelliC

I've changed a bit the syntax of your tree, since it was too much verbose for my taste. Maybe you could consider using a supported tree format, like XML (coded as element/3), that would give you much power in pattern matching, via library(xpath). Anyway

replace_tree(Old, New, Old, New).

replace_tree(Old, New, t(Key, L, R), t(Key, L1, R1)) :-
    replace_tree(Old, New, L, L1),
    replace_tree(Old, New, R, R1).

% base case of the recursive data structure
replace_tree(_Old, _New, t, t).

yields

?- T=t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3,t,t))),t), replace_tree(t(3,X,Y),t(new,X,Y),T,O).
T = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t),
X = t(4, t, t),
Y = t(5, t, t),
O = t(1, t(2, t(new, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t) ;
T = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t),
X = Y, Y = t,
O = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(new, t, t))), t) ;
T = O, O = t(1, t(2, t(3, t(4, t, t), t(5, t, t)), t(6, t, t(3, t, t))), t) ;
false.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deleteing a Node in a Binary tree

From Dev

Deleteing a Node in a Binary tree

From Dev

Binary tree,return the parent of the node

From Dev

Recursively Visiting a node in a binary tree

From Dev

Remove a node in binary search tree

From Dev

Finding the parent of a node in a Binary tree

From Dev

Binary Search Tree Remove Node

From Dev

Remove a node in binary search tree

From Dev

Finding the parent of a node in a Binary tree

From Dev

Binary Tree Node Class reference

From Dev

Binary tree,return the parent of the node

From Dev

Coordinates of every node in a binary tree?

From Dev

Editing a Node in a binary tree structure

From Dev

path to node in binary search tree as binary search tree

From Dev

in Binary Tree ,checking if given node is leaf node or not

From Dev

Find the parent node of a node in binary search tree

From Dev

Why the tree is not a binary tree?

From Dev

Find location of node in tree using Clojure zippers

From Dev

Find location of node in tree using Clojure zippers

From Dev

delete node in binary search tree python

From Dev

How to find a specific node in a non binary Tree?

From Dev

Exception: Deletion of node in Binary Search Tree

From Dev

Select a Node at Random from Unbalanced Binary Tree

From Dev

Creating a new Node for a binary search tree

From Dev

Deleting Root Node of a Binary Search Tree

From Dev

Node formula of a different kind of binary search tree

From Dev

Randomly select a node from a Binary Tree

From Dev

Computing rank of a node in a binary search tree

From Dev

Inserting a node into a binary search tree in C++