
Red/Black Trees
This is end of chapter review excercise i am doing on my own to practice. I want to know if this is a valid representation of a Red/black Trees:
rules of a red black tree:
a) root has to be black
b) all children of a red node are black
c) Every path from root to a leaf contains has same number of a black nodes
so prob. is start with empty red/black tree:
insert(40); insert(25); insert(10); insert(5); insert(1); insert(45); insert(50);
my representation of this is (after rebalancing and changing colors)
Code:
45  black
/ \
40  red 50  black
/ /
5black 25  red
/ \
1red 10  red
i believe this is correct but just need verification to ensure my practice answer is correct (no solutions provided in book. This IS NOT DUE for homework just practicing for my final).
After creating tree i have 2 more things to do: remove(40); remove(25)
again my implementation after rebalancing and changing colors:
Code:
45  black
/ \
5blk 50blk
/ /
1red 10  red
thanks.

 i am sure someone here can verify this ;)


thanks for the link; its pretty awesome :D
it came up with
Code:
45 black
/ \
5  black 50black
/ \
1red 10red
which is different from my ending result
Code:
45  black
/ \
5blk 50blk
/ /
1red 10  red
i understand how red/back trees are constructed. What i need to know is that is my answer correct even though it varies from the demo output(i feel mine is correct because it doesn't violate any rules of red/black trees). And that is really what i needed someone to verify for me as i know there are multiple ways to do same thing.
still thanks though.
oh and if someone needs to see demo for AVL Trees, go here:
http://www.site.uottawa.ca/~stan/csi...ts/avl/BT.html

Yeah no problem, been too long since I implemented a RB tree form me to say comfortably, but since you can follow the rules and sometimes shift either right or left depending on how you coded, you could in theory get different results. However, I remember in my class back in the day, there was only one result, perhaps because we tested based on the specific algorithm used in our text?