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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read