This time to bring you how to use the PHP sort binary tree, using PHP to sort the two-tree note what, the following is the actual case, take a look.
This demonstrates the function of the insertion of a sorted binary tree node, the middle sequence traversal, the search for extremum, and the search for a particular value.
There is basically no concept or definition provided. It is recommended to take a brief look at some of the concepts provided in this article.
In fact, simply provide the code, the comments are very few, you have worked hard.
two fork tree: in Computer science, a binary tree is a tree structure with a maximum of two subtrees per node.
sort binary Tree: The value of the left child node is less than the value of the parent node, and the value of the right child node is greater than the parent node value.
Several concepts:
Root node
Leaf node
Zuozi
Right sub-tree
Middle Sequence traversal
Pre-sequence traversal
Post-post traversal
Binary Tree Lookup
Middle Sequence Traversal:
The left subtree is traversed, traversing the node, and traversing the right node. The result after the traversal is a sorted result.
Created by Cuppenwy//Sort binary tree//complete the following tasks.//1. Insert the node in the corresponding position//2. Use the middle sequence traversal to traverse the binary tree//3. Find the extremum of this binary tree//4. Search for a specific value class node{public $key, $left, $right; Public function construct ($key) {$this->key = $key; }}class binarytree{public $root; Public $SORTARR = []; Insert node Public function Insertnode ($node, $newNode) {if ($node->key < $newNode->key) {//If parent node is smaller than child node, plug to the right if (Empty ($node->right)) {$node->right = $newNode; }else{$this->insertnode ($node->right, $newNode); }}elseif ($node->key > $newNode->key) {//If parent node is greater than child node, insert to left if (empty ($node->left)) {$node- >left = $newNode; }else{$this->insertnode ($node->left, $newNode); }}} Public function Insert ($key) {$newNode = new Node ($key); if (Empty ($this->root)) {$this->root = $newNode; }else{$this->insertnode ($this->root, $newNode); }}//middle order traverse public Function Midsort () {$this->midsortnoDe ($this->root); Public Function Midsortnode ($node) {if (!empty ($node)) {$this->midsortnode ($node->left); Array_push ($this->sortarr, $node->key); $this->midsortnode ($node->right); }//Search for extremum public Function findmin () {//Keep looking for its left subtree until the node of this left subtree is a leaf node. if (!empty ($this->root)) {$this->findminnode ($this->root); }} Public Function Findminnode (Node $node) {if (!empty ($node->left)) {$this->findminnode ($node->left); }else{echo ' The minimum value of this binary tree is: '. $node->key; }} Public Function Findmax () {if (!empty ($this->root)) {$this->findmaxnode ($this->root); }} Public Function Findmaxnode (Node $node) {if (!empty ($node->right)) {$this->findmaxnode ($node->right ); }else{echo ' The maximum value of this binary tree is: '. $node->key; }}//Find a specific value public function find ($val = ') {if (!empty ($val)) {$this->findnode ($this->root, $val); }} Public Function FindNode (Node $node, $val) {if ($node->key = = $val) {echo ' found '. $val. '; }else if ($node->key > $val) {//If the parent node's value is greater than the value to find, then find its left subtree if (!empty ($node->left) {$this- FindNode ($node->left, $val); }else{echo ' No this thing! '; }}else if ($node->key < $val) {if (!empty ($node->right)) {$this->findnode ($node->right, $val ); }else{echo ' No this thing! '; }}}} $tree = new BinaryTree (),//node inserted $nodes = Array (8,3,10,1,6,14,4,7,13), foreach ($nodes as $value) {$tree->inse RT ($value);} The middle sequence traversal//$tree->midsort ();//print_r ($tree->sortarr);//Search for Extrema//$tree->findmin ();//$tree->findmax ();// Find a specific value $tree->find (7); echo "<br/>"; $tree->find (11);
Operation Result:
We found 7.
There's no such thing!
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How to make JS array and JSON object dynamic implementation Add, modify, delete
How to implement server-side rendering using Vue+nuxt.js