Binary tree is a basic data structure in computer science. It is the most common tree structure, such as used in computer science for searching and sorting, and used in many other fields in computer science. PHP is a widely used server-side scripting language that supports dynamic web development. In this article, we will introduce how to implement a binary tree using PHP.
What is a binary tree?
Binary tree is composed of several nodes, each node has at most two child nodes. It has three attributes:
The binary tree is divided into the following Class:
Implementing binary tree
We can use classes to define binary tree structures. Each node is an object containing the following properties:
Create a class to represent the node.
class Node { public $value; public $left; public $right; function __construct($value){ $this -> value = $value; $this -> left = null; $this -> right = null; } }
Next, we need to create a class to represent the binary tree.
class BinarySearchTree { public $root; function __construct() { $this -> root = null; } }
Next, we will define the following binary tree methods:
Insert node
The insert node method will insert the new node to the correct position. If the tree is empty, the new node is the root node. Otherwise, we start comparing the current node's value from the root node.
This is the code for the insert method:
function insert($value) { $newNode = new Node($value); if ($this -> root == null) { $this -> root = $newNode; } else { $current = $this -> root; while (true) { if ($value < $current -> value) { if ($current -> left == null) { $current -> left = $newNode; return; } else { $current = $current -> left; } } else if ($value > $current -> value) { if ($current -> right == null) { $current -> right = $newNode; return; } else { $current = $current -> right; } } else { return; } } } }
Find Node
The Find Node method is a simple recursive method. Compare node values starting from the root node. If the values are equal, return the current node. Otherwise, if the node's value is less than the value you are looking for, continue searching the left subtree. If the value is greater than the value you are looking for, continue searching the right subtree.
This is the code for the find method:
function search($value) { $current = $this -> root; while ($current != null) { if ($value == $current -> value) { return $current; } else if ($value < $current -> value) { $current = $current -> left; } else { $current = $current -> right; } } return null; }
Delete Node
The delete node method is one of the most complex methods in the entire implementation. How a node is deleted depends on the number of children of the node. There can be the following situations:
This is the code for the delete method:
function delete($value) { $current = $this -> root; $parent = null; while ($current != null) { if ($value == $current -> value) { if ($current -> left == null && $current -> right == null) { if ($parent == null) { $this -> root = null; } else { if ($parent -> left == $current) { $parent -> left = null; } else { $parent -> right = null; } } } else if ($current -> left == null) { if ($parent == null) { $this -> root = $current -> right; } else { if ($parent -> left == $current) { $parent -> left = $current -> right; } else { $parent -> right = $current -> right; } } } else if ($current -> right == null) { if ($parent == null) { $this -> root = $current -> left; } else { if ($parent -> left == $current) { $parent -> left = $current -> left; } else { $parent -> right = $current -> left; } } } else { $replacementNode = $current -> right; while ($replacementNode -> left != null) { $replacementNode = $replacementNode -> left; } $removeValue = $replacementNode -> value; $this -> delete($replacementNode -> value); $current -> value = $removeValue; } return; } else if ($value < $current -> value) { $parent = $current; $current = $current -> left; } else { $parent = $current; $current = $current -> right; } } }
Conclusion
Now, we have learned how to implement a binary tree with php. Binary trees are a basic data structure in computer science, and many algorithms and applications involve it. We have learned how to insert nodes, find nodes and delete nodes. We can also extend this class to implement other practical methods.
The above is the detailed content of How to implement binary tree in php (code example). For more information, please follow other related articles on the PHP Chinese website!