Lecture Note
University
University of California San DiegoCourse
CSE 100/101 | Data Structures and AlgorithmsPages
2
Academic year
2023
anon
Views
45
Tree Crossing: Clarification Search using Depth First andBreadth First A tree is a type of data structure used in computer science that consists of nodes connected by edges. The process of going through each node in a tree in acertain order is known as tree traversal. Tree traversals can be divided into twocategories: depth-first search (DFS) and breadth-first search (BFS). We'llexamine the distinctions between these two varieties of tree traversals in thisarticle, as well as how to use them in programs. In-Depth Search (DFS) We fully explore one sub-tree in depth-first search before moving on to thesibling sub-tree. In-order and pre-order traversal are the two primary methodsused in DFS. Order-Based Traversal In-order traversal, which is used to print all the nodes of a binary search tree in a sorted order, is best defined for binary trees. Recursively movingthrough the left sub-tree, visiting the key (in this case, printing it), and thenmoving through the right sub-tree is how a tree is traversed in order. if we have a nil tree, we do nothing, otherwise, we traverse the left sub-tree, and thendo whatever we're going to do with the key, visit it, in this case, we're going to print it.But often there's just some operation you want to carry out, and then traverse the rightsub-tree. Take a binary search tree with the nodes Les, Cathy, Alex, Frank, Sam, Nancy, Violet, Tony, and Wendy as an illustration. This tree's nodes would beprinted in the following sequence if it were traversed in order: Alex, Cathy,Frank, Les, Nancy, Sam, Tony, Violet, and Wendy. Purchasing Traversal Pre-order traversal involves going to the root node first, then going back and forth across the left and right sub-trees. To obtain a prefix expression froman expression tree or to make a duplicate of a tree, this kind of traversal isfrequently utilized. First-Breadth Search (BFS)
Before moving on to the next level in a breadth-first search, we traverse every node at the previous level. BFS is employed while attempting todetermine the shortest route between any two nodes in a tree or the nearestcommon ancestor of any two nodes in a tree. A queue data structure can be used to implement BFS. The root node is first added to the queue, and after that the following actions are repeated untilthe queue is empty: ● Remove a node from the front of the waiting list. ● Go to the node. ● Put the node's offspring to the end of the queue. Up till all nodes have been visited, this procedure keeps on. In conclusion, two crucial tree traversal algorithms utilized in computer science are depth-first search and breadth-first search. Both can be done in codeusing a recursive or iterative method, albeit they have different use cases andapplications. A fundamental idea in computer science and a key tool forresolving many issues with computer algorithms is knowing how and when toemploy these algorithms.
Exploring Trees: Depth First vs. Breadth First Traversal Methods
Please or to post comments