插入二叉树的元素 [英] Inserting an element in Binary Tree

查看:185
本文介绍了插入二叉树的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试过探索了很多过网,但可以得到任何帮助,
到处都是它像添加节点到二叉搜索树。

问题:请求算法和code片段添加一个节点在二叉树。 (或指向我正确的URL)

假设:
按我的理解,二叉树二叉搜索树有什么不同?纠正我,如果我错了。

(要求:如果您在写code片段,请使用正确的变量名,在理解帮助)

例如:二叉树

5 7 3 X1 X2 X3

  5          7 3   X1 X2 X3

二叉搜索树5 7 3 2 4 6

  5
          3 7   2 4 6插入(INT键,结构节点**根)
{
    如果(NULL == *根)`
    {
        *根=(结构节点*)malloc的(的sizeof(结构节点));`
        (*根) - GT;数据=键;
        (*根) - GT;左= NULL;
        (*根) - GT;右= NULL;
    }
    否则,如果(关键≤(*根) - GT;数据)
    {
        插入(键,及(*根) - GT;左);
    }
    否则,如果(钥匙>(*根) - GT;数据)
    {
        插入(键,及(*根) - GT;右);
    }
}


解决方案

二叉树和二叉搜索树之间的区别是,虽然他们都有每个节点最多只能有2子节点,一个二叉搜索树限制(BST)也必须有其左子等于或更低的值和其右孩子必须大于或等于价值。这就是为什么它被称为搜索树,因为一切都在有序的数字,它具有运行时间用于搜索O(LOGN)。

因此​​,code为二叉树应该比code你已经找到了一个二叉搜索树很相似,其实不复杂。这将是pretty大致相同,除了你不必担心携带较小的,更大的数字,分别为左,右的孩子。所以,你可以删除一些关于价值的约束条件句的BST code转换为BT code。

 插入(INT键,结构节点**根)
{
    如果(*根== NULL)
    {
        *根=(结构节点*)malloc的(的sizeof(结构节点));
        (*根) - GT;数据=键;
        (*根) - GT;左= NULL;
        (*根) - GT;右= NULL;
    }
    否则,如果((*根) - GT;左== NULL)
    {
        插入(键,及(*根) - GT;左);
    }
    其他
    {
        插入(键,及(*根) - GT;右);
    }
}

Tried exploring a lot over the net, but could get any help, Everywhere its like adding a node to the Binary Search tree.

Question: Requesting for algorithm and code snippet for adding a node to the Binary tree. ( or point me to correct URL )

Assumption: As per my understanding, Binary Tree and Binary Search Tree is different? Correct me if I am wrong.

( request: if you are writing your code snippet please use proper variable name, that helps in understanding )

Eg: Binary Tree

5 7 3 x1 x2 x3

                 5

          7               3

   x1       x2       x3       

Binary Search Tree 5 7 3 2 4 6

                   5
          3               7

   2          4       6       





insert(int key, struct node **root)
{
    if( NULL == *root )`
    {
        *root = (struct node*) malloc( sizeof( struct node ) );`
        (*root)->data = key;
        (*root)->left = NULL;    
        (*root)->right = NULL;  
    }
    else if(key < (*root)->data)
    {
        insert( key, &(*root)->left );
    }
    else if(key > (*root)->data)
    {
        insert( key, &(*root)->right );
    }
}

解决方案

The difference between a Binary Tree and a Binary Search Tree is that though they both have restrictions that each node can have at most 2 child nodes, a Binary Search Tree (BST) also must have its left child be of equal or lesser value and the its right child must be of greater or equal value. This is why it is called a "Search" tree because everything is ordered numerically and it has an O(logn) run time for searching.

Therefore, the code for a Binary Tree should be quite similar and actually less complicated than the code you have found for a Binary Search Tree. It will be pretty much the same except you don't have to worry about the left and right children carrying smaller and bigger numbers, respectively. So you can remove some of the conditionals regarding value constraints to convert the BST code to BT code.

insert(int key, struct node **root)
{
    if( *root == NULL )
    {
        *root = (struct node*) malloc( sizeof( struct node ) );
        (*root)->data = key;
        (*root)->left = NULL;    
        (*root)->right = NULL;  
    }
    else if((*root)->left == NULL)
    {
        insert( key, &(*root)->left);
    }
    else
    {
        insert( key, &(*root)->right);
    }
}

这篇关于插入二叉树的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