标签: binary-tree

在C中实现Tree的指针问题

我正在为我的任务实现一个avl树.

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

struct TreeNode {
  char *item;
  struct TreeNode *left;
  struct TreeNode *right;
  signed char balance;
};

typedef struct TreeNode Node;

void _print_avl (Node *, int , const char *);

Node * get_new_node (char *);
int avl_insert(Node *, char *);
void print_avl (Node *);
void avl_swr(Node*);

int main (int argc, char *argv[])
{
  Node *root = get_new_node("thura");
  avl_insert(root, "thur2");
  print_avl(root);

  avl_insert(root, "thur1");

  return 0;
}

int avl_insert(Node *root, char *item)
{
  assert(root);

  if( …
Run Code Online (Sandbox Code Playgroud)

c algorithm tree binary-tree

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

在Haskell中正确旋转树:它是如何工作的?

我不知道haskell语法,但我知道一些FP概念(如代数数据类型,模式匹配,高阶函数等).

有人可以解释一下,这段代码意味着什么:

data Tree ? = Leaf ? | Fork ? (Tree ?) (Tree ?)

rotateR tree = case tree of
  Fork q (Fork p a b) c -> Fork p a (Fork q b c)
Run Code Online (Sandbox Code Playgroud)

据我所知,第一行就像Tree-type声明(但我完全不明白).第二行包括模式匹配(我不明白为什么我们需要在这里使用模式匹配).第三行为非haskell开发人员做了一些绝对不可读的事情.我已经找到了Fork的定义,fork (f,g) x = (f x, g x)但我不能再继续前进了.

syntax binary-tree haskell functional-programming

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

在 C# 中查找节点的深度

我有一个未排序对象的列表。这些对象代表一棵二叉树。

对象列表:

new List<Object> 
{
    new { Id = 3, Left = /, Right = / }
    new { Id = 5, Left = /, Right = / }
    new { Id = 4, Left = 2, Right = 5 }
    new { Id = 2, Left = 1, Right = 3 }
    new { Id = 1, Left = /, Right = / }
}
Run Code Online (Sandbox Code Playgroud)

二叉树:

      4
    /  \
   2    5
  / \
 1  3
Run Code Online (Sandbox Code Playgroud)

我需要一个算法来找到这些节点中的任何一个的深度。我知道的唯一算法是深度优先搜索。这意味着我必须将对象列表转换为树。考虑到 .NET 没有明确的树数据结构,您将如何解决这个问题?我是否必须将数据结构转换为树(我真的不想编写所有代码)。还有其他算法吗?

c# algorithm binary-tree

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

在二叉树中寻找最小共同祖先

可能重复:
如何在二叉树中找到两个节点的共同祖先?
二叉树的第一个共同祖先

我有一个二叉树如下.我需要找到最不常见的祖先(LCA).例如,6和4的LCA是1,4和5的LCA是2.

    1
   / \
  2   3
 / \ / \
4  5 6  7 
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我应该如何处理和解决这个问题?

java algorithm tree binary-tree

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

在c中将二叉树转换为数组

我想使用 C 将二叉树转换为数组。我尝试过但没有成功。

我的二叉树包含以下元素(预购)

4 3 5 10 8 7
Run Code Online (Sandbox Code Playgroud)

但我的数组包含(排序后)

4 4 5 7 8 10
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。我当前的代码如下所示:

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

typedef struct tree
{
    int data;
    struct tree *left;
    struct tree *right;
}tree;

int AddToArray(tree *node, int arr[], int i);
tree *CreateNode(int data);
tree *Insert(tree *node, int data);
void PrintPreorder(tree *node);
int count(tree *node);
int compare(const void * a, const void * b);

//---------------------------------------------------------------------------
int main()
{
    int i;
    int size;
    int *arr=NULL;
    tree *root=NULL;

    printf("***TEST …
Run Code Online (Sandbox Code Playgroud)

c arrays tree binary-tree preorder

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

C中的*和含义是什么?

我是树木概念的新手.我正在学习SerializationdeSerialization.我从链接获得了一个示例程序,复制并执行它.它跑了,但当我试图理解它时,我无法理解一条线 - void deSerialize(Node *&root, FILE *fp)

为什么*&意思?

整个代码是:

#include <stdio.h>
#define MARKER -1

/* A binary tree Node has key, pointer to left and right children */
struct Node
{
int key;
struct Node* left, *right;
};

/* Helper function that allocates a new Node with the
given key and NULL left and right pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL; …
Run Code Online (Sandbox Code Playgroud)

c c++ tree serialization binary-tree

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

使用队列的树的最大深度

在Narasimha Karumanchi所着的"数据结构和算法"一书中,这是用于找到树的最大深度的代码.

null由于某种原因,他提供了一个队列.我不懂为什么.删除它会破坏代码.

我想知道作者为什么要添加一个null,如果可以用这种方式解决问题,因为我们可以在不添加的情况下解决同样的问题null.

源代码:

public class MaxDepthInBinaryTreeWithLevelOrder {
// Returns the depth of this binary tree. The depth of a binary tree is the
// length of the longest path from this node to a leaf. The depth of a
// binary tree with no descendants (that is, just a leaf) is zero.
public int maxDepthLevelOrder(BinaryTreeNode root){
    if(root == null)
        return 0;
    int maxDepth = 1;
    Queue<BinaryTreeNode> q = new LinkedList<BinaryTreeNode>();
    q.offer(root);
    q.offer(null);   // …
Run Code Online (Sandbox Code Playgroud)

java algorithm tree binary-tree data-structures

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

C中二叉搜索树中的叶数

我是一个初学者,正在研究C二叉搜索树.我正在尝试一种方法,它将返回树中叶子的数量.叶子我的意思是一个没有孩子的节点(父节点)(左/右)Heres我的树结构:

struct Node {
    int value;
    struct Node *left;
    struct Node *right;
};

typedef struct Node TNode;
typedef struct Node *binary_tree;
Run Code Online (Sandbox Code Playgroud)

它是这样创建的:

binary_tree NewBinaryTree(int value_root) {
    binary_tree newRoot = malloc(sizeof(TNode));
    if (newRoot) {
        newRoot->value = value_root;
        newRoot->left = NULL;
        newRoot->right = NULL;
    }
    return newRoot;
}
Run Code Online (Sandbox Code Playgroud)

我添加元素如:

void Insert(binary_tree *tree, int val) {
    if (*tree == NULL) {
        *tree = (binary_tree)malloc(sizeof(TNode));
        (*tree)->value = val;
        (*tree)->left = NULL;
        (*tree)->right = NULL;
    } else {
        if (val < (*tree)->value) {
            Insert(&(*tree)->left, val); …
Run Code Online (Sandbox Code Playgroud)

c tree binary-tree binary-search-tree

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

在二叉树(Python)中查找指定节点的路径

我在计算从根到二叉树中指定节点的路径时遇到了麻烦(这特别是关于此问题的Python解决方案)。

这是一个例子。给定下面的二叉树,如果我指定值为4的节点,我想返回[1、2、4]。如果我指定值为5的节点,我想返回[1、2、5]。

       1
     /  \
   2      3
 /  \
4    5
Run Code Online (Sandbox Code Playgroud)

这是我尝试的解决方案。

class TreeNode:
 def __init__(self, x):
     self.val = x
     self.left = None
     self.right = None

def path(root, k, l=[]):
    if not root:
        return []
    if root.val == k:
        return l

    # Pre-order traversal: Visit root, then left, then right.
    l.append(root.val)
    path(root.left, k, l)
    path(root.right, k, l)
    return l
Run Code Online (Sandbox Code Playgroud)

现在如果我运行这个

>>> a = TreeNode(1)
>>> b = TreeNode(2)
>>> c = TreeNode(3)
>>> d = TreeNode(4)
>>> e = TreeNode(5) …
Run Code Online (Sandbox Code Playgroud)

python binary-tree tree-traversal depth-first-search

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

给出意外返回的Java方法

我工作的Java二叉搜索树,我试图使用这种方法findHelp()find().我期待一个返回值rt.getValue()(这是C2-112,因为我在返回之前打印出来),但是findHelp()在帮助方法中打印输出null.

我在网上找不到类似的错误,所以有人可以帮我搞清楚,或者给出类似问题的链接吗?

这是我的代码

private E findHelp(BinaryNode<Key, E> rt, Key k) {
        int compare = k.compareTo(rt.getKey()); 
        if (compare==0) {
            System.out.println(rt.getValue()); // I'm getting C2-112 here 
            return rt.getValue(); // so I expect a return of C2-112
        } else if (compare >0 ) {
            if (rt.getRight() == null) {
                return null;
            } else {
                findHelp(rt.getRight(), k);
            }
        } else {
            if (rt.getLeft() == null) {
                return null;
            } else {
                findHelp(rt.getLeft(), …
Run Code Online (Sandbox Code Playgroud)

java methods binary-tree return class

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