Breadth First Search VS Depth First Search (Algorithms)

First lets recall the concept for BFS and DFS.

I will use below Binary Tree as an example.

Before that, lets go through some of the concepts of Trees and Binary Trees quickly.

Concept of Binary Trees

A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data element. The topmost node in the tree is called the root.

More tree terminology:

    • The depth of a node is the number of edges from the root to the node.
    • The height of a node is the number of edges from the node to the deepest leaf.
    • The height of a tree is a height of the root.

Concept of Binary search Tree.

Binary Search Trees

We consider a particular kind of a binary tree called a Binary Search Tree (BST). The basic idea behind this data structure is to have such a storing repository that provides the efficient way of data sorting, searching and retriving.

    A BST is a binary tree where nodes are ordered in the following way:

  • each node contains one key (also known as data)
  • the keys in the left subtree are less then the key in its parent node, in short L < P;
  • the keys in the right subtree are greater the key in its parent node, in short P < R;
  • duplicate keys are not allowed.

In the following tree all nodes in the left subtree of 10 have keys < 10 while all nodes in the right subtree > 10. Because both the left and right subtrees of a BST are again search trees; the above definition is recursively applied to all internal nodes:

Traversals

Traversal is the process that visits all the nodes in a tree. Since a tree is a non-linear data structure, there is no unique traversal. We will consider several traversal algorithms which we group in the following two kinds.

  • Depth-first traversal.
  • Breadth-first traversal.

There are three different types of depth-first traversals, :

  • PreOrder traversal :  Visit Parent First, then left child, and then right child.
  • InOrder traversal :  Visit Left Child, then Parent, then Right child
  • PostOrder traversal:  visit left child, then the right child and then the parent;

There is only one kind of breadth-first traversal--t

  • the level order traversal. This traversal visits nodes by levels from top to bottom and from left to right.

Example.

for below tree:

PreOrder traversal 8 ->5->9->7->1->12->2->4->11->3

InOrder traversal   9-> 5->1->7->2->12->8->4->3->11

PostOrder Traversal  9->1->2->12->7->5->3->11->4->8

Level Order  8->5->4->9->7->11->1->12->3->2

Binary Search Tree Insertion

The insertion procedure is quite similar to searching.

We start at the root and recursively go down the tree searching for a location in a BST to insert a new node.

If the element to be inserted is already in the tree, we are done (we do not insert duplicates).

The new node will always replace a NULL reference.

Exercise

Exercise. Given a sequence of numbers:

11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31

Draw a binary search tree by inserting the above numbers from left to right.

(answer to below)

Searching

Searching in a BST always starts at the root. We compare a data stored at the root with the key we are searching for (let us call it as toSearch). If the node does not contain the key we proceed either to the left or right child depending upon comparison. If the result of comparison is negative we go to the left child, otherwise - to the right child. The recursive structure of a BST yields a recursive algorithm.

Please refer to below link to for "Deletion in Binary Search Tree";

https://www.cs.cmu.edu/~adamchik/15-121/lectures/Trees/trees.html

原文地址:https://www.cnblogs.com/codingyangmao/p/11328344.html

时间: 2024-10-14 13:43:52

Breadth First Search VS Depth First Search (Algorithms)的相关文章

DFS --- Depth First Search 深度优先搜索算法

Depth First Search 原理还是去看<DSAA>,这里着重分析实现策略. 如果对于图这种数据结构不熟悉,这个BFS一般是搞不定的... 下面分别是无向图的邻接表实现和邻接矩阵实现 http://blog.csdn.net/cinmyheart/article/details/41381845 http://blog.csdn.net/cinmyheart/article/details/41370465 --------------------------------------

python实现基础的深度优先搜索(DFS, depth first search)解决数的全排列问题

数的全排列,是一个很简单的问题,平时我们用笔用纸就能列出答案,但是数列位多的时候,排列的结果就有非常多了,例如有1,2,3,4,5,6,7,8,9这一个数列,有9个数字,则有9!(9的阶乘)这么多种结果.那是非常大的.今天我就来介绍用深度优先搜索来解决这个数的全排列的问题. 深度优先搜索 首先简单介绍一下深度优先搜索,深度优先搜索的关键在于当下该如何做,至于下一步如何做,就与当下做的一样.深度优先搜索的基本模型为: dfs(step): 判断边界:执行相关操作,返回 尝试每一种可能 for( i

[Algorithms] Refactor a Linear Search into a Binary Search with JavaScript

Binary search is an algorithm that accepts a sorted list and returns a search element from the list. It provides a dramatic performance boost over searching linearly through a list for an element. Let’s play around number of iterations required for e

PAT Search in a Binary Search Tree

Search in a Binary Search Tree To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison results of the keys. The searching path corresponds to a sequence of keys. For exa

04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

pat04-树7. Search in a Binary Search Tree (25)

04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue To search a key in a binary search tree, we start from the root and move all the way down, choosing branches according to the comparison res

[Leetcode]700. Search in a Binary Search Tree

700. Search in a Binary Search Tree 本题难度: Easy Topic: Binary Tree Description Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted

Chapter five Depth First Search(深度优先搜索)

组合搜索问题 Combination 问题模型:求出所有满足条件的"组合".判断条件:组合中的元素是顺序无关的.时间复杂度:与 2^n 相关. 1.Chapter one 第2题 subsets(子集) 2.Chapter one 第3题 subsets-ii(子集II) 3.combination-sum(数字组合) 给出一组候选数字(C)和目标数字(T),找到C中所有的组合,使找出的数字和为T.C中的数字可以无限制重复被选取.所有的数字(包括目标数字)均为正整数.元素组合(a1, 

深度优先搜索(DFS: Depth First Search)

深度优先搜索是一种树的遍历方式.与此对应的是广度优先搜索. ? 二叉树的优先搜索: ? 如何把一个数学问题转换为树的深度优先搜索问题: 例如:各位数之和为偶数的一个10位二进制数有几个. 我们来分析一下这个问题,首先一共有10位数,然后每一位数都只有两种状态0,1 这可以看做是一个深度为10的一个二叉树,然后用树的深度优先搜索即可解决问题. ? 用C语言实现的代码结构 void DFS(int depth) { ????if(depth==10)????????//递归出口 ????{ ????