PHP操作二叉树

 1 <?php
  2 class BinaryTree
  3 {
  4         public $value;
  5         public $left;
  6         public $right;
  7 }
  8
  9 function preorder($root)
 10 {
 11         $stack = array();
 12         $centerNode = $root;
 13         array_push($stack, $centerNode);
 14         while (!empty($stack))
 15         {
 16                 $centerNode = array_pop($stack);
 17                 echo $centerNode->value."\n";
 18                 if ($centerNode->right)
 19                 {
 20                         array_push($stack, $centerNode->right);
 21                 }
 22                 if ($centerNode->left)
 23                 {
 24                         array_push($stack, $centerNode->left);
 25                 }
 26         }
 27 }
 28
 29 function midorder($root)
 30 {
 31         $centerNode = $root;
 32         $stack = array();
 33         while (!empty($stack) || !is_null($centerNode))
 34         {
 35                 while (!is_null($centerNode))
 36                 {
 37                         array_push($stack, $centerNode);
 38                         $centerNode = $centerNode->left;
 39                 }
 40                 $centerNode = array_pop($stack);
 41                 echo $centerNode->value."\n";
 42                 $centerNode = $centerNode->right;
 43         }
 44 }
 45
 46 function postorder($root)
 47 {
 48         $stack = array();
 49         $outStack = array();
 50         array_push($stack, $root);
 51         while (!empty($stack))

 52         {

53                 $centerNode = array_pop($stack);
 54                 array_push($outStack, $centerNode);
 55                 if (!is_null($centerNode->left))
 56                 {
 57                         array_push($stack, $centerNode->left);
 58                 }
 59                 if (!is_null($centerNode->right))
 60                 {
 61                         array_push($stack, $centerNode->right);
 62                 }
 63         }
 64         while (!empty($outStack))
 65         {
 66                 $centerNode = array_pop($outStack);
 67                 echo $centerNode->value."\n";
 68         }
 69 }
 70
 71 //广度优先遍历
 72 function widthFirst($root)
 73 {
 74         $queue = array();
 75         array_unshift($queue, $root);
 76         while (!empty($queue))
 77         {
 78                 $no = array_pop($queue);
 79                 echo $no->value.'    ';
 80                 if (!is_null($no->left))
 81                 {

 82                         array_unshift($queue, $no->left);

 83                 }
 84                 if (!is_null($no->right))
 85                 {
 86                         array_unshift($queue, $no->right);
 87                 }
 88         }
 89 }
 90
 91 //深度优先遍历
 92 function deepFirst($root)
 93 {
 94         $stack = array();
 95         array_push($stack, $root);
 96         while (!empty($stack))
 97         {
 98                 $no = array_pop($stack);
 99                 echo $no->value.'   ';
100                 if (!is_null($no->right))
101                 {
102                         array_push($stack, $no->right);
103                 }
104                 if (!is_null($no->left))
105                 {
106                         array_push($stack, $no->left);
107                 }
108         }
109 }
110
111 //求二叉树深度
112 //todo 利用广度优先遍历
113 function getDeepth($root)
114 {
115         $queue = array();
116         array_unshift($queue, $root);
117         $nextCount = 1;
118         $count = 0;
119         $depth = 0;
120         while (!empty($queue))
121         {
122                 $count++;
123                 $node = array_pop($queue);
124                 if (!is_null($node->left))
125                 {
126                         array_unshift($queue, $node->left);
127                 }
128                 if (!is_null($node->right))
129                 {
130                         array_unshift($queue, $node->right);
131                 }
132                 if ($count == $nextCount)
133                 {

134                         $count =0;

135                         $nextCount = count($queue);
136                         $depth++;
137                 }
138         }
139         return $depth;
140 }
141
142 //判断是否是平衡二叉树
143 function isBalance($root)
144 {
145         $leftDepth = getDeepth($root->left);
146         $rightDepth = getDeepth($root->right);
147         if ($leftDepth-$rightDepth>1 || $rightDepth-$leftDepth>1)
148         {
149                 return 0;
150         }
151         return isBalance($root->left)&&isBalance($root->right);
152 }
153
154
155        5
156
157     3         7
158
159 1     4    6     8
160 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值