To add a new node to the tree, we need a recursive process to locate the appropriate location of the tree. The process starts from the root node. If the tree is empty, the new node will become the root node. If the tree is not empty, the new node will be checked against the parent node. If it is less than the parent node, the process will look for the proper location in the left side, otherwise, it looks in the right side. If the parent node is equal to the new node, it will do nothing. This process continues recursively until it finds the proper location.
///C# code to insert a node to the tree
public TreeNode insert(TreeNode Tree, int Tar)
//The place to insert the node
TreeNode item= new TreeNode(Tar);
//insert to the left
//insert to the right
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: