codepad.viper-7.com
Script:
(hide)
Author
unknown
Size:
2194 b
Created:
21/01/2013 1:27pm
Version:
2
Hits:
68
Render Time:
0.00 ms
PHP Version
trunk-dev
5.5-dev
5.4.9
5.4-dev
5.3.19
5.3.10
5.3-dev
5.2.17
Output:
HTML
(hide)
Code:
(hide)
<?php
class Node{
public $parent;
public $left;
public $right;
public $data;
public $level;
public function __construct($data){
$this->data = $data;
}
public function __toString() {
return $this->data;
}
}
class BST{
private $_root;
static $counter;
public function __construct(){
$this->_root = NULL;
}
public function insert($data){
$new = New Node($data);
$this->_insert($new, $this->_root);
}
private function _insert(&$new, &$node){
if($node == NULL){
$node = $new;
}else{
if($new->data <= $node->data){
if($node->left == NULL){
$node->left = $new;
$new->parent = $node;
}else{
$this->_insert($new, $node->left);
}
}else{
if($node->right == NULL){
$node->right = $new;
$new->parent = $node;
}else{
$this->_insert($new, $node->right);
}
}
}
}
public function kthSmallest(){
return $this->_kthSmallest($this->_root, 2);
}
public function _kthSmallest($node, $k){
if($node->left != NULL){
$this->_kthSmallest($node->left, $k);
}
echo $node->data . ' ';
self::$counter++;
echo self::$counter . "<br/>";
if(self::$counter > $k){
return $node->data;
}
if($node->right != NULL){
$this->_kthSmallest($node->right, $k);
}
}
}
$tree = new BST();
$tree->insert('5');
$tree->insert('3');
$tree->insert('7');
$tree->insert('2');
$tree->insert('4');
$tree->insert('8');
$tree->insert('10');
echo $tree->kthSmallest();
//$tree->printBFS();
//var_dump($tree->isValidBST());
?>
User
Create Account
Log In
Views
Request Headers
Response Headers
Response Body
Opcodes
Profile
Call Graph
Controls
New Paste
Paste History
unknown:
untitled
unknown:
untitled