DOC PREVIEW
UMBC CMSC 341 - Red-Black-Trees-1

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Red-Black TreesDefinitionsandBottom-Up Insertion8/3/2007UMBC CSMC 341 Red-Black-Trees-12Red-Black Trees Definition: A red-black tree is a binary search tree in which: Every node is colored either Red or Black. Each NULL pointer is considered to be a Black “node”. If a node is Red, then both of its children are Black. Every path from a node to a NULL contains the same number of Black nodes. By convention, the root is Black Definition: The black-height of a node, X, in a red-black tree is the number of Black nodes on any path to a NULL, not counting X.8/3/2007UMBC CSMC 341 Red-Black-Trees-13A Red-Black Tree with NULLs shownBlack-Height of the tree (the root) = 3Black-Height of node “X” = 2X8/3/2007UMBC CSMC 341 Red-Black-Trees-14A Red-Black Tree withBlack-Height = 38/3/2007UMBC CSMC 341 Red-Black-Trees-15Black Height of the tree?Black Height of X?X8/3/2007UMBC CSMC 341 Red-Black-Trees-16Theorem 1 – Any red-black tree with root x, has n ≥ 2bh(x) – 1 nodes, where bh(x) is the black height of node x.Proof: by induction on height of x.8/3/2007UMBC CSMC 341 Red-Black-Trees-17Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a NULL must be Black.Proof – If there is a Red node on the path, there must be a corresponding Black node.Algebraically this theorem meansbh( x ) ≥ h/28/3/2007UMBC CSMC 341 Red-Black-Trees-18Theorem 3 – In a red-black tree, no path from any node, X, to a NULL is more than twice as long as any other path from X to any other NULL.Proof: By definition, every path from a node to any NULL contains the same number of Black nodes. By Theorem 2, a least ½ the nodes on any such path are Black. Therefore, there can no more than twice as many nodes on any path from X to a NULL as on any other path. Therefore the length of every path is no more than twice as long as any other path.8/3/2007UMBC CSMC 341 Red-Black-Trees-19Theorem 4 –A red-black tree with n nodes has height h ≤ 2 lg(n + 1).Proof: Let h be the height of the red-black tree with root x. By Theorem 2,bh(x) ≥ h/2From Theorem 1, n ≥ 2bh(x)- 1Therefore n ≥ 2 h/2– 1n + 1 ≥ 2h/2lg(n + 1) ≥ h/22lg(n + 1) ≥ h8/3/2007UMBC CSMC 341 Red-Black-Trees-110Bottom –Up Insertion Insert node as usual in BST Color the node Red What Red-Black property may be violated? Every node is Red or Black? NULLs are Black? If node is Red, both children must be Black? Every path from node to descendant NULL must contain the same number of Blacks?8/3/2007UMBC CSMC 341 Red-Black-Trees-111Bottom Up Insertion Insert node; Color it Red; X is pointer to it Cases0: X is the root -- color it Black1: Both parent and uncle are Red -- color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation.2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children -- color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children -- color parent Black, color grandparent Red, rotate right(left) on grandparent8/3/2007UMBC CSMC 341 Red-Black-Trees-112XPGUPGUCase 1 – U is RedJust Recolor and move upX8/3/2007UMBC CSMC 341 Red-Black-Trees-113XPGUSXPGSUCase 2 – Zig-ZagDouble RotateX around P; X around GRecolor G and X8/3/2007UMBC CSMC 341 Red-Black-Trees-114XPGUSPXGSUCase 3 – Zig-ZigSingle Rotate P around GRecolor P and G8/3/2007UMBC CSMC 341 Red-Black-Trees-115Asymptotic Cost of Insertion O(lg n) to descend to insertion point O(1) to do insertion O(lg n) to ascend and readjust == worst case only for case 1 Total: O(log n)8/3/2007UMBC CSMC 341 Red-Black-Trees-116Top-Down InsertionAn alternative to this “bottom-up” insertion is “top-down” insertion.Top-down is iterative. It moves down the tree, “fixing” things as it goes.What is the objective of top-down’s “fixes”?8/3/2007UMBC CSMC 341 Red-Black-Trees-11711141521758Black nodeRed nodeInsert 4 into this R-B Tree8/3/2007UMBC CSMC 341 Red-Black-Trees-118Insertion PracticeInsert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black


View Full Document

UMBC CMSC 341 - Red-Black-Trees-1

Download Red-Black-Trees-1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Red-Black-Trees-1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Red-Black-Trees-1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?