标签: binary-tree

Swift 中具有结构的二叉树

我试图在struct下面给出的帮助下制作一棵二叉树:

struct BinaryTree {
    var value: Int
    var left: BinaryTree
    var right: BinaryTree 
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误error: value type 'BinaryTree' cannot have a stored property that recursively contains it。这里的结构是值类型,所以我无法在其中创建相同的结构对象。

我怎样才能实现这个目标???

tree binary-tree data-structures swift

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

用 C 打印简单的二叉搜索树

我只是用 C 实现简单的二叉搜索树。

struct node_struct {
    int data;
    struct node_struct *right, *left;
};

typedef struct node_struct Node;
Run Code Online (Sandbox Code Playgroud)

具有已经可以查找的插入、删除和搜索功能。

但我还需要实现打印功能,以这种方式打印树

6
|-2
  |-1
  |-4
|-9
Run Code Online (Sandbox Code Playgroud)

从上面看,节点 6 左侧有 2 个,右侧有 9 个,节点 2 左侧有 1 个,右侧有 4 个。

所以我想问一下这个打印功能如何实现。

c printing binary-tree binary-search-tree

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

如何不重复打印树的代码

我有一个从左到右打印树节点的函数。

void PrintTree()
{
...
Print(curentNode);
...
}
Run Code Online (Sandbox Code Playgroud)

但是现在我想添加一个函数来打印满足某些条件的节点。例如,只打印这样的节点,其中的字符串以给定的字符串开头。所以它看起来像

void PrintTreeByCondition(string a)
{
...
if(IsPrefix(a,curentNode->stringVar))
      Print(curentNode);
...
}
Run Code Online (Sandbox Code Playgroud)

但是后来我有两个具有相同代码的函数,不同之处在于一行。我将如何避免代码重复?

UPD:遍历代码:

void BinTree::TraverseTree()
{
    std::stack<TreeNode*> s;
    s.push(root);
    TreeNode* curentNode = s.top();
    while (curentNode != nullptr|| s.empty() == false)
    {
        while (curentNode != nullptr)
        {
            s.push(curentNode);
            curentNode = curentNode->GetLeft();
        }

        curentNode = s.top();
        s.pop();

        // do stuff

        curentNode = curentNode->GetRight();
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ binary-tree code-duplication

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

访问二叉树左节点的指针时出现 SIGSEGV,即使该指针已初始化

我正在尝试创建一个返回二叉树的镜像副本的函数。我所说的“镜像”是指一棵树,每个左节点作为其右节点,反之亦然。

我进行此练习的页面中的视觉示例。

The one on the left gets copied to resemble the one on the right. This is the code of the function, with the definition of the binary nodes and "insert node" function that I use:

typedef struct bNode {
    int data;
    struct bNode *left;
    struct bNode *right;
} bNode;
    
//  =============================================================
    
bNode* reverse_tree (bNode **tree) {
    bNode *copy = malloc(sizeof(bNode));
    copy->data = (*tree)->data;
    if (!((*tree)->right) && !((*tree)->left)){
        return copy;
    }
        
    copy->left = reverse_tree(&(*tree)->right);
    copy->right = reverse_tree(&(*tree)->left);
    return copy;
} …
Run Code Online (Sandbox Code Playgroud)

c recursion reverse binary-tree function-definition

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

Java算法,用于在二叉树中查找最大的独立节点集

通过独立节点,我的意思是返回的集合不能包含直接关系的节点,不能同时包含父节点和子节点.我试图使用谷歌,但没有成功.我认为我没有正确的搜索词.

一个链接,任何帮助将非常感谢.刚刚开始这个.

我需要返回实际的独立节点集,而不仅仅是金额.

java algorithm binary-tree

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

计划中的二叉树

考虑以下BNF定义数字树.请注意,树可以是叶子,具有一个子树的节点1,或者具有两个子树的节点2.

tree ::= (’leaf number)
| (’node-1 tree)
| (’node-2 tree tree)
Run Code Online (Sandbox Code Playgroud)

一个.为这些树上的递归过程编写模板.

湾 定义返回t中叶子数的过程(叶子数t)

> (leaf-count ’(leaf 5))

1

> (leaf-count ’(node-2 (leaf 25) (leaf 17)))

2

> (leaf-count ’(node-1
(node-2 (leaf 4)
(node-2 (leaf 2) (leaf 3)))))

3
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所拥有的:

;define what a leaf, node-1, and node-2 is
(define leaf list)
(define node-1 list)
(define node-2 list)

;procedure to decide if a list is a leaf or a node
(define (leaf? tree) (number? (car tree)))
(define (node? tree) (pair? …
Run Code Online (Sandbox Code Playgroud)

scheme binary-tree racket

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

在预排序的数组中查找给定值的索引

今天,我去接受采访,面试官问我如何在预先排序的数组中找到给定值(数字)的索引,如下所示:

$preSortedArr=array(23,32,36,41,45,54);
Run Code Online (Sandbox Code Playgroud)

他还说不允许使用递归.

我认为该函数应如下所示:

function findIndexByValue($preSortedArray,$value){            
//some codes here       
}
Run Code Online (Sandbox Code Playgroud)

你认为他对我的期待是什么解决方案?

编辑:对不起,我忘了添加他最初让我写伪代码,但我说我不知道​​.然后我尝试用PHP编写它,但我认为他期待一种独立于语言的解决方案.

php arrays algorithm binary-tree

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

在java问题中将节点插入二叉树中

即时通讯从c ++到java,我在二进制树上与java混淆.让Node类成为内部静态类的唯一方法是什么?我看到的所有例子都是这样做的.但是,我这样做的方式是我有一个节点类,而binarytree类使用这个节点类.但是当我在第二次插入后尝试插入树时,我一直收到错误.我在这条线上得到了例外if(dataIn <= nodeIn.getLeft().getData()){

我很困惑我做错了什么....这是我的插入代码我有.提前致谢..

public void insert(int dataIn){
    root = insert(root, dataIn);
}

private Node insert(Node nodeIn, int dataIn){
    if(nodeIn==null){
        nodeIn = new Node(null, null, dataIn);
    }else{
        if(dataIn <= nodeIn.getLeft().getData()){
            nodeIn.setLeft(insert(nodeIn.getLeft(), dataIn));
        }else{
            nodeIn.setRight(insert(nodeIn.getRight(), dataIn));
        }
    }

    return nodeIn;
}
Run Code Online (Sandbox Code Playgroud)

java binary-tree

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

在Java中实现BinaryTree

我有这个代码用于BinaryTree创建和遍历


class Node
{
    Integer data;
    Node left;
    Node right;
    Node()
    {
        data = null;
        left = null;
        right = null;
    }
}
class BinaryTree
{
    Node head;
    Scanner input = new Scanner(System.in);
    BinaryTree()
    {
        head = null;
    }
    public void createNode(Node temp, Integer value) 
    {
        Node newnode= new Node();
        value = getData();
        newnode.data = value;
        temp = newnode;
        if(head==null)
        {
            head = temp;
        }
        System.out.println("If left child exits for ("+value+") enter y else n");
        if(input.next().charAt(0)=='y')
        {
            createNode(temp.left, value);
        } …
Run Code Online (Sandbox Code Playgroud)

java algorithm binary-tree

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

二叉树实现C++

我一直在尝试用C++实现二进制搜索树以获得乐趣.我的问题是我的插入功能有问题.以下是我到目前为止:

class TreeNode{

public: 
    int data;          
    TreeNode *left;    
    TreeNode *right;  
    void Insert(int value, TreeNode *x);
    void TreeNode::Print(TreeNode *x);
    TreeNode();
};


TreeNode::TreeNode(){

left = NULL;
right = NULL;

}
Run Code Online (Sandbox Code Playgroud)

.

void TreeNode::Insert(int value, TreeNode *x){

    if(x->left == NULL && x->right == NULL){
           TreeNode *tree = new TreeNode();                        
           tree->datavalue;                               
           x->left = tree;                                  
    }

    else if(x->left == NULL && x->right != NULL){

           TreeNode *tree = new TreeNode();                
           tree->data = value;                          
           x->left = tree;                                 
    }

    else if(x->left != NULL && x->right == NULL){

           TreeNode …
Run Code Online (Sandbox Code Playgroud)

c++ tree binary-tree function insert

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