A tree is a non-linear hierarchical data structure that consists of nodes that are connected by edges. Tree stores data in a non-sequential manner so that operations like addition, deletion, updating, or searching could be performed in much less time than what it would take in linear data structures.
Tree traversal take logarithmic time for basic operations, while for linear data structures with the increase in size the time complexity increases linearly. Hence tree traversals are faster because log(n)<n.
In Lisp, to make a tree we use con cells, as lists of lists. We traverse through con cells for performing preorder, post-order, and in-order traversal.
Different Tree Functions in Lisp:
Lisp has some pre-defined functions which we could use on tree-data structures.
S.No. | Function | Description |
---|
1 | copy-tree x & optional vecp | It returns a copy of the tree of cons cells x. It recursively copies both the car and the cdr directions. If x is not a cons cell, the function simply returns x unchanged. If the optional vecp argument is true, this function copies vectors (recursively) as well as cons cells. |
---|
2 | tree-equal x y & key :test :test-not :key | It compares two trees of cons cells. If x and y are both cons cells, their cars and cdrs are compared recursively. If neither x nor y is a cons cell, they are compared by eql, or according to the specified test. The :key function, if specified, is applied to the elements of both trees. |
---|
3 | subst new old tree & key :test :test-not :key | Old items are replaced (substituted) with a new item, in a tree, which is a tree of cons cells. |
---|
4 | nsubst new old tree & key :test :test-not :key | It works the same as subst, the only difference is that it destroys the original tree. |
---|
5 | sublis alist tree & key :test :test-not :key | It works like subst, except that it takes an association list alist of old-new pairs. Each element of the tree (after applying the :key function), is compared with the cars of alist; if it matches, it is replaced by the corresponding cdr. |
---|
6 | nsublis alist tree & key :test :test-not :key | It works the same as sublis, but it is a destructive version. |
---|
In lisp, the car represents the first element of the list. cdr represents the rest of the list leaving the first element. car and cdr does not remove any elements from the list it just returns a report of what the elements are
Building Your Own Tree:
With the help of the tree and list functions, we can make a tree as follows:
Step 1: Function to create a node with data items in it.
Lisp
; the below function creates a node with items in it.
(defun make-tree (item)
(cons (cons item nil) nil)
)
Step 2: Function to add child nodes.
Lisp
; Function below will take two tree nodes and
; add the second tree as the child of the first.
(defun add-child (tree child)
(setf (car tree) (append (car tree) child))
tree
)
Step 3: Function to define the first child.
Lisp
; Function below will take a tree node and return
; the first child of that node or nil,
; if this node does not have any child node.
(defun first-child (tree)
(if (null tree)
nil
(cdr (car tree))
)
)
Step 4: Function to return the next sibling of a given node.
Lisp
; it takes a tree node as argument,
; and returns a reference to the next sibling node,
; or nil, if the node does not have any
(defun next-sibling (tree)
(cdr tree)
)
Step 5: Function to return the information in a node.
Lisp
;a function to return the information in a node
(defun data (tree)
(car (car tree))
)
Now let us build a whole tree by combining the codes written above in a single file.
Example:
Lisp
; Code for aLisp tree
; the below function creates a node with items in it.
(defun make-tree (item)
(cons (cons item nil) nil)
)
; Function below will take two tree
; nodes and add the second tree as the child of the
; first.
(defun add-child (tree child)
(setf (car tree) (append (car tree) child))
tree
)
; Function below will take a tree node and return
; the first child of that node or nil,
; if this node does not have any child node.
(defun first-child (tree)
(if (null tree)
nil
(cdr (car tree))
)
)
; it takes a tree node as argument, and returns
; a reference to the next sibling node,
; or nil, if the node does not have any
(defun next-sibling (tree)
(cdr tree)
)
;a function to return the information in a node
(defun data (tree)
(car (car tree))
)
(setq tree '((1 3 (2 6 9) ( (4 7 8)))))
(setq mytree (make-tree 60))
(write (data mytree))
(terpri)
(write (first-child tree))
(terpri)
(setq newtree (add-child tree mytree))
(terpri)
(write newtree)
Output:
Similar Reads
Strings in LISP
A string is a set of characters. String  are enclosed in double-quotes. Example: "hello geek","java","python" etc Example: LISP program to display strings Lisp ;edisplay hello geek (write-line "Hello Geek") ;display (write-line "Welcome to java") Output: Hello Geek Welcome to javaString Comparison
4 min read
Structures in LISP
LISP, is a list processing, is a programming language widely used in working with data manipulation. Structures are used defines data types, that have the ability to combine with another data type to complete the given task. Attribute used: The defstruct attribute is used to create an instance of a
2 min read
Predicates in LISP
In this article, we will discuss predicates. Predicates are similar to functions that will be used to test their arguments for conditions. They will return NIL if the conditions are not met, if the conditions are met, they will return T. Types of predicates: Below is a list of major Predicates with
5 min read
Lists in LISP
Lists in common LISP is simply a single Linked list. In LISP, Lists are designed as a chain of records. While talking about record structures in LISP, the concept of Cons is vital. Cons in LISP is a record structure with 2 Â primary components. A cons function takes in 2 arguments and returns a new c
2 min read
Recursion in LISP
In the Lisp programming language, recursion is a commonly used technique for solving problems. Lisp is a functional programming language, which means it is well-suited to recursive solutions. In LISP, recursion is a programming technique in which a function calls itself repeatedly until a certain co
4 min read
Sequences in LISP
In Lisp, the ordered set of elements is represented by sequences. All the functionality we use in sequences is applied on vectors and lists which are two of the subtypes of sequences. Creating a Sequence:The generic function for creating a Sequence in Lisp is: ;The generic function for creating a Se
7 min read
Numbers in LISP
LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. LISP Math Function:floor:Â floor returns the nearest smalles
2 min read
What is Binary Tree?
A binary tree is a type of tree data structure in which each node can have at most two child nodes, known as the left child and the right child. Each node of the tree consists of - data and pointers to the left and the right child. Example of Binary TreeProperties of a Binary Tree: The following are
3 min read
Full Binary Tree
What is a Binary Tree?A binary tree is a tree data structure with a maximum of 2 children per node. We commonly refer to them as the left and right child as each element in a binary tree may only have two children.What is a Full Binary Tree?A full binary tree is a binary tree with either zero or two
2 min read
Binary Tree in C
A binary tree is a non-linear hierarchical data structure in which each node has at most two children known as the left child and the right child. It can be visualized as a hierarchical structure where the topmost node is called the root node and the nodes at the bottom are called leaf nodes or leav
12 min read