(5)Root (3)-------^--------(7) (2)---^----(5) ^-----(8)
I want to add node with data 5 to this binary search tree. Please, help.
Note. This answer assumes you are talking about a binary search tree.
You cross the binary tree from the root:
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).
(5)
(3)
, :
, , :
(5) (5)----^ (3)-------^--------(7) (2)---^----(5) ^-----(8)
, , . . .
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; } } }
/// <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; } }
Source: https://habr.com/ru/post/1742680/More articles:Should non-english names be replaced with english? - .netHow can I check if a class contains a specific attribute? - pythonЕсть ли способ заставить CoreImage использовать графический процессор? - objective-cXMLHttpRequest open () failed - internet-explorer-8Learning Objective-C. Using Xcode 3.2.1. What is the error: Program signal: "EXC_ARITHMETIC" - programming-languages | fooobar.comHow to start a service that is defined in another package? - androidhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1742683/ways-to-store-data-in-net&usg=ALkJrhhgBccRi7yCU42bdy44nvP9SM7B1AAn easy way to print the register value in x86 assembly - assemblyHow to get a .so file from a .la file? - gccHow can I change the default Android language for the soft keyboard? - androidAll Articles