1、插入原则:第一个节点为根节点。如果后续插入的节点的值小于根节点,则成为根节点的左儿子。如果后续插入的节点的值大于根节点,则成为根节点的右儿子2.示意图3.二叉搜索树的实现rootNode=null;$this->size=0;}/***二叉树的根节点*@varNode*/protected$rootNode;/***获取根节点*@returnNode*/publicfunctiongetRootNode():Node{return$this->rootNode;}/***二叉树的数量*@varint*/protected$size;/***获取二叉树的元素个数*@returnint*/publicfunctiongetSize():int{return$this->size;}/***判断是否为空*@returnbool*/publicfunctionisEmpty():bool{return$this->size==0;}/***插入节点*@parammixed$value*@returnvoid*/publicfunctionadd($value):void{//如果没有根节点则插入根节点if(is_null($this->rootNode)){$this->rootNode=newNode($value);$这个->大小;返回;}else{$this->addChild($value,$this->rootNode);返回;}}/***插入子节点*@parammixed$value*@paramNode|null$parentNode*/privatefunctionaddChild($value,?Node$parentNode):void{if(bccomp($parentNode->getValue(),$value)>0){//左子节点if(is_null($parentNode->getLeftChild())){$parentNode->setLeftChild(newNode($value));$这个->尺寸++;返回;}else{$this->addChild($value,$parentNode->getLeftChild());}}elseif(bccomp($parentNode->getValue(),$value)<0){//右孩子if(is_null($parentNode->getRightChild())){$parentNode->setRightChild(新节点($value));$这个->尺寸++;返回;}else{$this->addChild($value,$parentNode->getRightChild());}}else{返回;}}}4。demoadd(10);$tree->add(5);$tree->add(15);$tree->add(1);$tree->add(9);
