PHP binary tree (1): binary search tree

Source: Internet
Author: User
There are a lot of resources on the Internet about the principle of the binary search tree, and the situation is a little complicated. so I will not describe it here. let's go directly to the code: there are a lot of resources on the Internet about the principle of the binary search tree, and the situation is a little complicated. so I will not describe it here. let's go directly to the code:

# Bst. php file
 Key = $ key; $ this-> parent = NULL; $ this-> left = NULL; $ this-> right = NULL ;}} // binary search tree class Bst {public $ root;/*** initialize the tree structure * @ param $ arr initialize the tree structure array * @ return null */public function init ($ arr) {$ this-> root = new Node ($ arr [0]); for ($ I = 1; $ I <count ($ arr); $ I ++) {$ this-> Insert ($ arr [$ I]) ;}/ *** (internal) sequential traversal * @ param $ root (tree or subtree) root node * @ return null */private function mid_order ($ root) {if ($ root! = NULL) {$ this-> mid_order ($ root-> left); echo $ root-> key. ""; $ this-> mid_order ($ root-> right) ;}}/*** (external) in-order traversal * @ param null * @ return null */public function MidOrder () {$ this-> mid_order ($ this-> root );} /*** check whether the node corresponding to $ key exists in the search tree * @ param $ key: number to be searched * @ return $ key corresponding node */function search ($ key) {$ current = $ this-> root; while ($ current! = NULL) {if ($ current-> key ==$ key) {return $ current;} elseif ($ current-> key> $ key) {$ current = $ current-> left;} else {$ current = $ current-> right;} return $ current ;} /*** search for the smallest keyword in the tree * @ param $ root node * @ return node corresponding to the smallest keyword */function search_min ($ root) {$ current = $ root; while ($ current-> left! = NULL) {$ current = $ current-> left;} return $ current ;} /*** find the maximum keyword in the tree * @ param $ root node * @ return the node corresponding to the maximum keyword */function search_max ($ root) {$ current = $ root; while ($ current-> right! = NULL) {$ current = $ current-> right;} return $ current ;} /*** find the direct precursor node of a $ key in the middle order * @ param $ x reference of the node to be searched for the precursor node * @ return */function predecessor ($ x) {// if ($ x-> left! = NULL) {return $ this-> search_max ($ x-> left) ;}// otherwise, find the parent node, until the current node is located on the right of the parent node $ p = $ x-> parent; // if x is the left child of p, it indicates that p is the successor of x, we need to find that p is the precursor of x while ($ p! = NULL & $ x = $ p-> left) {$ x = $ p; $ p = $ p-> parent;} return $ p ;} /*** find the direct successor node of a $ key in the middle order * @ param $ x reference of the node of the successor node to be searched * @ return reference of the successor node */function successor ($ x) {if ($ x-> left! = NULL) {return $ this-> search_min ($ x-> right);} $ p = $ x-> parent; while ($ p! = NULL & $ x ==$ p-> right) {$ x = $ p; $ p = $ p-> parent;} return $ p ;} /*** Insert $ key into the tree * @ param $ key number of the tree to be inserted * @ return null */function Insert ($ key) {if (! Is_null ($ this-> search ($ key) {throw new Exception ('node'. $ key. 'already exists and cannot be inserted! ') ;}$ Root = $ this-> root; $ inode = new Node ($ key); $ current = $ root; $ prenode = NULL; // find the proper insert position for $ inode while ($ current! = NULL) {$ prenode = $ current; if ($ current-> key> $ inode-> key) {$ current = $ current-> left ;} else {$ current = $ current-> right; }}$ inode-> parent = $ prenode; // if $ prenode = NULL, it is proved that the tree is an empty tree if ($ prenode = NULL) {$ this-> root = $ inode ;} else {if ($ inode-> key <$ prenode-> key) {$ prenode-> left = $ inode;} else {$ prenode-> right = $ inode ;}} // return $ root;}/*** delete the node corresponding to $ key in the tree * @ param $ key to be deleted Node number * @ return null */function Delete ($ key) {if (is_null ($ this-> search ($ key) {throw new Exception ('node '. $ key. "No. deletion failed! ") ;}$ Root = $ this-> root; $ dnode = $ this-> search ($ key ); if ($ dnode-> left = NULL | $ dnode-> right = NULL) {# if the node to be deleted has no subnode or only one subnode, then c = dnode $ c = $ dnode;} else {# if the node to be deleted has two subnodes, c sets it as the direct successor of dnode, replace the value of the node to be deleted with the subsequent value $ c = $ this-> successor ($ dnode);} // regardless of the preceding situation, at the end of c, only one subnode is left. if ($ c-> left! = NULL) {$ s = $ c-> left;} else {$ s = $ c-> right;} if ($ s! = NULL) {# set the parent node of the child node of c to the parent node of c. Here c may only have one child node, because if c has two child nodes, then c cannot be the direct successor of dnode $ s-> parent = $ c-> parent;} if ($ c-> parent = NULL) {# if the parent of c is empty, c = dnode is the root node. after the root node is deleted, the root node is directly set as the child node of the root node. here, dnode is the root node, with two subnodes, c is the successor node of the dnode, and the parent node of c will not be empty, so it will not enter this if $ this-> root = $ s ;} else if ($ c ==c c-> parent-> left) {# if c is the left and right sub-nodes of its parent node, set the left and right child nodes of the c parent node to the left and right child nodes of c $ c-> parent-> left = $ s ;} else {$ c-> parent-> right = $ s;} # if c! = Dnode, which indicates that c is the successor node of dnode. if ($ c! = $ Dnode) {$ dnode-> key = $ c-> key;} # return the root node/return $ root;}/*** (internal) obtain the depth of the tree * @ param $ root node * @ return tree depth */private function getdepth ($ root) {if ($ root = NULL) {return 0 ;} $ dl = $ this-> getdepth ($ root-> left); $ dr = $ this-> getdepth ($ root-> right); return ($ dl> $ dr? $ Dl: $ dr) + 1;}/*** (external) obtain the Depth of the tree * @ param null * @ return null */public function Depth () {return $ this-> getdepth ($ this-> root );}}

During debugging, you can call the middle-order traversal method. in my previous blog, we provided a binary tree graph implemented by PHP. with visual help, we can better help with debugging, for details, visit my previous blog: "Using PHP to display binary tree Images".

The above is the PHP binary tree (1): binary search tree content. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.