标签: binary-tree

如何轻松难忘地记住中序遍历和前序遍历?

二叉树遍历如前序遍历、中序遍历、后序遍历、层序遍历是很多IT公司经常面试的。

记得前序遍历和中序遍历的迭代实现让我很困惑。

以下是leetcode上的问题。

https://leetcode.com/problems/binary-tree-inorder-traversal/

https://leetcode.com/problems/binary-tree-preorder-traversal/

computer-science binary-tree recursive-datastructures data-structures

1
推荐指数
1
解决办法
1663
查看次数

错误:返回语句没有值,在返回“int”的函数中[-fpermissive]

我已经用 C++ 编写了二叉树遍历及其高度,但是在经过一些编码后进行编译时,我不断收到错误:返回语句没有值,在函数中返回“int”(-fpermissive)。

这是我的代码:

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int data;
    struct node *right;
    struct node *left;

    node(int val)
    {
        data = val;
        left = NULL;
        right = NULL;
    }
};

void Preorder(struct node *root)
{
    if (root == NULL)
    {
        return;
    }

    cout << root->data << " ";
    Preorder(root->left);
    Preorder(root->right);
}

void Postorder(struct node *root)
{
    if (root == NULL)
    {
        return;
    }

    cout << root->data << " ";
    Postorder(root->left);
    Postorder(root->right);
}

void Inorder(struct node *root) …
Run Code Online (Sandbox Code Playgroud)

c++ binary-tree data-structures

1
推荐指数
1
解决办法
1296
查看次数

如何删除具有 O(1) 额外内存的二叉树?

我想知道是否可以在不使用递归或堆栈的情况下删除具有 O(1) 额外内存的二叉树。

我已经成功编写了简单的递归后序遍历解决方案(使用堆栈内存):

void deleteTreeRec(Node *root)
{
   if (root == NULL) return;
   deleteTreeRec(root->left);
   deleteTreeRec(root->right);
   cout << "Deleting node " << root->data << endl;
   delete root;
}
Run Code Online (Sandbox Code Playgroud)

我听说这可以使用(中序)莫里斯遍历来实现,这似乎是错误的,或者至少是违反直觉的,因为据我所知,树删除需要以后序方式进行遍历(首先删除两个子树,然后才是根)。但是,我还没有找到解决方案的任何详细描述/伪代码,因此我在这里碰碰运气。

如果有人能够阐明这个问题,我们将不胜感激。谢谢!

c++ algorithm tree binary-tree

1
推荐指数
1
解决办法
213
查看次数

将N个项插入空二进制搜索树

为什么最坏的情况是将O项插入空二进制搜索树n ^ 2?没有余额检查.

big-o binary-tree

0
推荐指数
1
解决办法
4698
查看次数

我似乎无法删除C中二进制搜索树上最简单的情况

我去年发布了这个,因为一些大学项目,现在我必须再做一次(我从来没有完成我去年必须做的事情).我已经看过我以前的代码,你们所有人都回答了这些问题,但是,我似乎无法理解这一点.

我不打算把所有问题放在一篇长篇文章中,它只是让一切变得更加混乱,我需要一劳永逸地理解这一点.

我正在使用最简单的BST(只是元素的整数),我正在尝试从树中删除一个节点,它是最简单的演员,删除一个叶子.

我正在测试的树元素按以下顺序插入: 7 3 10 2 5 1 6 9 4 8

当然,顺序打印的输出是: 1 2 3 4 5 6 7 8 9 10

这是我的树结构:

typedef int TreeElement;

typedef struct sTree {
   TreeElement item;

   struct sTree *left;
   struct sTree *right;
} Tree;
Run Code Online (Sandbox Code Playgroud)

这是我的删除功能:

