我需要在 PHP 中实现“完美二叉树”。
目前,我有这个:
<?php
$teams = 8;
$num_rounds = round(log($teams, 2)) + 1;
for ($i = 0; $i < $num_rounds; ++$i)
{
$matches = $teams * pow(.5, $i - 1) / 2;
for ($j = 0; $j < $matches; ++$j)
{
echo "<div style=\"border-style: inset;\"><span>Round $i Match $j</span></div>\n";
}
}
?>
可以查看here .我正在使用 Frank Mich jQuery Binary Tree显示数据的插件,但正如我之前所说,我相信我需要一个二叉树才能正确显示它。
是否有更好的方法,或者我只是做错了?解决方案是什么?
最佳答案
这是在php中实现二叉树(数据结构)的代码:
<?php
class Node
{
public $data;
public $leftChild;
public $rightChild;
public function __construct($data)
{
$this->data = $data;
$this->leftChild = null;
$this->rightChild = null;
}
public function disp_data()
{
echo $this->data;
}
}
class BinaryTree
{
public $root;
public function __construct()
{
$this->root = null;
}
/**
* function to display the tree
*/
public function display()
{
$this->display_tree($this->root);
}
public function display_tree($local_root)
{
if ($local_root == null) {
return;
}
$this->display_tree($local_root->leftChild);
echo $local_root->data."<br/>";
$this->display_tree($local_root->rightChild);
}
/**
* function to insert a new node
*/
public function insert($key)
{
$newnode = new Node($key);
if ($this->root == null) {
$this->root = $newnode;
return;
} else {
$parent = $this->root;
$current = $this->root;
while (true) {
$parent = $current;
if ($key == ($this->find_order($key, $current->data))) {
$current = $current->leftChild;
if ($current == null) {
$parent->leftChild = $newnode;
return;
}//end if2
} else {
$current = $current->rightChild;
if ($current == null) {
$parent->rightChild = $newnode;
return;
}
}
}
}
}
/**
* function to search a particular Node
*/
public function find($key)
{
$current = $this->root;
while ($current->data != $key) {
if ($key == $this->find_order($key, $current->data)) {
$current = $current->leftChild;
} else {
$current = $current->rightChild;
}
if ($current == null) {
return (null);
}
}
return ($current->data);
}
public function delete1($key)
{
$current = $this->root;
$parent = $this->root;
$isLeftChild = true;
while ($current->data != $key) {
$parent = $current;
if ($key == ($this->find_order($key, $current->data))) {
$current = $current->leftChild;
$isLeftChild = true;
} else {
$current = $current->rightChild;
$isLeftChild = false;
}
if ($current == null) {
return (null);
}
}
echo "<br/><br/>Node to delete:".$current->data;
//to delete a leaf node
if ($current->leftChild == null && $current->rightChild == null) {
if ($current == $this->root) {
$this->root = null;
} else {
if ($isLeftChild == true) {
$parent->leftChild = null;
} else {
$parent->rightChild = null;
}
}
return ($current);
} else { //to delete a node having a leftChild
if ($current->rightChild == null) {
if ($current == $this->root) {
$this->root = $current->leftChild;
} else {
if ($isLeftChild == true) {
$parent->leftChild = $current->leftChild;
} else {
$parent->rightChild = $current->leftChild;
}
}
return ($current);
} else { //to delete a node having a rightChild
if ($current->leftChild == null) {
if ($current == $this->root) {
$this->root = $current->rightChild;
} else {
if ($isLeftChild == true) {
$parent->leftChild = $current->rightChild;
} else {
$parent->rightChild = $current->rightChild;
}
}
return ($current);
} else { //to delete a node having both childs
$successor = $this->get_successor($current);
if ($current == $this->root) {
$this->root = $successor;
} else {
if ($isLeftChild == true) {
$parent->leftChild = $successor;
} else {
$parent->rightChild = $successor;
}
}
$successor->leftChild = $current->leftChild;
return ($current);
}
}
}
}
/**
* Function to find the successor node
*/
public function get_successor($delNode)
{
$succParent = $delNode;
$successor = $delNode;
$temp = $delNode->rightChild;
while ($temp != null) {
$succParent = $successor;
$successor = $temp;
$temp = $temp->leftChild;
}
if ($successor != $delNode->rightChild) {
$succParent->leftChild = $successor->rightChild;
$successor->rightChild = $delNode->rightChild;
}
return ($successor);
}
/**
* function to find the order of two strings
*/
public function find_order($str1, $str2)
{
$str1 = strtolower($str1);
$str2 = strtolower($str2);
$i = 0;
$j = 0;
$p1 = $str1[$i];
$p2 = $str2[$j];
while (true) {
if (ord($p1) < ord($p2) || ($p1 == '' && $p2 == '')) {
return ($str1);
} else {
if (ord($p1) == ord($p2)) {
$p1 = $str1[++$i];
$p2 = $str2[++$j];
continue;
}
return ($str2);
}
}
}
public function is_empty()
{
return $this->root === null;
}
}
关于PHP 二叉树实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697761/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
MIMO技术的优缺点优点通过下面三个增益来总体概括:阵列增益。阵列增益是指由于接收机通过对接收信号的相干合并而活得的平均SNR的提高。在发射机不知道信道信息的情况下,MIMO系统可以获得的阵列增益与接收天线数成正比复用增益。在采用空间复用方案的MIMO系统中,可以获得复用增益,即信道容量成倍增加。信道容量的增加与min(Nt,Nr)成正比分集增益。在采用空间分集方案的MIMO系统中,可以获得分集增益,即可靠性性能的改善。分集增益用独立衰落支路数来描述,即分集指数。在使用了空时编码的MIMO系统中,由于接收天线或发射天线之间的间距较远,可认为它们各自的大尺度衰落是相互独立的,因此分布式MIMO
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
通常,数组被实现为内存块,集合被实现为HashMap,有序集合被实现为跳跃列表。在Ruby中也是如此吗?我正在尝试从性能和内存占用方面评估Ruby中不同容器的使用情况 最佳答案 数组是Ruby核心库的一部分。每个Ruby实现都有自己的数组实现。Ruby语言规范只规定了Ruby数组的行为,并没有规定任何特定的实现策略。它甚至没有指定任何会强制或至少建议特定实现策略的性能约束。然而,大多数Rubyist对数组的性能特征有一些期望,这会迫使不符合它们的实现变得默默无闻,因为实际上没有人会使用它:插入、前置或追加以及删除元素的最坏情况步骤复
在ruby中,你可以这样做:classThingpublicdeff1puts"f1"endprivatedeff2puts"f2"endpublicdeff3puts"f3"endprivatedeff4puts"f4"endend现在f1和f3是公共(public)的,f2和f4是私有(private)的。内部发生了什么,允许您调用一个类方法,然后更改方法定义?我怎样才能实现相同的功能(表面上是创建我自己的java之类的注释)例如...classThingfundeff1puts"hey"endnotfundeff2puts"hey"endendfun和notfun将更改以下函数定
我目前有一个reddit克隆类型的网站。我正在尝试根据我的用户之前喜欢的帖子推荐帖子。看起来K最近邻或k均值是执行此操作的最佳方法。我似乎无法理解如何实际实现它。我看过一些数学公式(例如k表示维基百科页面),但它们对我来说并没有真正意义。有人可以推荐一些伪代码,或者可以查看的地方,以便我更好地了解如何执行此操作吗? 最佳答案 K最近邻(又名KNN)是一种分类算法。基本上,您采用包含N个项目的训练组并对它们进行分类。如何对它们进行分类完全取决于您的数据,以及您认为该数据的重要分类特征是什么。在您的示例中,这可能是帖子类别、谁发布了该项
我一直在尝试在Ruby中实现BinaryTree类,但我得到了stackleveltoodeep错误,尽管我似乎没有在该特定代码段中使用任何递归:1.classBinaryTree2.includeEnumerable3.4.attr_accessor:value5.6.definitialize(value=nil)7.@value=value8.@left=BinaryTree.new#stackleveltoodeephere9.@right=BinaryTree.new#andhere10.end11.12.defempty?13.(self.value==nil)?true:
我查看了Stripedocumentationonerrors,但我仍然无法正确处理/重定向这些错误。基本上无论发生什么,我都希望他们返回到edit操作(通过edit_profile_path)并向他们显示一条消息(无论成功与否)。我在edit操作上有一个表单,它可以POST到update操作。使用有效的信用卡可以正常工作(费用在Stripe仪表板中)。我正在使用Stripe.js。classExtrasController5000,#amountincents:currency=>"usd",:card=>token,:description=>current_user.email)