Your SlideShare is downloading. ×
Red black 2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Red black 2

1,012
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,012
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Red-Black Trees—Again• rank(x) = # black pointers on path from x to an external node.• Same as #black nodes (excluding x) from x to an external node.• rank(external node) = 0.
  • 2. An Example 3 10 2 2 7 40 1 3 1 8 2 30 1 451 1 1 35 1 60 1 1 5 20 0 0 0 1 25 0 0 0 00 0 0 0 0 0 0
  • 3. Properties Of rank(x)• rank(x) = 0 for x an external node.• rank(x) = 1 for x parent of external node.• p(x) exists => rank(x) <= rank(p(x)) <= rank(x) + 1.• g(x) exists => rank(x) < rank(g(x)).
  • 4. Red-Black TreeA binary search tree is a red-black tree iff integer ranks can be assigned to its nodes so as to satisfy the stated 4 properties of rank.
  • 5. Relationship Between rank() And Color• (p(x),x) is a red pointer iff rank(x) = rank(p(x)).• (p(x),x) is a black pointer iff rank(x) = rank(p(x)) – 1.• Red node iff pointer from parent is red.• Root is black.• Other nodes are black iff pointer from parent is black.• Given rank(root) and node/pointer colors, remaining ranks may be computed on way down.
  • 6. rank(root)• Height <= 2 * rank(root).• No external nodes at levels 1, 2, …, rank(root).  So, #nodes >= 1 <= i <= rank(root) 2i -1 = 2 rank(root) – 1.  So, rank(root) <= log2(n+1).• So, height(root) <= 2log2(n+1).
  • 7. Join(S,m,B)• Input  Dictionary S of pairs with small keys.  Dictionary B of pairs with big keys.  An additional pair m.  All keys in S are smaller than m.key.  All keys in B are bigger than m.key.• Output  A dictionary that contains all pairs in S and B plus the pair m.  Dictionaries S and B may be destroyed.
  • 8. Join Binary Search Trees m S B• O(1) time.
  • 9. Join Red-black Trees• When rank(S) = rank(B), use binary search tree method. m S B• rank(root) = rank(S) + 1 = rank(B) + 1.
  • 10. rank(S) > rank(B)• Follow right child pointers from root of S to first node x whose rank equals rank(B). p(x) p(x) m S x x B a b a b
  • 11. rank(S) > rank(B)• If there are now 2 consecutive red pointers/nodes, perform bottom-up rebalancing beginning at m.• O(rank(S) – rank(B)). p(x) p(x) m S x x B a b a b
  • 12. rank(S) < rank(B)• Follow left child pointers from root of B to first node x whose rank equals rank(B).• Similar to case when rank(S) > rank(B).
  • 13. Split(k)• Inverse of join.• Obtain  S … dictionary of pairs with key < k.  B … dictionary of pairs with key > k.  m … pair with key = k (if present).
  • 14. Split A Binary Search Tree A S Ba B C b c D E d e m f g
  • 15. Split A Binary Search Tree S B B A C b ac D E de m f g
  • 16. Split A Binary Search Tree S B A B C a bc D E de m f g
  • 17. Split A Binary Search Tree S B A B a C b D c E de m f g
  • 18. Split A Binary Search Tree S B A B a C D b c d Ee m f g
  • 19. Split A Binary Search Tree S B A B a C D b c E d e mf g
  • 20. Split A Binary Search Tree S B A B a C D b c E g d e fm
  • 21. Split A Red-Black Tree• Previous strategy does not split a red-black tree into two red-black trees.• Must do a search for m followed by a traceback to the root.• During the traceback use the join operation to construct S and B.
  • 22. Split A Red-Black Tree A S=f B=ga B C b c D E d e m f g
  • 23. Split A Red-Black Tree A S=f B=ga B C b c D E d e
  • 24. Split A Red-Black Tree A S=f B=ga B S = join(e, E, S) C b c D E d e
  • 25. Split A Red-Black Tree A S=f B =ga B S = join(e, E, S) C b B = join(B, D, d) c D d
  • 26. Split A Red-Black Tree A S=f B =ga B S = join(e, E, S) C b B = join(B, D, d) c S = join(c, C, S)
  • 27. Split A Red-Black Tree A S=f B =ga B S = join(e, E, S) b B = join(B, D, d) S = join(c, C, S) B = join(B, B, b)
  • 28. Split A Red-Black Tree A S=f B =ga S = join(e, E, S) B = join(B, D, d) S = join(c, C, S) B = join(B, B, b) S = join(a, A, S)
  • 29. Complexity Of Split• O(log n)• See text.

×