int delete(Tree **tree, TreeElement item) {
    if(!*tree) return 1;

    Tree *currPtr = *tree;
    Tree *prevPtr = NULL;

    while(currPtr) {
        if(item < currPtr->item) {
            prevPtr = currPtr;
            currPtr = currPtr->left;
        } else if(item > currPtr->item) {
            prevPtr …
Run Code Online (Sandbox Code Playgroud)

c binary-tree

0
推荐指数
1
解决办法
195
查看次数

二叉搜索树删除中的指针问题

我正在尝试实现二进制搜索树操作并在删除时陷入困境.

  11
 /  \
10  14 
Run Code Online (Sandbox Code Playgroud)

使用inorder遍历作为树的表示最初输出是10 11 14.

删除节点10,输出预期为11 14但我得到0 11 14.

删除节点14,输出预期只有11但我得到0 11 67837.

请解释我输出错误的原因.我不是在找任何代码:).

typedef struct _node{
  int data;
  struct _node *left;
  struct _node *right;
} Node;

Node* bstree_search(Node *root, int key)
{
  if(root == NULL){
    return root;
  }
  // Based on binary search relation, key can be found in either left,
  // right, or root.
  if(key > root->data)
    return bstree_search(root->right, key);
  else if(key < root->data)
    return bstree_search(root->left, key);
  else
    return root;
}
void bstree_insert(Node **adroot, int …
Run Code Online (Sandbox Code Playgroud)

c binary-tree pointers

0
推荐指数
1
解决办法
1402
查看次数

实现树迭代器

我创建了一个非常简单的节点类,其中包含名称和节点数组.我还创建了一个迭代器类,其下一个方法可以帮助我迭代每个节点和子节点.我需要编写下一个方法,但我不知道最好的方法是什么.

class Node

  def initialize(name, nodes
    @name = name
    @nodes = nodes
  end
end

class Iterator
  def initialize(node)
    @node = node
  end

  def next
    ???
  end
end
Run Code Online (Sandbox Code Playgroud)

例:

z = Node.new("z", [])
b = Node.new("b", [z])    
c = Node.new("c", [])
parent = Node.new("a", [b, c]) 

iterator = Iterator.new(parent)
str = ''
next = iterator.next
while next do
 str += next.name
 next = iterator.next
end
Run Code Online (Sandbox Code Playgroud)

str应该等于"abzc"

任何人都可以帮我吗?

ruby algorithm tree binary-tree traversal

0
推荐指数
1
解决办法
3317
查看次数

重构霍夫曼树进行解码

我使用霍夫曼压缩编码压缩字符串数据,即"需要更多资金"

编码

\n 0110
   1011
d  100
e  11
m  001
n  000
o  010
r  0111
y  1010
**
001010011111101100101000011101010110001111100111000110
Run Code Online (Sandbox Code Playgroud)

我想在java中重建Huffman树来解码编码.用于这种解码的任何实现或示例.

我尝试并编写了完美的解决方案.

public class HuffmanTree {

    public Node root;

    public HuffmanTree(){
        this.root = new Node();
    }

    public void add(char data, String sequence){

        Node temp = this.root;
        int i = 0;
        for(i=0;i<sequence.length()-1;i++){

          if(sequence.charAt(i)=='0'){
                if(temp.left == null){
                    temp.left = new Node();
                    temp = temp.left;
                }
                else{
                   temp = (Node) temp.left;
                }
            }
            else
              if(sequence.charAt(i)=='1'){
                if(temp.right == null){
                    temp.right = new Node(); …
Run Code Online (Sandbox Code Playgroud)

java algorithm binary-tree huffman-code

0
推荐指数
1
解决办法
1万
查看次数

在C中使用字符串插入二叉树

我的插入函数遇到了字符串的二叉树问题.我递归地这样做.有两个编译错误,我似乎无法摆脱它,它与人们的名字有关.我将发布我的代码和我的错误以及示例输出和我应该阅读的文件.我的教授给我们显示功能,使它看起来像一个特定的方式.我在这里先向您的帮助表示感谢.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

#define MAX_NAME_LEN 25
#define START_DEPTH 0

typedef struct TreeNode_ {
    char name[MAX_NAME_LEN];
    struct TreeNode_ *left;
    struct TreeNode_ *right;
}TreeNode;

TreeNode* read_from_file(const char* file);
TreeNode* insert(TreeNode* node, const char *name);
TreeNode* create_node(const char *name);

int main (int argc, char *argv[]) {
    /*
     * Check command line parameters
     * */
    if (argc < 2) {
            printf("%s is missing parameters to run properly\n", argv[0]);
            return 1;
    }
    TreeNode* root = NULL;
    root = read_from_file(argv[1]);

    display_tree(root,START_DEPTH);
}

TreeNode* read_from_file(const …
Run Code Online (Sandbox Code Playgroud)

c string malloc binary-tree compiler-errors

0
推荐指数
1
解决办法
1814
查看次数

C++:我的新节点在哪里?

我试图使用二叉树在集合中添加元素:

bool TreeSet::add(const string &str)
{
    if (treesize == 0)
    {
        TreeNode->data = str;
        treesize++;
        return true;
    }
    else
    {
        if (str < TreeNode->data)
            return insert(TreeNode->left, str);
        else if (str > TreeNode->data)
            return insert(TreeNode->right, str);
        else
            return false;
    }
    return false;
}

bool TreeSet::insert(TREE *node, const string &str) //private
{
    if (node == NULL)
    {
        node = new TREE;
        node->data=str;
        node->left = NULL;
        node->right = NULL;
        treesize++;
        return true;
    }
    else
    {
        if (str < node->data)
            return insert(node->left, str);
        else …
Run Code Online (Sandbox Code Playgroud)

c++ binary-tree set

0
推荐指数
1
解决办法
69
查看次数