二叉树深度优先与广度优先遍历
来源:互联网 发布:淘宝无线端店铺装修 编辑:程序博客网 时间:2024/06/18 07:08
<?php
class Node{
public $data = null;
public $left = null;
public $right = null;
}
function breadth_first_traverse($btree){
$traverse_data = array();
$queue = array();
array_unshift($queue, $btree);//根节点插入数组中
while(!empty($queue)){
$cnode = array_pop($queue);
$traverse_data[] = $cnode->data;
if($cnode->left != null){
array_unshift($queue, $cnode->left);
}
if($cnode->right != null){
array_unshift($queue, $cnode->right);
}
}
return $traverse_data;
}
function depth_first_traverse($btree){
$traverse_data = array();
$stack = array();
array_push($stack, $btree);
while(!empty($stack)){
$cnode = array_pop($stack);
$traverse_data[] = $cnode->data;
if($cnode->right != null){
array_push($stack, $cnode->right);
}
if($cnode->left != null){
array_push($stack, $cnode->left);
}
}
return $traverse_data;
}
$root = new Node();
$node1 = new Node();
$node2 = new Node();
$node3 = new Node();
$node4 = new Node();
$root->data = 1;
$node1->data = 2;
$node2->data = 3;
$node3->data = 4;
$node4->data = 5;
$root->left = $node1;
$root->right = $node2;
$node1->left = $node3;
$node1->right = $node4;
$traverse = breadth_first_traverse($root);
print_r($traverse);
echo "<br>";
$traverse = depth_first_traverse($root);
print_r($traverse);
?>
class Node{
public $data = null;
public $left = null;
public $right = null;
}
function breadth_first_traverse($btree){
$traverse_data = array();
$queue = array();
array_unshift($queue, $btree);//根节点插入数组中
while(!empty($queue)){
$cnode = array_pop($queue);
$traverse_data[] = $cnode->data;
if($cnode->left != null){
array_unshift($queue, $cnode->left);
}
if($cnode->right != null){
array_unshift($queue, $cnode->right);
}
}
return $traverse_data;
}
function depth_first_traverse($btree){
$traverse_data = array();
$stack = array();
array_push($stack, $btree);
while(!empty($stack)){
$cnode = array_pop($stack);
$traverse_data[] = $cnode->data;
if($cnode->right != null){
array_push($stack, $cnode->right);
}
if($cnode->left != null){
array_push($stack, $cnode->left);
}
}
return $traverse_data;
}
$root = new Node();
$node1 = new Node();
$node2 = new Node();
$node3 = new Node();
$node4 = new Node();
$root->data = 1;
$node1->data = 2;
$node2->data = 3;
$node3->data = 4;
$node4->data = 5;
$root->left = $node1;
$root->right = $node2;
$node1->left = $node3;
$node1->right = $node4;
$traverse = breadth_first_traverse($root);
print_r($traverse);
echo "<br>";
$traverse = depth_first_traverse($root);
print_r($traverse);
?>
0 0
- 二叉树的深度优先遍历与广度优先遍历
- 二叉树的深度优先遍历与广度优先遍历
- 二叉树的深度优先遍历与广度优先遍历
- 二叉树的深度优先遍历与广度优先遍历
- 二叉树的深度优先与广度优先遍历
- 二叉树深度优先与广度优先遍历
- 二叉树广度优先遍历+二叉树深度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的广度优先遍历和深度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历和广度优先遍历
- 二叉树的深度优先遍历以及广度优先遍历
- C语言极简入门-学习前的约定-1
- Unity3d之截图方法
- 通过UIGraphics重新设置image的大小
- exp/imp实战之imp导入
- [C++11]_[初级]_[使用std::move移动对象资源]
- 二叉树深度优先与广度优先遍历
- wodpress博客的评论外链转内链
- OC--继承 self 的概念
- Java 8无人谈及的八大功能
- TCP和UDP区别
- 使用WebRTC搭建前端视频聊天室——信令篇
- 浅谈HTTP中Get与Post的区别-java
- 对称加密与非对称加密
- 11 款用于优化、分析源代码的Java工具