How to add add node to binary tree

                   (5)Root
          (3)-------^--------(7)
     (2)---^----(5)           ^-----(8)

I want to add node with data 5 to this binary search tree. Please, help.

+3
source share
4 answers

Note. This answer assumes you are talking about a binary search tree.

You cross the binary tree from the root:

  • if your new element is less than or equal to the current node, you go to the left subtree, otherwise to the right subtree and continue traversing
  • if you have reached a node where you cannot go deep because there is no subtree, this is the place to insert your new element

                   (5)Root
          (3)-------^--------(7)
     (2)---^----(5)           ^-----(8)
            (5)--^
    

(5), ( 5 & lt; = 5) (3), ( 5> 3) (5), ( 5 & lt; = 5), , , (5).

+4

, :

, , :

                         (5)
                   (5)----^
          (3)-------^--------(7)
     (2)---^----(5)           ^-----(8)

, , . . .

, , . . .

+2
private void Insert(Node node, ref Node tree)
{    
        if (tree == null)                          // Found a leaf?    
        {                                      
             tree = node;                          // Found it! Add the new node as the new leaf.
        }    
        else    
        {        
             int val = string.Compare(node.Key, tree.Key);        // already inserted        
             if (val == 0)
             {            
                  throw new InvalidOperationException("Duplicate key");
             }        
             elseif (val < 0)        
             {            
                  Node left = tree.Left;            
                  Insert(node, ref left);              // Keep moving down the left side.            
                  tree.Left = left;        
             }        
             else        
             {            
                  Node right = tree.Right;         
                  Insert(node, ref right);            // Keep moving down the right side.            
                  tree.Right = right;        
             }    
      }
}
+1
source
/// <summary>
    /// Construct the tree from a pre order traversal
    /// </summary>
    /// <param name="preorderTraversal"></param>
    /// <returns></returns>
    public static TreeNode ConstructTreeFromPreOrderTraversal(int[] preorderTraversal)
    {

        if (null == preorderTraversal || preorderTraversal.Length < 1)
            return null;
        TreeNode root = null;
        int len = preorderTraversal.Length;
        for (int i = 0; i < len; i++)
        {
            TreeNode newNode = new TreeNode();
            newNode.Data = preorderTraversal[i];
            newNode.Left = newNode.Right = null;
            AddNode(ref root, newNode);
        }
        return root;
    }

    /// <summary>
    /// add not in the tree
    /// </summary>
    /// <param name="root"></param>
    /// <param name="newNode"></param>
    private static void AddNode(ref TreeNode root, TreeNode newNode)
    {
        if (root == null)
            root = newNode;
        else if (newNode.Data < root.Data)
        {
            TreeNode left = root.Left;
            AddNode(ref left, newNode);
            root.Left = left;
        }
        else
        {
            TreeNode right = root.Right;
            AddNode(ref right, newNode);
            root.Right = right;
        }
    }
+1
source

Source: https://habr.com/ru/post/1742680/


All Articles