php实现的二叉树遍历算法示例

2025-05-27 0 101

本文实例讲述了php实现的二叉树遍历算法。分享给大家供大家参考,具体如下:

今天使用php来实现二叉树的遍历

创建的二叉树如下图所示

php实现的二叉树遍历算法示例

php代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86
<?php

class Node {

public $value;

public $child_left;

public $child_right;

}

final class Ergodic {

//前序遍历:先访问根节点,再遍历左子树,最后遍历右子树;并且在遍历左右子树时,仍需先遍历根节点,然后访问左子树,最后遍历右子树

public static function preOrder($root){

$stack = array();

array_push($stack, $root);

while(!empty($stack)){

$center_node = array_pop($stack);

echo $center_node->value . ' ';

//先把右子树节点入栈,以确保左子树节点先出栈

if($center_node->child_right != null) array_push($stack, $center_node->child_right);

if($center_node->child_left != null) array_push($stack, $center_node->child_left);

}

}

//中序遍历:先遍历左子树、然后访问根节点,最后遍历右子树;并且在遍历左右子树的时候。仍然是先遍历左子树,然后访问根节点,最后遍历右子树

public static function midOrder($root){

$stack = array();

$center_node = $root;

while (!empty($stack) || $center_node != null) {

while ($center_node != null) {

array_push($stack, $center_node);

$center_node = $center_node->child_left;

}

$center_node = array_pop($stack);

echo $center_node->value . ' ';

$center_node = $center_node->child_right;

}

}

//后序遍历:先遍历左子树,然后遍历右子树,最后访问根节点;同样,在遍历左右子树的时候同样要先遍历左子树,然后遍历右子树,最后访问根节点

public static function endOrder($root){

$push_stack = array();

$visit_stack = array();

array_push($push_stack, $root);

while (!empty($push_stack)) {

$center_node = array_pop($push_stack);

array_push($visit_stack, $center_node);

//左子树节点先入$pushstack的栈,确保在$visitstack中先出栈

if ($center_node->child_left != null) array_push($push_stack, $center_node->child_left);

if ($center_node->child_right != null) array_push($push_stack, $center_node->child_right);

}

while (!empty($visit_stack)) {

$center_node = array_pop($visit_stack);

echo $center_node->value . ' ';

}

}

}

//创建二叉树

$a = new Node();

$b = new Node();

$c = new Node();

$d = new Node();

$e = new Node();

$f = new Node();

$g = new Node();

$h = new Node();

$i = new Node();

$a->value = 'A';

$b->value = 'B';

$c->value = 'C';

$d->value = 'D';

$e->value = 'E';

$f->value = 'F';

$g->value = 'G';

$h->value = 'H';

$i->value = 'I';

$a->child_left = $b;

$a->child_right = $c;

$b->child_left = $d;

$b->child_right = $g;

$c->child_left = $e;

$c->child_right = $f;

$d->child_left = $h;

$d->child_right = $i;

//前序遍历

Ergodic::preOrder($a); //结果是:A B D H I G C E F

echo '<br/>';

//中序遍历

Ergodic::midOrder($a); //结果是: H D I B G A E C F

echo '<br/>';

//后序遍历

Ergodic::endOrder($a); //结果是: H I D G B E F C A

希望本文所述对大家PHP程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php实现的二叉树遍历算法示例 https://www.kuaiidc.com/72244.html

相关文章

发表评论
暂无评论