-2

I was studying Red Black trees and I was wondering what is the time complexity for assigning black heights for each node when we do a process like insert ?

svick
  • 236,525
  • 50
  • 385
  • 514
user3085336
  • 13
  • 1
  • 1
  • 3
  • 2
    I don't understand the question. I don't thing anything like "assigning black heights" is part of the algorithm. – svick Dec 10 '13 at 03:54
  • `what is the time complexity for assigning black heights for each node when we do a process like insert` This isn't a thing you do when inserting into a red-black tree? – Preston Guillot Dec 10 '13 at 03:56
  • I mean if you delete a node, what is the time needed to update the black heights for other nodes. Consider the node that we are deleting is the root node. What is the time needed to change the black heights for the rest of the nodes ? – user3085336 Dec 10 '13 at 04:05
  • @user3085336 Black heights for nodes are usually not stored anywhere (because they are not necessary), so the time is 0. Black heights are only useful when analyzing the algorithms. – svick Dec 10 '13 at 21:05
  • Do you mean that if we do deletion to a red black tree, the info regarding the black height is still maintained without the need to change the O(logn) time for that deletion ? – user3085336 Dec 11 '13 at 02:41

1 Answers1

2

Insertion to a Red Black tree costs log(N)...check out this cool link for other complexities of various data structures/algorithms...

http://bigocheatsheet.com/

another useful link showing how insertion/deletion/rearranging of nodes takes place in a red black tree...

http://www.cs.usfca.edu/~galles/visualization/RedBlack.html

Josh Engelsma
  • 2,636
  • 14
  • 17
  • Thank you. So if we did a deletion in a higher-level node that has a subtree down, what is the the O time needs to rearrange the black heights for all of the nodes down? – user3085336 Dec 10 '13 at 03:57
  • Actually I am not asking for the deletion, I am asking about the time it takes to rearrange the black heights for each node after deleting a node at the top, say a root for example. – user3085336 Dec 10 '13 at 04:02
  • yes, deletion/rearrange will all cost you log(N), not every node will have to have a new height necessarily because when you delete, not all heights will necessarily change...I will add another link to show how insertion/deletion for red black trees works – Josh Engelsma Dec 10 '13 at 04:06
  • So if we delete the root it needs time less than O(n) to change the black heights for the rest of the nodes (if needed)? – user3085336 Dec 10 '13 at 04:10
  • 1
    correct sir, if it cost O(n) to change heights of all the nodes, then deletion and insertion would have a complexity of O(n) not log(n)...does this help? – Josh Engelsma Dec 10 '13 at 04:11
  • Yes it helps a lot. So then if I do a deletion to a RB tree, the infor regarding the black height will be maintained without affecting the O(logn) of the deletion ? – user3085336 Dec 11 '13 at 02:45
  • That is correct! if this was helpful, consider accepting my answer – Josh Engelsma Dec 11 '13 at 02:55
  • How to accept it? Let me know how to do that and I will :) By the way, is the successor of a node info will be maintained too like black height without affecting O(logn) time after insert or delete ? Or to get the info of the successor of the node after delete or insert will require time more than O(logn) ? say O(n) ? – user3085336 Dec 13 '13 at 03:00
  • I did it. Now all the info of any node is maintained and updated in O(logn) time after delete or insert? even the successor of the node? – user3085336 Dec 13 '13 at 03:34
  • Okay bare with me please. This means that deletion or insertion will take care of everything and the worst running time would never exceed O(logn) ? no matter what the info we need to maintained for any node in the red black tree after the delete or insert? – user3085336 Dec 13 '13 at 03:42
  • No problem, Yes both operations worst case will cost log(n) – Josh Engelsma Dec 13 '13 at 03:47
  • Yea I know about their complexity. I am asking about maintaining info about the nodes like height of the node or successor of a node. maintaining the info of that node will be also O(logn) ? – user3085336 Dec 13 '13 at 03:49
  • I understand the question, yes everything is done in log(n) – Josh Engelsma Dec 13 '13 at 03:52
  • So i can say that O(logn) for insertion or deletion, getting the info of successor would be O(h) right ? if thats correct, then O(h) will not affect O(logn) for sure. – user3085336 Dec 13 '13 at 06:23
  • Info for successor would be O(logn) as well...but that still reduces to logN – Josh Engelsma Dec 13 '13 at 18:31