CLIPS的二叉树

时间:2016-01-04 08:58:02

标签: structure binary-tree clips

在剪辑中,我尝试实现二叉树结构,我知道如何在其他语言中实现二叉树,但我无法表示我的知识。

我是CLIPS的初学者。 我的尝试:

(deftemplate root
    (slot lchild)
    (slot rchild)
    (slot data))

(deftemplate node
    (slot lchild)
    (slot rchild)
    (slot data)
)
 ;------------------------------------------
(deffacts initial-facts
(emptyyy)
(root (lchild niil) (rchild niil) (data niil))
 )
;-----first insert
(defrule  insert-root-1
(initial-fact)
?emp <-(emptyyy)
?ro <-(root (lchild ?lr)(rchild ?rr)(data ?dr))
=>
(retract ?emp )
(assert (notemptyyy))
(printout t "enter your data:  "  )
(bind ?r  (read))
(modify ?ro (lchild niil)(rchild niil)(data ?r))
(printout t "---->root = " ?r  "----> leftchild= " ?lr    "--->  rightchild= " ?rr crlf)
)
;................
(defrule insert-node
(notemptyyy)
?ro <-(root (lchild ?lr)(rchild ?rr)(data ?dr))
=>
(printout t "enter your node data:  "  )
(bind ?dn  (read))
(if(<= ?dr  ?dn ) 
then
(modify ?ro (lchild ?lr)(rchild ?dn)(data ?dr))
(printout t "---->node = " ?dr  "----> leftchild= " ?lr    "--->  rightchild= " ?dn crlf)
else
(modify ?ro (lchild ?dn)(rchild ?rr)(data ?dr))
(printout t "---->node = " ?dr  "----> leftchild= " ?dn    "--->  rightchild= " ?rr crlf)
)
)
  

我不知道如何继续,这段代码只适用于根节点!!

2 个答案:

答案 0 :(得分:0)

正如您在过程语言中所做的那样,您必须遍历树以确定插入新节点的位置。您需要使用事实来跟踪您在树遍历中的位置。

(defglobal ?*next-id* = 0)

(deffunction next-id()
   (bind ?*next-id* (+ ?*next-id* 1)))

(deftemplate node
   (slot id)
   (slot parent (default none))
   (slot left-child (default none))
   (slot right-child (default none))
   (slot data))

(deftemplate process
   (slot data))

(deftemplate traverse
   (slot id))

(defrule get-data
   (not (process))
   =>
   (printout t "Enter data: ")
   (assert (process (data (read)))))

(defrule root-node
   ?f <- (process (data ?value))
   (test (numberp ?value))
   (not (node))
   =>
   (retract ?f)
   (assert (node (id 0) 
                 (data ?value))))

(defrule already-exists
   ?f <- (process (data ?value))
   (test (numberp ?value))
   (node (data ?value))
   =>
   (retract ?f)
   (printout t "Node already exists" crlf))

(defrule start-traverse
   (process (data ?value))
   (test (numberp ?value))
   (not (node (data ?value)))
   (node (id 0))
   =>
   (assert (traverse (id 0))))

(defrule add-left
   ?p <- (process (data ?value))
   ?t <- (traverse (id ?id))
   ?n <- (node (id ?id) (data ?ovalue) (left-child none))
   (test (< ?value ?ovalue))
   =>
   (retract ?p ?t)
   (bind ?nid (next-id))
   (modify ?n (left-child ?nid))     
   (assert (node (id ?nid) (data ?value) (parent ?id))))

(defrule add-right
   ?p <- (process (data ?value))
   ?t <- (traverse (id ?id))
   ?n <- (node (id ?id) (data ?ovalue) (right-child none))
   (test (> ?value ?ovalue))
   =>
   (retract ?p ?t)
   (bind ?nid (next-id))
   (modify ?n (right-child ?nid))     
   (assert (node (id ?nid) (data ?value) (parent ?id))))

(defrule traverse-left
   (process (data ?value))
   ?t <- (traverse (id ?id))
   (node (id ?id) (data ?ovalue) (left-child ?left&~none))
   (test (< ?value ?ovalue))
   =>
   (modify ?t (id ?left)))

(defrule traverse-right
   (process (data ?value))
   ?t <- (traverse (id ?id))
   (node (id ?id) (data ?ovalue) (right-child ?right&~none))
   (test (> ?value ?ovalue))
   =>
   (modify ?t (id ?right)))

答案 1 :(得分:0)

我在您的代码中添加了以下代码:

&#13;
&#13;
(defrule r-child
?t<- (rchild-node ?value)
  (node (id ?id) (data ?value) (right-child ?r))
  (node (id ?r) (data ?ovalue) )
=>
(retract ?t)
(printout t "right child node<--- " ?value " --> is:= " ?ovalue crlf ) 
)


(defrule l-child
?t<- (lchild-node ?value)
  (node (id ?id) (data ?value) (left-child ?l))
  (node (id ?l) (data ?ovalue) )
=>
(retract ?t)
(printout t "left child node<--- " ?value " --> is:= " ?ovalue crlf ) 
)

(defrule leave
  ?t<- (leaf)
  (node (id ?id) (data ?value) (left-child none) (right-child none) )
 =>
(printout t "-leaf- " ?value  crlf ) 
)

(defrule parent
?t<- (parent-node ?value)
(node (id ?id) (data ?value)(parent ?p  ))
(node (id ?p) (data ?ovalue) )
=>
(retract ?t)
(printout t "parent<--- " ?value " --> is:= " ?ovalue crlf ) 
)
&#13;
&#13;
&#13;