这是我的代码:
template <typename DataType> bool SearchValue(TreeNode<DataType> *root, DataType search_value)
{
if(search_value != root->data)
{
if(root->right != NULL)
{
return SearchValue(root->right, search_value);
}
if (root->left != NULL)
{
return SearchValue(root->left, search_value);
}
return false;
}
else
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我无法使SearchValue功能正常工作.条件不是改变SearchValue功能的签名.问题如下:例如,我们尝试找到数据字段等于"90"的元素,它存在于树中.有时这段代码会找到这个元素,有时候不会 - 取决于它在树中的位置.问题是如何使其每次都正常工作.
我以这种方式构建树:
template <typename DataType> TreeNode<DataType> *BuildTree()
{
TreeNode<DataType> *root = new TreeNode<DataType>(10, new TreeNode<DataType>(20), new TreeNode<DataType>(30));
TreeNode<DataType> *curRoot = root;
curRoot = curRoot->left;
curRoot->left = new TreeNode<DataType>(40);
curRoot->left->left = new TreeNode<DataType>(70);
curRoot->right = …Run Code Online (Sandbox Code Playgroud) 我已经编写了下面的代码来递归搜索二叉树.即使我的system.out语句被执行,return语句也没有返回整个递归,因此这个方法没有返回true.
任何人都可以建议我如何退出整个递归.?
public static boolean isElementinTree(int num, BinaryTreeNode root)
{
if (root != null)
{
int rootVal = root.getData();
BinaryTreeNode left = root.getLeft();
BinaryTreeNode right = root.getRight();
if (left != null)
{
isElementinTree(num,left);
}
if (right != null)
{
isElementinTree(num,right);
}
if (num == rootVal)
{
System.out.println("------ MATCH -----");
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 这个问题可能已经被很多人提出过,但是,它有点不同.我们有一棵二叉树.而且你有两个节点p&q.我们必须找到最不常见的父母.但是你没有指向根的根节点指针.您将获得两个内置功能:
1)BOOL same(node *p, node *q);- >如果节点相同则返回true,否则返回false.
2)node* parentNode(node *c);- >返回一个节点,该节点是当前节点的父节点.
如果节点c实际上是root,那么parentNode函数将返回一个NULL值.使用我们必须的函数来查找树的最不常见的父代.
我在计算二叉树中的节点时遇到问题.这是一个真正的简单树,如下图所示.
(5)
(3)-------^-------(7)
(2)---^---(6) ^-------(9)
(5)---^---(8)
Run Code Online (Sandbox Code Playgroud)
我添加了8个节点,因此应该有8个.但是当我运行我的代码时,它会计算7个节点.我认为它只计算所有左节点和右节点而不计算根,但我将节点数设置为1以计算根,然后计算左右节点.请参阅下面的代码
private int getNumNodes(Node<E> root){
numNodes = 1; // starts by counting the root
// counts the left nodes
if(root.left != null){
numNodes += getNumNodes(root.getLeft());
}
// counts the right nodes
if(root.right != null){
numNodes += getNumNodes(root.getRight());
}
return numNodes;
}
public int getNumNodes(){
return root == null ? 0 : getNumNodes(root);
}
Run Code Online (Sandbox Code Playgroud)
它必须在某个地方错过一个计数,但我不确定它在哪里发生.你能帮帮我吗?谢谢.
int postOrder(struct node* root, char organ[], char bt[], int* val){
if(root != NULL) {
postOrder(root->left, organ, bt, val);
postOrder(root->right, organ, bt, val);
if(strcmp(root->organ.organname, organ) == 0){
if(strcmp(root->organ.bloodtype, bt) == 0){
if(val == 0){
printf("%s\n", root->organ.name);
val = 1;
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正试图在第一次打印后立即终止此递归功能.我的想法最初是传入十进制指针'val'并在我想终止函数时将其设置为1.思考过程是它会改变函数之外的值,所以以前的所有调用都会有更新的指针,但我不认为它在这个设置中是如何工作的.
此函数通过二叉树搜索Post Orderly.
所以,我正在尝试用C创建这个二进制树程序,它只是不断抛出最糟糕的编译错误.每当我更改某些内容以找到导致错误的内容时,它就会发生变化!我没有忘记我的包含守卫,我曾经使用过#pragma.如果我将结构放在标题中,我会得到不兼容的类型,如果我把它放在.c中,我会重新定义pretty_print,当我重命名pretty_print时,它会消失.当我修剪bintree.c中的其他函数和标题时,错误更改为intTree未定义.如果我然后将intTree更改为int,则它变为不兼容的类型.如果我将标头合并到.c中,这就消失了.现在它已经消失了.我认为这些可能是单独的错误,而且变化的错误可能是我的坏事,但它正在发生,我不知道为什么.我将展示代码,演示尽可能多的这些,但每当我尝试复制我所做的时,它会抛出不同的错误.但我并不沮丧.我希望能够发布一个格式正确的问题和有用的信息,但我没有,尽管在这个程序上花了几个小时.
bintree.cxx
#include "bintree.h"
int add(intTree* root,int key,void* val){
if(!root){
root=malloc(sizeof(intTree));
if(!root){
return 1;
}
root->key=key;
root->val=val;
root->left=0;
root->right=0;
return 0;
}
if(key>root->key){
if(root->right){
return add(root->right,key,val);
}else{
root->right=(intTree*) malloc(sizeof(intTree));
if(!root->right){
return 1;
}
root->right->key=key;
root->right->val=val;
root->right->left=0;
root->right->right=0;
return 0;
}
}
if(key<root->key){
if(root->left){
return add(root->left,key,val);
}else{
root->left=malloc(sizeof(intTree));
if(!root->left){
return 1;
}
root->left->key=key;
root->left->val=val;
root->left->left=0;
root->left->right=0;
return 0;
}
}
return 2;
}
void* get(intTree* root,int key){
if(!root){
return 0;
}
if(key>root->key){
return get(root->right,key);
}
if(key<root->key){
return get(root->left,key);
}
return root->val; …Run Code Online (Sandbox Code Playgroud) 我已经看到很多搜索算法要在二进制排序树中搜索,但它们都使用相同的方式:递归.我知道递归是昂贵相比,循环,因为每次我们调用了搜索功能,对于那些方法,它最终会使用大量的内存,如果二叉搜索树太大创建一个新的堆栈帧.
为什么我们不能像这样搜索二叉搜索树:
while (root!=NULL)
{
if (root->data==data)
return 0;
else if (data > root->data)
root=root->right;
else
root=root->left;
}
Run Code Online (Sandbox Code Playgroud)
我认为这种方式比递归方式更快更有效,如果我错了,请纠正我!
到目前为止我有这个代码:
data BinaryTree a = Null | Node a (BinaryTree a) (BinaryTree a)
treeLeaves :: BinaryTree a -> [a]
treeLeaves tree = case tree of
Null -> []
Node v t1 t2 -> [] ++ treeLeaves t1 ++ treeLeaves t2
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么.它输出一个空列表.
好的,所以我的二进制树程序有一个打印功能,可以在Breadth-First中打印所有内容.当我调用它时,它会按照我期望的顺序打印,但是它也会删除除节点之外的所有节点,并留下一个空树.
void BST::breadth(Node *& cur_root)
{
if (cur_root != NULL) {
cout << cur_root->m_value;
if (cur_root->m_left != NULL) {
myqueue.push(cur_root->m_left);
}
if (cur_root->m_right != NULL) {
myqueue.push(cur_root->m_right);
}
if (!myqueue.empty()) {
cout << ", ";
cur_root = myqueue.front();
myqueue.pop();
breadth(cur_root);
} else {
cout << "}" << endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我假设从myqueue弹出节点可能是问题,但是当我使用普通打印功能(按顺序遍历)时我没有这个问题.
void BST::print(Node *& cur_root)
{
if (cur_root != NULL) {
print(cur_root->m_left);
myqueue.push(cur_root);
print(cur_root->m_right);
}
int sizecompare = myqueue.size();
if (size() == sizecompare) {
while (!myqueue.empty()) {
cout << …Run Code Online (Sandbox Code Playgroud) 我以前编写过C代码,用于在二进制搜索树中插入和遍历整数值。我试图使它也适用于字符串。我进行了一些更改,例如将所有整数转换为字符串,还添加了诸如strcpy()和strcmp()之类的函数来处理字符串操作。但是代码似乎不起作用。有人可以向我解释哪里出了问题吗?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char *str;
struct node *left;
struct node *right;
};
struct node *root=NULL;
void preorder(struct node *temp)
{
if(temp==NULL)
return;
printf("%s ", temp->str);
preorder(temp->left);
preorder(temp->right);
}
void inorder(struct node *temp)
{
if(temp==NULL)
return;
inorder(temp->left);
printf("%s ", temp->str);
inorder(temp->right);
}
void postorder(struct node *temp)
{
if(temp==NULL)
return;
postorder(temp->left);
postorder(temp->right);
printf("%s ", temp->str);
}
struct node* create(char *str) // Function to create new node
{
struct node *new1;
new1=(struct node*)malloc(strlen(str)+10);
strcpy(new1->str,str);
new1->left=NULL;
new1->right=NULL;
return new1;
} …Run Code Online (Sandbox Code Playgroud)