我在一次采访中向我询问了这个问题:我有一个二叉树,我必须找到共同的祖先(父)给出该树的两个随机节点.我也得到了一个指向根节点的指针.
我的回答是:
分别遍历两个节点的树,直到到达预期的节点.遍历时并行存储元素和链接列表中的下一个地址.然后我们有两个链接列表.因此,尝试比较两个链表,两个链表中的最后一个公共节点是父列表.
我认为这个解决方案是正确的,如果我错了,请纠正我.如果这个解决方案是正确的,我可能知道这是这项任务的唯一更好的解决方案还是有比这更好的解决方案!
我需要从CLRS算法书中获得这个练习的提示:
证明无论我们在高度-h二进制搜索树中从哪个节点开始,对Tree-Successor的k次连续调用都需要O(k + h)时间.
我已实现以下代码以按级别顺序打印二叉搜索树.
public void printLevelOrder(int depth) {
for (int i = 1; i <= depth; i++) {
printLevel(root, i);
}
}
public void printLevel(BinaryNode<AnyType> t, int level) {
if (t == null) {
return;
}
if (level == 1) {
System.out.print(t.element);
} else if (level > 1) {
printLevel(t.left, level - 1);
printLevel(t.right, level - 1);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何改进我的代码,让它以某种格式打印出来.
举个例子,给一棵树
1
/ \
2 3
/ / \
4 5 6
Run Code Online (Sandbox Code Playgroud)
目前它打印如下:
123456
Run Code Online (Sandbox Code Playgroud)
我正在寻找它打印如下:
Level 0: 1
Level 1: 2 3 …Run Code Online (Sandbox Code Playgroud) 我正在学习左倾红黑树.
在本文提出的删除算法中,如果密钥匹配节点并且该子节点的右子树为NULL,则删除该节点.但是可能还有一个左子树也没有考虑.
我无法理解为什么左子树也是NULL.删除最小值或最大值时也会执行类似的操作.有人可以指导我吗?
如何打印到.txt file我已创建的空白?
我已经将结果打印到控制台,现在我想打印到一个名为的文件"Output.txt".我尝试了一些没有用的东西,但我认为创建一个printDictionary()专门用于打印到一个名为的文件的副本更容易printDictionaryToFile().虽然如此,我有点迷失方向.谁能纠正我的错误?我已经为文件添加了一个FILE名为*out输出的额外类型.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#define PUNC " \t\n\r,;.:!()[]{}?'\""
typedef struct node node;
typedef struct node {
char *word;
int count;
node *left;
node *right;
} node;
void insert(node ** dictionary, char * word) {
int result;
node * entry;
if (word == NULL || dictionary == NULL)
return;
if (*dictionary == NULL) {
entry= (node *) malloc(sizeof(node));
strcpy( entry->word= …Run Code Online (Sandbox Code Playgroud) 我想实现一个算法,将已排序的数组插入到二叉搜索树中,但我不希望最终得到一个只生长到一侧的树.
你有什么想法?
谢谢.
我试图了解BST以及如何迭代地插入元素.我的节点结构实现如下:
struct Node{
Node *left;
Node *right;
T data; //template class
};
Run Code Online (Sandbox Code Playgroud)
我的插入实现看起来像这样:
template<typename T>
bool BST<T>::Insert(const T value)
{
Node *newNode = new Node;
newNode -> data = value;
newNode -> left = NULL;
newNode -> right = NULL;
if(root == NULL) {root = newNode;} //If the BST is empty
else
{//The BST is not empty
Node *ptr = root; //points to the current Node
Node *ptr_parent; //points to the parent Node
while(ptr != NULL)
{
if((ptr -> data) …Run Code Online (Sandbox Code Playgroud) 我是Go的新手,已经实现了二叉搜索树.树可以存储任何值(特别是实现的任何值interface{}).
我想在此实现的基础上构建一个自平衡的红黑树.在面向对象的语言中,我定义了一个子类,BinarySearchTree它添加了一个color数据成员,然后重写该Insert方法来执行平衡操作.
问题:如何在Go中实现二进制搜索树和红黑树而不重复代码?
这是我的二叉搜索树实现:
package trees
import (
"github.com/modocache/cargo/comparators"
"reflect"
)
type BinarySearchTree struct {
Parent *BinarySearchTree
Left *BinarySearchTree
Right *BinarySearchTree
Value interface{} // Can hold any value
less comparators.Less // A comparator function to determine whether
// an inserted value is placed left or right
}
func NewBinarySearchTree(value interface{}, less comparators.Less) *BinarySearchTree {
return &BinarySearchTree{Value: value, less: less}
}
func (tree *BinarySearchTree) Insert(value interface{}) *BinarySearchTree {
if tree.less(value, tree.Value) { …Run Code Online (Sandbox Code Playgroud) 我已经读过std map是使用二叉搜索树数据结构实现的.
BST是一种顺序数据结构(类似于数组中的元素),它将元素存储在BST节点中并按顺序维护元素.例如,如果element小于node,则将其存储在节点的左侧,如果它大于node,则将其存储在节点的右侧.通过这种方法,我们实现了搜索,插入等各种操作的O(log n)复杂度.
但是,std map是一个关联容器.我们有一个键和值插入.它是否真的使用BST实现,如果是,如何实现?在BST,我们没有任何关键或价值.它是一种标准容器.
我有点困惑.请帮我澄清一下.它不影响我的工作,但我想更好地理解它们.谢谢你的帮助.
可以在O(n logn)时间内从列表构造堆,因为将元素插入堆需要O(logn)时间并且有n个元素.
类似地,可以在O(n logn)时间内从列表构造二叉搜索树,因为将元素插入到BST中需要平均登录时间并且存在n个元素.
从最小到最大遍历堆需要O(n logn)时间(因为我们必须弹出n个元素,并且每个pop需要O(logn)接收器操作).从最小到最大遍历BST需要O(n)时间(字面上只是顺序遍历).
所以,在我看来,构造两个结构需要相同的时间,但BST迭代的速度更快.那么,为什么我们使用"Heapsort"代替"BSTsort"呢?
编辑:感谢Tobias和lrlreon的回答!总之,以下是我们使用堆而不是BST进行排序的要点.