PHP Two fork Tree (i): two fork search tree

Source: Internet
Author: User
On the principle of binary search tree on-line resources are quite many, and the situation is a little complicated, so here I will no longer state, directly on the code bar:

#bst. php Files <!--? php/** * Author:zhongjin * TIME:2016/10/20 11:53 * Description: Two fork Find tree *///node class node{public $key    ;    Public $parent;    Public $left;     Public $right;        Public function __construct ($key) {$this--->key = $key;        $this->parent = NULL;        $this->left = NULL;    $this->right = NULL;     }}//Two forks search tree class bst{public $root; /** * Initialize tree structure * @param an array of $arr initialization tree structure * @return NULL */Public function init ($arr) {$this-&gt        ; root = new Node ($arr [0]);        for ($i = 1; $i < count ($arr), $i + +) {$this->insert ($arr [$i]);     }}/** * (internal) middle order 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) middle order traversal * @param null * @return NULL  */Public Function Midorder () {$this->mid_order ($this->root);        }/** * Find the node in the tree for $key * @param $key number to search * @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;        /** * Find the Minimum keyword in the tree * @param $root root node * @return the node corresponding to the minimum keyword */function search_min ($root) {        $current = $root;        while ($current->left! = NULL) {$current = $current->left;    } return $current;        /** * Find the largest keyword in the tree * @param $root root node * @return The node for 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 sequence traversal * @param $x node reference for the precursor node to be found * @return precursor node reference */function predecessor ($x {//Left dial hand node exists, directly returns the right child node of the left child node if ($x->left! = NULL) {return $this->search_max ($x->left        );        }//Otherwise finds its parent node until the current node is on the right side of the parent node $p = $x->parent;            If X is the left child of P, stating 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 of a $key in the middle sequence traversal * @param $x node Reference for the successor to be found * @return subsequent node reference */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 trees to be inserted * @return NULL */function Insert ($key) {if (!is_null ($this->search ($key)) {throw new Exception (' node '. $key. ' already exists, cannot be inserted!        ');        } $root = $this->root;        $inode = new Node ($key);        $current = $root;        $prenode = NULL;            Find the appropriate insertion 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, the proof 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 $key corresponding node in the tree * @param $kEY number of nodes to delete * @return Null */function Delete ($key) {if (Is_null ($this->search ($key))) { throw new Exception (' node '. $key. "Does not exist, delete failed!"        ");        } $root = $this->root;        $dnode = $this->search ($key);        if ($dnode->left = = NULL | | $dnode->right = = null) {#如果待删除结点无子节点或只有一个子节点, then c = dnode $c = $dnode;        The else {#如果待删除结点有两个子节点, C is the direct successor of Dnode, and the value of the node to be deleted is subsequently replaced by the value of its successor $c = $this->successor ($dnode);        }//Regardless of the previous situation, to the last C only one side of the child node if ($c->left! = NULL) {$s = $c->left;        } else {$s = $c->right; if ($s! = NULL) {#将c的子节点的父母结点置为c的父母结点, here C can have only 1 child nodes, because C cannot be a direct successor to Dnode if C has two child nodes $s->paren        t = $c->parent; if ($c->parent = = NULL) {#如果c的父母为空, C=dnode is the root node, and the root node is immediately placed as a child node of the root node, where Dnode is the root node and has two child nodes.        C is the successor node of Dnode, C parents will not be empty, will not enter the IF $this->root = $s; } else if ($c == $c->parent->left) {#如果c是其父节点的左右子节点, the left and right child nodes of C parents are set to the left and right child nodes of c $c->parent->left = $s;        } else {$c->parent->right = $s;        } #如果c!=dnode, stating that C is the successor of Dnode, Exchange the key value of C and Dnode if ($c! = $dnode) {$dnode->key = $c->key;    } #返回根节点//return $root;        /** * (inward) Gets the depth of the tree * @param $root 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) Gets the depth of the tree * @param null * @return NULL */Public Function Depth () {return $this    ->getdepth ($this->root); }}

debugging when you can call the sequence traversal to do, I in the previous blog provides PHP implementation of the two-tree graphics, with visual help can better help us to debug, detailed everyone can visit my previous blog: "Using PHP to achieve two-tree graphics display"

The above is the PHP two Fork Tree (a): Two forks search tree content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.