120 likes | 272 Views
COP 3503 - Computer Science II (Fall 2005) Week Seven. By Yunjun Zhang. A. B. C. D. E. Red-Black Tree. General Rooted Tree properties One root Each node in the tree has only one parent The path between the root and any single node in the tree is unique. (no circle) Things to remember
E N D
COP 3503 - Computer Science II (Fall 2005)Week Seven By Yunjun Zhang
A B C D E Red-Black Tree • General Rooted Tree • properties • One root • Each node in the tree has only one parent • The path between the root and any single node in the tree is unique. (no circle) • Things to remember • Parents • Children • siblings – have the same parent • Depth of a node • Height of a node
Red-Black Tree • Binary Trees • No node can have more than two children • Left • Right • Expression tree • Leaves: operands, such as constants and variables • Non-leaves: operators • 1+(2*b)/c
Red-Black Tree • A good animation reference http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html
Red-Black Tree • Binary Search Trees • Average O(logN) • Worst O(N) • To eliminate the O(N) worst case • AVL tree • red-black tree • AA tree
Red-Black Tree • Basic binary search tree: • Search order property • left tree < current node < right tree • No duplicate items (this can be changed) • Operations • Find • findMin • findMax • remove
Red-Black Tree • Basic binary search tree: • Operation remove • For leaf, easy • For node with one child: parentchild directly • For node with two children: replace the node with the smallest one in its right subtree.
Red-Black Tree • It is a binary search tree • Its operations have logN worst case running time • Properties • Nodes are colored in red/black • Root is black • if a node is black, its children must be black. • Every path from a node to a null link must contain the same number of black nodes.
Red-Black Tree • Insertion • Use find from BST • Insert the node as RED • 3 cases (When father is the left child for grandpa. Other cases are symmetric): • Father and uncle are both red
Red-Black Tree • 3 cases: • Father red, uncle black, it is the left child • Father red, uncle black, it is the right child
Red-Black Tree • Top-Down Deletion • Delete node as in a regular BSTNode to be deleted will have at most one childIf we delete a Red node tree still is a Red-Black treeAssume we delete a black node • http://sage.mc.yu.edu/kbeen/teaching/algorithms/resources/red-black-mod.html
Animation • http://www.ece.uc.edu/~franco/C321/html/RedBlack/redblack.html