Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the complete code i used to build a binary tree datastructure and its corresponding operations: </p> <pre><code>&lt;?php class Node { public $data; public $leftChild; public $rightChild; public function __construct($data) { $this-&gt;data=$data; $this-&gt;leftChild=null; $this-&gt;rightChild=null; } public function disp_data() { echo $this-&gt;data; } }//end class Node class BinaryTree { public $root; //public $s; public function __construct() { $this-&gt;root=null; //$this-&gt;s=file_get_contents('store'); } //function to display the tree public function display() { $this-&gt;display_tree($this-&gt;root); } public function display_tree($local_root) { if($local_root==null) return; $this-&gt;display_tree($local_root-&gt;leftChild); echo $local_root-&gt;data."&lt;br/&gt;"; $this-&gt;display_tree($local_root-&gt;rightChild); } // function to insert a new node public function insert($key) { $newnode=new Node($key); if($this-&gt;root==null) { $this-&gt;root=$newnode; return; } else { $parent=$this-&gt;root; $current=$this-&gt;root; while(true) { $parent=$current; //$this-&gt;find_order($key,$current-&gt;data); if($key==($this-&gt;find_order($key,$current-&gt;data))) { $current=$current-&gt;leftChild; if($current==null) { $parent-&gt;leftChild=$newnode; return; }//end if2 }//end if1 else { $current=$current-&gt;rightChild; if($current==null) { $parent-&gt;rightChild=$newnode; return; } //end if1 } //end else }//end while loop }//end else } //end insert function //function to search a particular Node public function find($key) { $current=$this-&gt;root; while($current-&gt;data!=$key) { if($key==$this-&gt;find_order($key,$current-&gt;data)) { $current=$current-&gt;leftChild; } else { $current=$current-&gt;rightChild; } if($current==null) return(null); } return($current-&gt;data); }// end the function to search public function delete1($key) { $current=$this-&gt;root; $parent=$this-&gt;root; $isLeftChild=true; while($current-&gt;data!=$key) { $parent=$current; if($key==($this-&gt;find_order($key,$current-&gt;data))) { $current=$current-&gt;leftChild; $isLeftChild=true; } else { $current=$current-&gt;rightChild; $isLeftChild=false; } if($current==null) return(null); }//end while loop echo "&lt;br/&gt;&lt;br/&gt;Node to delete:".$current-&gt;data; //to delete a leaf node if($current-&gt;leftChild==null&amp;&amp;$current-&gt;rightChild==null) { if($current==$this-&gt;root) $this-&gt;root=null; else if($isLeftChild==true) { $parent-&gt;leftChild=null; } else { $parent-&gt;rightChild=null; } return($current); }//end if1 //to delete a node having a leftChild else if($current-&gt;rightChild==null) { if($current==$this-&gt;root) $this-&gt;root=$current-&gt;leftChild; else if($isLeftChild==true) { $parent-&gt;leftChild=$current-&gt;leftChild; } else { $parent-&gt;rightChild=$current-&gt;leftChild; } return($current); }//end else if1 //to delete a node having a rightChild else if($current-&gt;leftChild==null) { if($current==$this-&gt;root) $this-&gt;root=$current-&gt;rightChild; else if($isLeftChild==true) { $parent-&gt;leftChild=$current-&gt;rightChild; } else { $parent-&gt;rightChild=$current-&gt;rightChild; } return($current); } //to delete a node having both childs else { $successor=$this-&gt;get_successor($current); if($current==$this-&gt;root) { $this-&gt;root=$successor; } else if($isLeftChild==true) { $parent-&gt;leftChild=$successor; } else { $parent-&gt;rightChild=$successor; } $successor-&gt;leftChild=$current-&gt;leftChild; return($current); } }//end the function to delete a node //Function to find the successor node public function get_successor($delNode) { $succParent=$delNode; $successor=$delNode; $temp=$delNode-&gt;rightChild; while($temp!=null) { $succParent=$successor; $successor=$temp; $temp=$temp-&gt;leftChild; } if($successor!=$delNode-&gt;rightChild) { $succParent-&gt;leftChild=$successor-&gt;rightChild; $successor-&gt;rightChild=$delNode-&gt;rightChild; } return($successor); } //function to find the order of two strings public function find_order($str1,$str2) { $str1=strtolower($str1); $str2=strtolower($str2); $i=0; $j=0; $p1=$str1[i]; $p2=$str2[j]; while(true) { if(ord($p1)&lt;ord($p2)||($p1==''&amp;&amp;$p2=='')) { return($str1); } else { if(ord($p1)==ord($p2)) { $p1=$str1[++$i]; $p2=$str2[++$j]; continue; } return($str2); } }//end while } //end function find string order public function is_empty() { if($this-&gt;root==null) return(true); else return(false); } }//end class BinaryTree ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload