树的宽度优先遍历

 /*parent对应父亲结点,child对应儿子结点,如果child is NULL则本结点为叶子结点*/
create table new_tree  ( parent varchar(80), child varchar(80))
go
insert new_tree values ( '1','2');
insert new_tree values ( '1','3');
insert new_tree values ( '2','4');
insert new_tree values ( '2','5');
insert new_tree values ( '3','6');
insert new_tree values ( '3','7');
insert new_tree values ( '3','8');
insert new_tree values ( '6','9');
insert new_tree values ( '5','10');
insert new_tree values ( '4','11');
insert new_tree values ( '9','12');
insert new_tree values ( '7',NULL);
insert new_tree values ( '8',NULL);
insert new_tree values ( '10',NULL);
insert new_tree values ( '11',NULL);
insert new_tree values ( '12',NULL);

drop proc proc_new_tree
go
/*@parent 输入根结点标识,@mode为0 则输出为所有子孙记录,否则输出所有叶子结点*/
create proc proc_new_tree (@parent varchar(80),@mode int =0)
as

begin
set nocount on
  /*如果不是SQLSERVER2000可以用临时表*/
  declare @tmp1  table ( parent varchar(80), child varchar(80))
declare @tmp2  table ( parent varchar(80), child varchar(80))
declare @tmp3  table ( parent varchar(80), child varchar(80))
  
insert @tmp1 select * from new_tree where parent = @parent
insert @tmp3 select * from new_tree where parent = @parent

  /*循环的次数等于树的深度*/
while exists(select * from @tmp1 where child is not NULL)
begin
insert @tmp2 select a.*  from new_tree a,@tmp1 b where a.parent = b.child
    /*@tmp2表中存本次查询的层次的所有结点*/
delete from @tmp1 where child is not NULL
    /*@tmp1表中最终存的是叶子结点*/
insert @tmp1 select * from @tmp2
    /*@tmp3表中最保存每次查到的子孙*/
insert @tmp3 select * from @tmp2
    delete from @tmp2
end
  if @mode =0 select * from @tmp3
else select * from @tmp1
set nocount off
end
go

proc_new_tree '1',1
go

以下是图的深度优先遍历宽度优先遍历的介绍: 深度优先遍历(DFS): 深度优先遍历是一种用于遍历或搜索或图的算法。在这种搜索方法中,从根结点开始,尽可能深地搜索每个分支,直到到达最深的结点,然后回溯到前一个结点,继续搜索另一个分支,直到所有结点都被访问为止。深度优先遍历使用栈来实现,因此也称为“栈搜索”。 以下是深度优先遍历的Python代码示例: ```python # Python3 program to print DFS traversal # from a given given graph from collections import defaultdict # This class represents a directed graph using # adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # A function used by DFS def DFSUtil(self, v, visited): # Mark the current node as visited # and print it visited.add(v) print(v, end=' ') # Recur for all the vertices # adjacent to this vertex for neighbour in self.graph[v]: if neighbour not in visited: self.DFSUtil(neighbour, visited) # The function to do DFS traversal. It uses # recursive DFSUtil() def DFS(self, v): # Create a set to store visited vertices visited = set() # Call the recursive helper function # to print DFS traversal self.DFSUtil(v, visited) ``` 宽度优先遍历(BFS): 宽度优先遍历是一种用于遍历或搜索或图的算法。在这种搜索方法中,从根结点开始,逐层遍历每个结点的所有子结点,直到到达最深的结点。宽度优先遍历使用队列来实现。 以下是宽度优先遍历的Python代码示例: ```python # Python3 program to print BFS traversal # from a given source vertex. BFS(int s) # traverses vertices reachable from s. from collections import defaultdict # This class represents a directed graph # using adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # Function to print a BFS of graph def BFS(self, s): # Mark all the vertices as not visited visited = [False] * (max(self.graph) + 1) # Create a queue for BFS queue = [] # Mark the source node as # visited and enqueue it queue.append(s) visited[s] = True while queue: # Dequeue a vertex from # queue and print it s = queue.pop(0) print(s, end=" ") # Get all adjacent vertices of the # dequeued vertex s. If a adjacent # has not been visited, then mark it # visited and enqueue it for i in self.graph[s]: if visited[i] == False: queue.append(i) visited[i] = True ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值