Converting a List into a Binary tree

Converting a List into a Binary tree

stanovb
Advocate Advocate
1,041 Views
7 Replies
Message 1 of 8

Converting a List into a Binary tree

stanovb
Advocate
Advocate

I am reading a book about common lisp and its talking about binary tree data structures. They give a list of (A B (C D) E) and they also give a graphical representation of it. I believe each box is either the CAR or the CDR of the cons cell. The terminal nodes of the tree are the atoms A, B, C, D, E, and NIL. The nonterminal nodes are the cons cells. 

stanovb_0-1721001738666.png

They say  "To view this diagram as a binary tree instead of a list, turn the page 45 degrees clockwise". I am having a little trouble visualizing what that would look like and I could use some help as to what the binary tree would look like. Its a very good book in my opinion. but just saying to turn it 45 degrees clockwise is not helping me visualize this.

0 Likes
1,042 Views
7 Replies
Replies (7)
Message 2 of 8

paullimapa
Mentor
Mentor

since autolisp does not support binary trees there's no builtin function to do this.

but there's a way using a different programming language:

python - How to convert a flat list to a binary tree - Stack Overflow


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 3 of 8

_gile
Consultant
Consultant

Hi,

The only AutoLISP data structure is a 'linked list' which can be seen as a binary tree where each 'cons cell' is a 'tree node', left 'leaves' are atoms and right 'leaves' are nil.

_gile_0-1721038800571.png

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 8

stanovb
Advocate
Advocate

Thanks. That might actually be it. The book told me to turn it at an angle, but that wasnt helping me

0 Likes
Message 5 of 8

_gile
Consultant
Consultant
'(A B (C D) E)

is a shortcut for:

(cons 'A (cons 'B (cons (cons 'C (cons 'D ())) (cons 'E ())))) 

which can be written using 'dotted pair notation':

'(A . (B . ((C . (D . ())) . (E . ()))))

 which can be seen as a binary tree:

_gile_0-1721040198682.png

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 8

stanovb
Advocate
Advocate

 I did find a video on what I guess is common lisp

https://www.youtube.com/watch?v=D4UA5pcGx58

The guy said in the video that if for instance if you have a list of numbers, the lesser number goes to the left and the greater number goes to the right

0 Likes
Message 7 of 8

_gile
Consultant
Consultant

The example in the video is a "binary search tree".

We can emulate this kind of tree by using nested lists.

For example, a tree can be a list which first item is a number (tree node), second item is the left child tree (a sublist) and the the third item is the right child tree (another sublist).

 

(defun list2tree (lst / insert tree)
  (defun insert	(ele tree)
    (cond
      ((null tree) (list ele nil nil))
      ((< ele (car tree))
       (list (car tree) (insert ele (cadr tree)) (caddr tree))
      )
      (T
       (list (car tree) (cadr tree) (insert ele (caddr tree)))
      )
    )
  )
  (foreach x lst (setq tree (insert x tree)))
)

 

 

Example:

 

_$ (list2tree '(6 1 8 5 3 4 9 2 7))
(6 (1 nil (5 (3 (2 nil nil) (4 nil nil)) nil)) (8 (7 nil nil) (9 nil nil)))

 

 

_gile_1-1721061369314.png

 

We can define a 'traversal' function to get the tree leaves from the lower to the greater item.

 

(defun traversal (tree)
  (append
    (if	(cadr tree)
      (traversal (cadr tree))
    )
    (list (car tree))
    (if	(caddr tree)
      (traversal (caddr tree))
    )
  )
)

 

 

This could be used to sort a list:

 

_$ (traversal (list2tree '(6 1 8 5 3 4 9 2 7)))
(1 2 3 4 5 6 7 8 9)

 

 

 

 

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8

stanovb
Advocate
Advocate

I guess that's sort of what they meant about turning it an an angle

stanovb_0-1721088010786.png

 

0 Likes