עזרה בפרולוג accumulators

lilithmm

New member
עזרה בפרולוג accumulators

אני מנסה לכתוב תוכנית שמקבלת עץ בניארי, וסורקת אותו בסדר תוכי. מצד אחד היא מסדרת את איברי העץ במערך בסדר תוכי, ומצד שני רצה עם מונה. התוכנית אמורה בסופו של דבר לקבל ערך key ולהחזיר את המיקום שלו בעץ, בסריקה התוכית. אני לא מצליחה לעבוד עם הaccumulators בשביל לשמור את הערך שאני מגדילה מונה בתנאי עצירה...מי יכול לעזור
הקלט:in_order(tree(a,tree(b,tree(c,void,void),void),tree(d,void,void)),X,Z,0,0,Key). in_order(void,Xs,Xs,Counter,Counter1,Key):- Counter1 is Counter+1. in_order(tree(X,L,R),Xs,Zs,Counter,Pos,Key):- in_order(L,Xs,[X|Ys],Counter,Pos,Key), in_order(R,Ys,Zs,Counter,Pos,Key).
 

zivsh103

New member
...

1. when posting code, lease click תחילת קוד before and סיום קוד after so it is from left to right (like this text). it is hard to read like you wrote it. 2. can you explain the problem again? (what you got and what you expected). thanks
 

lilithmm

New member
תודה

הבנתי בקשר לתחילת קוד סיום קוד... עכשיו אני אכתוב משהו ואני מייד מעבירה.
 

lilithmm

New member
הכנתי את זה. תעזרו לי בבקשה../images/Emo70.gif

מדובר בתוכנית שאמורה לצרף איברים מעץ בינארי לרשימה, ובדרך להראות עבור כל איבר בעץ את המיקום שלו בסדר תוכי. אני לא רוצה לעשות את זה לפי רשימה. בעצם, התוכנית אמורה להחזיר מיקום של איבר בסדר תוכי בלי הרשימה. בינתיים עבדתי רק על הסריקה, ושה-POS יראה בכל רגע את המיקום. בינתיים המונה כל הזמן מתאפס לי. האקומולטור לא עובד כמו שצריך ואני לא יודעת איפה הטעות שלי. תודה!!
in(void,Xs,Xs,Pos,Pos,Key). in_order(void,Xs,Xs,Counter,Pos,Key):- Counter1 is Counter+1, in(void,Xs,Xs,Counter1,Pos,Key). in_order(tree(X,L,R),Xs,Zs,Counter,Pos,Key):- in_order(L,Xs,[X|Ys],Counter,Pos,Key), in_order(R,Ys,Zs,Counter,Pos,Key).​
 

zivsh103

New member
...

1. maybe the in_order(R...) shuould be before the in_order(L...) ? 2. can you post the call to the function and the result you get? thanks​
 

lilithmm

New member
...

the inorder call which works well is the following: =========== in_order(void,Xs,Xs). in_order(tree(X,L,R),Xs,Zs):- in_order(L,Xs,[X|Ys]), in_order(R,Ys,Zs). ============== here is the code with the changes: in(void,Xs,Xs,Pos,Pos,Key). in_order(void,Xs,Xs,Counter,Pos,Key):- Counter1 is Counter+1, in(void,Xs,Xs,Counter1,Pos,Key). in_order(tree(X,L,R),Xs,Zs,Counter,Pos,Key):- in_order(L,Xs,[X|Ys],Counter,Pos,Key), in_order(R,Ys,Zs,Counter,Pos,Key). ====================== here is the input: in_order(tree(a,tree(b,tree(c,void,void),void),tree(d,void,void)),X,Z,0,T,Key). =========== Thanx again!!
 

zivsh103

New member
...

1. i am not sure exactly. maybe you can try something like this: --- Creating an in_order list of the tree elements (try, should work): inorder(void,[ ]). inorder(btree(Item,L_T,R_T),Inorder) :- inorder(L_T,Left), inorder(R_T,Right), append(Left,[Item|Right],Inorder). --- write a function find that returns the position of an element in a list or 0 if not in the list find (list, Key) ---- Now write (and call) your function findLocation something like this: findLocation(void,Key):- 0 findLocation(tree(Key,void,void),Key):- 1 findLocation(TREE,Key):- find(inorder(TREE),key) (this syntax may be incorrect, but it is the idea)​
 
למעלה