Is there any algorithm to insert,delete in a binary tree? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is there any algorithm to insert,delete in a binary tree?

I know about BST ,but I just want to know whether we can insert ,delete in Binary tree

9th Aug 2019, 3:00 PM
abishek sriram
abishek sriram - avatar
10 Answers
+ 11
abishek sriram Insertion begins by examining the root, left, right elements of the subtree, We examine the root and recursively insert the new node to the left subtree if its key is less than that of the root, or the right subtree if its key is greater than the root. If the key exists, we can either replace the value by the new value or just return without doing anything. Deletion is more complex as we need to delete the value by key so for that first we have to find node by given key and then removal of the node is possible. 1) if the node which we want to delete is in leaf then we can simply remove them. 2) Deleting a node with one child: remove the node and replace it with its child. 3) Deleting a node with two children: we first find its in-order successor (left-most node in its right sub-tree), let's say A. Then copy A's key and value to the node, and remove A from its right sub-tree. These way this two operation are performed on the binary search tree.
9th Aug 2019, 3:49 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 2
abishek sriram "I'm looking for cpp" If you look for and wanted to focus on answers implementable in C++ then you need to add 'C++' in Relevant Tags of your question. That way the focus of the question is clear. Good luck! 👍
9th Aug 2019, 3:32 PM
Ipang
9th Aug 2019, 3:41 PM
Michael
Michael - avatar
+ 1
What do you exactly mean? Is it the mathematical explanation of Heron‘s method for cube roots or are you looking for methods in a specific programming language?
9th Aug 2019, 3:06 PM
Michael
Michael - avatar
+ 1
A BST algorithm is usually recursive, depending on the condition for the current value (greater or lower) the algorithm is recalled with a refering keyword „left“ or „right“.
9th Aug 2019, 3:27 PM
Michael
Michael - avatar
+ 1
Ipang Sorry I'm using this app from last month so I'm not familiar with most of the things . thank you ! Here after I will make it more precise
9th Aug 2019, 3:38 PM
abishek sriram
abishek sriram - avatar
+ 1
abishek sriram Thanks for understanding bro! 👍
9th Aug 2019, 3:51 PM
Ipang
0
Michael No I just asking how can we insert in a binary tree .In BST we can insert the element in left if it is smaller and right if it is greater(I mean it will be automatically inserted according to the value) .so like that is there any way to insert in a binary tree
9th Aug 2019, 3:09 PM
abishek sriram
abishek sriram - avatar
0
Or we have to manually enter position left or right for every node?
9th Aug 2019, 3:10 PM
abishek sriram
abishek sriram - avatar
0
Michael I'm looking for cpp
9th Aug 2019, 3:20 PM
abishek sriram
abishek sriram - avatar