Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:53:58+00:00 2026-06-08T17:53:58+00:00

I made a simple program in C# to add a node in binary tree.

  • 0

I made a simple program in C# to add a node in binary tree.
I have an object field ‘Root’ to hold the main parent Node. such that every time I add a node I traverse from the tree by retrieving the data in Parent Node.

Here is my code

public class BTNode
    {
        public int Value { get; set; }        
        public BTNode Left { get; set; }
        public BTNode Right { get; set; }
        public BTNode Root { get; set; }
   }


public class BinaryTree
   {
       public  BinaryTree()
        {
            size = 0;
            Root = null; //To hold the main Parent Node
         }
        int size;
        object Root;

        public void AddNode(int value)
        {
            BTNode NewNode = new BTNode();
            if (Root != null)
            {
                NewNode = (BTNode)Root; //If tree exists, Get the Root Node
            }

            while (NewNode.Root != null)
            {
                if (value < NewNode.Value)
                {
                    NewNode.Root = NewNode.Left;
                }
                else if (value > NewNode.Value)
                {
                    NewNode.Root = NewNode.Right;
                }
            }

            size++;
            NewNode.Value = value;   //OBJECT 'ROOT' GETTING UPDATED AT THIS POINT
            NewNode.Root = NewNode;  //self pointer 
            NewNode.Left = null;
            NewNode.Right = null;

            if (size == 1)
            {
                Root = (object) NewNode;  //if this is the Parent Node(First Node) 
            }                             //then update the Root to be the parent Node
        }
    }

I want to hold only parent node of the Binary Tree in ‘Root’.. I only want to execute the last line when size =1 i.e if its the first node of the tree but no matter what I do the Root gets updated for every node. I am struggling to know why this is happening, Please help me. am I missing any concpept, logic here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:53:59+00:00Added an answer on June 8, 2026 at 5:53 pm
    1. Your Root property could be typed to BTNode. This way you wouldn’t need to cast it
    2. The line NewNode = (BTNode)Root; is getting the root node reference. Any change you make to NewNode will affect the root node. Are you aware of value types and reference types?
    3. I don’t understand why you’re going up (checking for the Root) and not down (checking for the Left or Right nodes).

    Check this solution, please. It uses a simple recursive method to place the new node:

    public class BinaryTree
        {
            public BinaryTree()
            {
                size = 0;
                Root = null; //To hold the main Parent Node
            }
            int size;
            BTNode Root;
    
            public void AddNode(int value)
            {
                size++;
                BTNode NewNode = new BTNode()
                {
                    Value = value
                };
    
                if (this.Root == null)
                {
                    this.Root = NewNode;
                    return;
                }
    
                this.PlaceNewNode(this.Root, NewNode);
            }
    
            public void PlaceNewNode(BTNode RootNode, BTNode NewNode)
            {
                if (NewNode.Value < RootNode.Value)
                {
                    if (RootNode.Left != null)
                    {
                        PlaceNewNode(RootNode.Left, NewNode);
                    }
                    else
                    {
                        RootNode.Left = NewNode;
                        return;
                    }
                }
                else
                {
                    if (RootNode.Right != null)
                    {
                        PlaceNewNode(RootNode.Right, NewNode);
                    }
                    else
                    {
                        RootNode.Right = NewNode;
                        return;
                    }
                }
            }
        }
    

    Hope this helps.

    Regards

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made my own file type (.ddd) and I made a simple program
I have just started on AWT and made a simple program in it, it
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,
I'm learning java and I have made simple program that simply reads value from
I have made a program in Flex for creating simple schedules, similar to MS
I have made a simple program to write text to existing files: ;; write
I have made a simple program that show directory listing in QTreeView using QFileSystemModel.
So I made this (very simple) program with a swing GUI with NetBeans, and
Hello I made a simple racket program on windows xp sp3 I wonder how
For an assignment I've made a simple C++ program that uses a superclass (Student)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.