self-balancing search trees - kennesaw state...
TRANSCRIPT
![Page 1: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/1.jpg)
SELF-BALANCING SEARCH TREES
Chapter 11
![Page 2: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/2.jpg)
Chapter Objectives
To understand the impact that balance has on the performance of binary search trees
To learn about the AVL tree for storing and maintaining a binary search tree in balance
To learn about the Red-Black tree for storing and maintaining a binary search tree in balance
If got time, will learn about 2-3 trees, 2-3-4 trees, and B-trees and how they achieve balance
To understand the process of search and insertion in each of these trees and to be introduced to removal
![Page 3: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/3.jpg)
Self-Balancing Search Trees
The performance of a binary search tree is proportional to the height of the tree or the maximum number of nodes along a path from the root to a leaf
A full binary tree of height k can hold 2k -1 items If a binary search tree is full and contains n items, the expected
performance is O(log n) However, if a binary tree is not full, the actual performance is worse
than expected To solve this problem, we introduce self-balancing trees to achieve a
balance so that the heights of the right and left subtrees are equal or nearly equal
If we got time, we will also look non-binary search trees: the B-tree and its specializations, the 2-3 and 2-3-4 trees
![Page 4: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/4.jpg)
Section 11.1
Tree Balance and Rotation
![Page 5: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/5.jpg)
Why Balance is Important
Searches into this unbalanced search tree are O(n), not O(log n)
A realistic example of an unbalanced tree
![Page 6: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/6.jpg)
Rotation
We need an operation on a binary tree that changes the relative heights of left and right subtrees, but preserves the binary search tree property
![Page 7: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/7.jpg)
Section 11.2
AVL Trees
![Page 8: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/8.jpg)
AVL Trees
In 1962 G.M. Adel'son-Vel'skiî and E.M. Landis developed a self-balancing tree. The tree is known by their initials: AVL
The AVL tree algorithm keeps track of the difference in height of each subtree
As items are added to or removed from a tree, the balance of each subtree from the insertion or removal point up to the root is updated
If the balance gets out of the range -1 to +1, the tree is rotated to bring it back into range
![Page 9: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/9.jpg)
Balancing a Left-Left Tree
50
c
25
ba
Each light purple triangle represents a tree of height k
![Page 10: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/10.jpg)
Balancing a Left-Left Tree (cont.)
50
c
25
baThe dark purple
trapezoid represents an
insertion into this tree, making its
height k + 1
![Page 11: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/11.jpg)
Balancing a Left-Left Tree (cont.)
50
c
25
ba
-2
-1
The formula
hR – hLis used to calculate the balance of each node
k - (k + 1)
k – (k + 2)The heights of the left and right subtrees are unimportant; only the relative difference matters when balancing
![Page 12: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/12.jpg)
Balancing a Left-Left Tree (cont.)
50
c
25
ba
-2
-1
When the root and left subtree are both left-
heavy, the tree is called a Left-Left tree
![Page 13: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/13.jpg)
Balancing a Left-Left Tree (cont.)
50
c
25
ba
-2
-1
A Left-Left tree can be balanced by a rotation
right
![Page 14: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/14.jpg)
Balancing a Left-Left Tree (cont.)
50
c
25
b
a
0
0
![Page 15: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/15.jpg)
Balancing a Left-Right Tree
50
c
25
a b
-2
+1(k + 1) - k
k - (k + 2)
![Page 16: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/16.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a b
-2
+1
A Left-Right tree cannot be balanced by a simple rotation
right
![Page 17: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/17.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a b
-2
+1
Subtree b needs to be expanded into its subtrees bL
and bR
![Page 18: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/18.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a
-2
+1
40
bRbL40 is left-heavy. The left
subtree now can be rotated left
-1
![Page 19: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/19.jpg)
Balancing a Left-Right Tree (cont.)
50
c25
a
-2
-240
bR
bL
0
The overall tree is now Left-Left and a rotation right will balance
it.
![Page 20: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/20.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a
0
+1
40
bRbL
0
![Page 21: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/21.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a
-2
+1
40
bL bR
In the previous example, an item was inserted in bL.
We now show the steps if an item was inserted into bR instead
+1
![Page 22: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/22.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a
-2
+1
40
bL bR Rotate the left subtree left
+1
![Page 23: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/23.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a
-2
-140
bL
bR
Rotate the tree right
-1
![Page 24: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/24.jpg)
Balancing a Left-Right Tree (cont.)
50
c
25
a
0
0
40
bL bR
-1
![Page 25: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/25.jpg)
Four Kinds of Critically Unbalanced Trees
Left-Left (parent balance is -2, left child balance is -1) Rotate right around parent
Left-Right (parent balance -2, left child balance +1) Rotate left around child Rotate right around parent
Right-Right (parent balance +2, right child balance +1) Rotate left around parent
Right-Left (parent balance +2, right child balance -1) Rotate right around child Rotate left around parent
![Page 26: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/26.jpg)
AVL Tree Example
Build an AVL tree from the words in "The quick brown fox jumps over the lazy dog"
![Page 27: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/27.jpg)
AVL Tree Example (cont.)
The
quick
brown
+2
-1
0
The overall tree is right-heavy (Right-Left)
parent balance = +2right child balance = -1
![Page 28: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/28.jpg)
AVL Tree Example (cont.)
The
quick
brown
+2
-1
0
1. Rotate right around the child
![Page 29: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/29.jpg)
AVL Tree Example (cont.)
The
brown
quick
+2
+1
0
1. Rotate right around the child
![Page 30: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/30.jpg)
AVL Tree Example (cont.)
The
brown
quick
+2
+1
0
1. Rotate right around the child
2. Rotate left around the parent
![Page 31: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/31.jpg)
AVL Tree Example (cont.)
brown
quickThe
0
00
1. Rotate right around the child
2. Rotate left around the parent
![Page 32: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/32.jpg)
AVL Tree Example (cont.)
brown
quickThe
0
00Insert fox
![Page 33: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/33.jpg)
AVL Tree Example (cont.)
brown
quickThe
+1
-10Insert fox
fox 0
![Page 34: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/34.jpg)
AVL Tree Example (cont.)
brown
quickThe
+1
-10Insert jumps
fox 0
![Page 35: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/35.jpg)
AVL Tree Example (cont.)
brown
quickThe
+2
-20Insert jumps
fox +1
jumps 0
![Page 36: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/36.jpg)
AVL Tree Example (cont.)
brown
quickThe
+2
-20
fox +1
jumps 0
The tree is now left-heavy about quick (Left-Right case)
![Page 37: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/37.jpg)
AVL Tree Example (cont.)
brown
quickThe
+2
-20
fox +1
jumps 0
1. Rotate left around the child
![Page 38: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/38.jpg)
AVL Tree Example (cont.)
brown
quickThe
+2
-20
jumps -1
fox 0
1. Rotate left around the child
![Page 39: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/39.jpg)
AVL Tree Example (cont.)
brown
quickThe
+2
-20
jumps -1
fox 0
1. Rotate left around the child
2. Rotate right around the parent
![Page 40: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/40.jpg)
AVL Tree Example (cont.)
brown
jumpsThe
+1
00
fox 0 quick 0
1. Rotate left around the child
2. Rotate right around the parent
![Page 41: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/41.jpg)
AVL Tree Example (cont.)
brown
jumpsThe
+1
00
fox 0 quick 0
Insert over
![Page 42: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/42.jpg)
AVL Tree Example (cont.)
brown
jumpsThe
+2
+10
fox 0 quick -1
Insert over
over 0
![Page 43: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/43.jpg)
AVL Tree Example (cont.)
brown
jumpsThe
+2
+10
fox 0 quick -1
over 0
We now have a Right-Right imbalance
![Page 44: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/44.jpg)
AVL Tree Example (cont.)
brown
jumpsThe
+2
+10
fox 0 quick -1
over 0
1. Rotate left around the parent
![Page 45: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/45.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
0
-10
The 0 fox 0 over 0
1. Rotate left around the parent
![Page 46: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/46.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
0
-10
The 0 fox 0 over 0
Insert the
![Page 47: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/47.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
0
00
The 0 fox 0 over 0
Insert the
the 0
![Page 48: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/48.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
0
00
The 0 fox 0 over 0
Insert lazy
the 0
![Page 49: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/49.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
+1
-10
The 0 fox 0 over -1
Insert lazy
the 0
lazy 0
![Page 50: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/50.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
+1
-10
The 0 fox 0 over -1
Insert dog
the 0
lazy 0
![Page 51: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/51.jpg)
AVL Tree Example (cont.)
jumps
quickbrown
0
-1+1
The 0 fox -1 over -1
Insert dog
the 0
lazy 0dog 0
![Page 52: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/52.jpg)
Section 11.3
Red-Black Trees
![Page 53: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/53.jpg)
Red-Black Trees
Rudolf Bayer developed the Red-Black tree as a special case of his B-tree
Leo Guibas and Robert Sedgewick refined the concept and introduced the color convention
![Page 54: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/54.jpg)
Red-Black Trees (cont.)
A Red-Black tree maintains the following invariants:
1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
![Page 55: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/55.jpg)
Red-Black Trees (cont.)
Height is determined by counting only black nodes
A Red-Black tree is always balanced because the root node’s left and right subtrees must be the same height
By the standards of the AVL tree this tree is out of balance and would be considered a Left-Right tree
However, by the standards of the Red-Black tree it is balanced, because there are two black nodes (counting the root) in any path from the root to a leaf
11
2 14
5 8
71
![Page 56: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/56.jpg)
Insertion into a Red-Black Tree
The algorithm follows the same recursive search process used for all binary search trees to reach the insertion point
When a leaf position is found, the new item is inserted and initially given the color red
If the parent is black, we are done; otherwise there is some rearranging to do
We introduce three situations ("cases") that may occur when a node is inserted; more than one can occur after an insertion
![Page 57: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/57.jpg)
Insertion into a Red-Black Tree (cont.)
20
10 30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
CASE 1
![Page 58: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/58.jpg)
Insertion into a Red-Black Tree (cont.)
20
10 30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red
CASE 1
![Page 59: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/59.jpg)
Insertion into a Red-Black Tree (cont.)
20
10 30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
If a parent is red, and its sibling is also red, they can both be changed to black, and the grandparent to red
CASE 1
![Page 60: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/60.jpg)
Insertion into a Red-Black Tree (cont.)
20
10 30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
The root can be changed to black and still maintain
invariant 4
CASE 1
![Page 61: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/61.jpg)
Insertion into a Red-Black Tree (cont.)
20
10 30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
The root can be changed to black and still maintain
invariant 4
CASE 1
![Page 62: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/62.jpg)
Insertion into a Red-Black Tree (cont.)
20
10 30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Balanced tree
CASE 1
![Page 63: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/63.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
CASE 2
![Page 64: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/64.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
If a parent is red (with no sibling), it can be changed to black, and the grandparent
to red
CASE 2
![Page 65: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/65.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
If a parent is red (with no sibling), it can be changed to black, and the grandparent
to red
CASE 2
![Page 66: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/66.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
There is one black node on the right and none on the
left, which violates invariant 4
CASE 2
![Page 67: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/67.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
35
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Rotate left around the grandparent to correct this
CASE 2
![Page 68: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/68.jpg)
Insertion into a Red-Black Tree (cont.)
30
3520
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Rotate left around the grandparent to correct this
CASE 2
![Page 69: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/69.jpg)
Insertion into a Red-Black Tree (cont.)
30
3520
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Balanced tree
CASE 2
![Page 70: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/70.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
CASE 3
![Page 71: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/71.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
25
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
If a parent is red (with no sibling), it can be changed to black, and the grandparent
to red
CASE 3
![Page 72: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/72.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
25
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
If a parent is red (with no sibling), it can be changed to black, and the grandparent
to red
CASE 3
![Page 73: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/73.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
25
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
A rotation left does not fix the violation of #4
CASE 3
![Page 74: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/74.jpg)
Insertion into a Red-Black Tree (cont.)
30
20
25
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
A rotation left does not fix the violation of #4
CASE 3
![Page 75: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/75.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
25
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Back-up to the beginning (don't perform rotation or
change colors)
CASE 3
![Page 76: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/76.jpg)
Insertion into a Red-Black Tree (cont.)
20
30
25
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Rotate right about the parent so that the red child is on the same side of the parent as the parent is to
the grandparent
CASE 3
![Page 77: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/77.jpg)
Insertion into a Red-Black Tree (cont.)
20
25
30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Rotate right about the parent so that the red child is on the same side of the parent as the parent is to
the grandparent
CASE 3
![Page 78: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/78.jpg)
Insertion into a Red-Black Tree (cont.)
20
25
30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
NOW, change colors
CASE 3
![Page 79: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/79.jpg)
Insertion into a Red-Black Tree (cont.)
20
25
30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
NOW, change colors
CASE 3
![Page 80: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/80.jpg)
Insertion into a Red-Black Tree (cont.)
20
25
30
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
and rotate left . . .
CASE 3
![Page 81: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/81.jpg)
Insertion into a Red-Black Tree (cont.)
25
3020
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
and rotate left. . .
CASE 3
![Page 82: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/82.jpg)
Insertion into a Red-Black Tree (cont.)
25
3020
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Balanced tree
CASE 3
![Page 83: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/83.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
![Page 84: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/84.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
4
![Page 85: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/85.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
4If a parent is red, and its
sibling is also red, they can both be changed to black, and the grandparent to red
CASE 1
![Page 86: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/86.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
4If a parent is red, and its
sibling is also red, they can both be changed to black, and the grandparent to red
CASE 1
![Page 87: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/87.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
4The problem has now shifted
up the tree
![Page 88: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/88.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
4We cannot change 2 to black
because its sibling 14 is already black (both siblings have to be red (unless there is no sibling) to
do the color change
CASE 3
![Page 89: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/89.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
2 14
5 8
71
4We need to rotate left around 2
so that the red child is on the same side of the parent as the parent is to the grandparent
CASE 3
![Page 90: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/90.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
7 14
51
82
4We need to rotate left around 2
so that the red child is on the same side of the parent as the parent is to the grandparent
CASE 3
![Page 91: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/91.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
7 14
51
82
4
Change colors
CASE 3
![Page 92: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/92.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
7 14
51
82
4
Change colors
CASE 3
![Page 93: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/93.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
11
7 14
51
82
4Rotate right around 11 to
restore the balance
CASE 3
![Page 94: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/94.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
7
2 11
14851
4
Rotate right around 11 to restore the balance
CASE 3
![Page 95: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/95.jpg)
Insertion into a Red-Black Tree (cont.)
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
7
2 11
14851
4
Balanced tree
![Page 96: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/96.jpg)
Red-Black Tree Example
Build a Red-Black tree for the words in "The quick brown fox jumps over the lazy dog"
![Page 97: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/97.jpg)
Red-Black Tree Example (cont.)
The
quick
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
![Page 98: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/98.jpg)
Red-Black Tree Example (cont.)
The
quick
brown
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Rotate so that the child is on the same side of its
parent as its parent is to the grandparent
CASE 3
![Page 99: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/99.jpg)
Red-Black Tree Example (cont.)
The
brown
quick
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Change colors
CASE 3
![Page 100: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/100.jpg)
Red-Black Tree Example (cont.)
The
brown
quick
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Change colors
CASE 3
![Page 101: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/101.jpg)
Red-Black Tree Example (cont.)
The
brown
quick
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
Rotate left
CASE 3
![Page 102: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/102.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
![Page 103: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/103.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
![Page 104: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/104.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
fox's parent and its parent's sibling are both
red. Change colors.
CASE 1
![Page 105: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/105.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
fox's parent and its parent's sibling are both
red. Change colors.
CASE 1
![Page 106: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/106.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
We can change brown's color to black and not
violate #4
CASE 1
![Page 107: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/107.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
We can change brown's color to black and not
violate #4
CASE 1
![Page 108: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/108.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
jumps
![Page 109: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/109.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox
jumps
Rotate so that red child is on same side of its parent
as its parent is to the grandparent
CASE 3
![Page 110: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/110.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
jumps
fox
Change fox's parent and grandparent colors
CASE 3
![Page 111: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/111.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
jumps
fox
Change fox's parent and grandparent colors
CASE 3
![Page 112: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/112.jpg)
Red-Black Tree Example (cont.)
brown
quickThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
jumps
fox
Rotate right about quick
CASE 3
![Page 113: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/113.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
Rotate right about quick
CASE 3
![Page 114: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/114.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
over
![Page 115: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/115.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
overCASE 1
Change colors of parent, parent's sibling and
grandparent
![Page 116: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/116.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
overCASE 1
Change colors of parent, parent's sibling and
grandparent
![Page 117: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/117.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
over
No changes needed
the
![Page 118: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/118.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
over the
lazy
![Page 119: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/119.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
over the
lazy
Because over and the are both red, change parent,
parent's sibling and grandparent colors
CASE 1
![Page 120: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/120.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
over the
lazy
CASE 2
![Page 121: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/121.jpg)
Red-Black Tree Example (cont.)
brown
jumpsThe
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
fox quick
over the
lazy
CASE 2
![Page 122: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/122.jpg)
Red-Black Tree Example (cont.)
jumps
quickbrown
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
over theThe fox
lazy
CASE 2
![Page 123: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/123.jpg)
Red-Black Tree Example (cont.)
jumps
quickbrown
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
over theThe fox
lazydog
![Page 124: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/124.jpg)
Red-Black Tree Example (cont.)
jumps
quickbrown
Invariants:1. A node is either red or black2. The root is always black3. A red node always has black
children (a NULL pointer is considered to refer to a black node)
4. The number of black nodes in any path from the root to a leaf is the same
over theThe fox
lazydog
Balanced tree
![Page 125: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/125.jpg)
GRAPHS
Chapter 12
![Page 126: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/126.jpg)
Chapter Objectives
To become familiar with graph terminology and the different types of graphs
To study the Graph ADT and different implementations of the Graph ADT
To learn the breadth-first and depth-first search traversal algorithms
To learn some algorithms involving weighted graphs To study some applications of graphs and graph
algorithms
![Page 127: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/127.jpg)
Graphs
Trees are limited in that a data structure can have only one parent Graphs overcome this limitation Graphs were studied long before computers were invented, but
associated algorithms were impractical before the advent of computers
Graphs algorithms facilitate large communication networks the software that makes the Internet function programs to determine optimal placement of components on a silicon
chip Graphs describe
roads maps airline routes course prerequisites
![Page 128: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/128.jpg)
Section 12.1
Graph Terminology
![Page 129: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/129.jpg)
Graph Terminology
A graph is a data structure that consists of a set of vertices (or nodes) and a set of edges (relations) between pairs of vertices
Edges represent paths or connections between vertices Both the set of vertices and the set of edges must be
finite Either set may be empty (if the set of vertices is empty,
the set of edges also must be empty) We restrict our discussion to simple graphs in which there
is at least one edge from a given vertex to another vertex
![Page 130: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/130.jpg)
Visual Representation of Graphs
Vertices are represented as points or labeled circles and edges are represented as line segments joining the vertices
V = {A, B, C, D, E}E = {{A, B}, {A, D}, {C, E}, {D, E}}
![Page 131: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/131.jpg)
Visual Representation of Graphs (cont.)
Each edge is represented by the two vertices it connects
If there is an edge between vertices x and y, there is a path from x to y and vice versa
![Page 132: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/132.jpg)
Visual Representation of Graphs (cont.)
The physical layout of the vertices and their labeling is not relevant
V = {0, 1, 2, 3, 4, 5, 6}E = {{0, 1}, {0, 2}, {0, 5}, {0, 6}, {3, 5}, {3, 4}, {4, 5}, {4, 6}}
![Page 133: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/133.jpg)
Directed and Undirected Graphs
The edges of a graph aredirected if the existence of an edge from A to B does not necessarily guarantee that there is an edge from B to A
A graph with directed edges is called a directed graph or digraph
A graph with undirected edges is an undirected graph, or simply a graph
![Page 134: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/134.jpg)
Directed and Undirected Graphs (cont.) A directed edge is like a one-
way street; you can travelin only one direction
Directed edges are represented as lines with an arrowhead on one end (undirected edges do not have an arrowhead at either end)
Directed edges are represented by ordered pairs of vertices {source, destination}; the edges for the digraph on this slide are:
E = {{A, B}, {B, A}, {B, E}, {D, A}, {E, A}, {E, C}, {E, D}}
![Page 135: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/135.jpg)
Directed and Undirected Graphs (cont.)
The edges in a graph may have associated values known as weights
A graph with weighted edges is known as a weighted graph
320
130
180
150
180
180 120
148
26040
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann ArborDetroit
Toledo
Cleveland Pittsburgh
Philadelphia
![Page 136: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/136.jpg)
Paths and Cycles
• The following definitions describe pathways between vertices
![Page 137: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/137.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A vertex is adjacent to another vertex if there is an edge to it from that other vertex
![Page 138: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/138.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A vertex is adjacent to another vertex if there is an edge to it from that other vertex
Philadelphia is adjacent to Pittsburgh
![Page 139: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/139.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A vertex is adjacent to another vertex if there is an edge to it from that other vertexIndianapolis is adjacent to
Columbus
![Page 140: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/140.jpg)
Paths and Cycles (cont.)
A
ED
B
C
A vertex is adjacent to another vertex if there is an edge to it from that other vertex
A is adjacent to D, but D is NOT adjacent to A
![Page 141: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/141.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A path is a sequence of vertices in which each successive vertex is adjacent to its predecessor
![Page 142: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/142.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A path is a sequence of vertices in which each successive vertex is adjacent to its predecessor
![Page 143: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/143.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
In a simple path, the vertices and edges are distinct except that the first and last vertex may be the same
![Page 144: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/144.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
In a simple path, the vertices and edges are distinct except that the first and last vertex may be the sameThis path is a simple
path
![Page 145: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/145.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
In a simple path, the vertices and edges are distinct except that the first and last vertex may be the sameThis path is NOT a
simple path
![Page 146: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/146.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A cycle is a simple path in which only the first and final vertices are the same
![Page 147: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/147.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
A cycle is a simple path in which only the first and final vertices are the same
In an undirected graph a cycle must contain at least three distinct vertices;
Pittsburgh → Columbus → Pittsburghis not a cycle
![Page 148: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/148.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
An undirected graph is called a connected graph if there is a path from every vertex to every other vertex
![Page 149: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/149.jpg)
Paths and Cycles (cont.)
320
130
180
150
180
180 120
148
260
40
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann Arbor
Detroit
Toledo
Cleveland Pittsburgh
Philadelphia
An undirected graph is called a connected graph if there is a path from every vertex to every other vertex
This graph is a connected graph
![Page 150: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/150.jpg)
Paths and Cycles (cont.)
An undirected graph is called a connected graph if there is a path from every vertex to every other vertex
4
8
5
9
6 7
![Page 151: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/151.jpg)
Paths and Cycles (cont.)
An undirected graph is called a connected graph if there is a path from every vertex to every other vertex
This graph is a connected graph
4
8
5
9
6 7
![Page 152: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/152.jpg)
Paths and Cycles (cont.)
An undirected graph is called a connected graph if there is a path from every vertex to every other vertex
4
8
5
9
6 7
![Page 153: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/153.jpg)
Paths and Cycles (cont.)
An undirected graph is called a connected graph if there is a path from every vertex to every other vertex
This graph is NOT a connected graph
4
8
5
9
6 7
![Page 154: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/154.jpg)
Paths and Cycles (cont.)
If a graph is not connected, it is considered unconnected, but still consists of connected components
4
8
5
9
6 7
![Page 155: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/155.jpg)
Paths and Cycles (cont.)
If a graph is not connected, it is considered unconnected, but will still consist of connected components
{4, 5} are connected components
4
8
5
9
6 7
![Page 156: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/156.jpg)
Paths and Cycles (cont.)
If a graph is not connected, it is considered unconnected, but will still consist of connected components
{6, 7, 8, 9} are connected
components
4
8
5
9
6 7
![Page 157: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/157.jpg)
Paths and Cycles (cont.)
If a graph is not connected, it is considered unconnected, but will still consist of connected components
A single vertex with no edge is
considered a connected component
4
8 9
6 7
![Page 158: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/158.jpg)
Relationship between Graphs and Trees A tree is a special case of a graph Any graph that is
connected contains no cyclescan be viewed as a tree by making one of the vertices the root
![Page 159: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/159.jpg)
Graph Applications
Graphs can be used to: determine if one node in a network is connected to all
the others map out multiple course prerequisites (a solution exists
if the graph is a directed graph with no cycles) find the shortest route from one city to another (least
cost or shortest path in a weighted graph)
![Page 160: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/160.jpg)
Section 12.3
Implementing the Graph ADT
![Page 161: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/161.jpg)
Implementing the Graph ADT
Many of the original publications of graph algorithms and their implementations did not use an object-oriented approach or even abstract data types
Two representations of graphs are most common Edges are represented by an array of lists called
adjacency lists, where each list stores the vertices adjacent to a particular vertex
Edges are represented by a two dimensional array, called an adjacency matrix, with |V| rows and |V| columns
![Page 162: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/162.jpg)
Adjacency List
An adjacency list representation of a graph uses an array of lists - one list for each vertex
The vertices are in no particular order In each node, only the destination vertex as shown
as the value field In the actual implementation, the entire Edge is stored
Symmetric entries are required for an undirected graph If [u,v] is an edge, then v appears in the adjacency list for u
and u appears in the adjacency list for v
![Page 163: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/163.jpg)
Adjacency List – Directed Graph Example
![Page 164: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/164.jpg)
Adjacency List – Undirected Graph Example
![Page 165: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/165.jpg)
Adjacency Matrix
Use a two-dimensional array to represent the graph For an unweighted graph, the entries can be bool
or int values: true or 1, if the edge exists false or 0, if the edge does not exist
Integer values have benefits over boolean values for some graph algorithms that use matrix multiplication
![Page 166: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/166.jpg)
Adjacency Matrix (cont.)
For a weighted graph, the matrix would contain the weights Since 0 is a valid weight, numeric_limits<double>::infinity() (a special double value in C++ that approximates the mathematical behavior of infinity) can represent the absence of an edge
An unweighted graph would contain the value 1.0 to indicate the presence of an edge
In an undirected graph, the matrix is symmetric, so only the lower diagonal of the matrix needs to be saved (an example is on the following slide)
![Page 167: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/167.jpg)
Adjacency Matrix (cont.)
![Page 168: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/168.jpg)
Section 12.4
Traversals of Graphs
![Page 169: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/169.jpg)
Traversals of Graphs
Most graph algorithms involve visiting each vertex in a systematic order
As with trees, there are different ways to do this The two most common traversal algorithms are the
breadth-first search and the depth-first search
![Page 170: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/170.jpg)
Breadth-First Search
In a breadth-first search, visit the start node first, then visit all nodes that are adjacent to it, then visit all nodes that can be reached by a path from the
start node containing two edges, then visit all nodes that can be reached by a path from the
start node containing three edges, and so on
We must visit all nodes for which the shortest path from the start node is length k before we visit any node for which the shortest path from the start node is length k+1
![Page 171: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/171.jpg)
Breadth-First Search, (cont.)
Visualize a breadth-first traversal by “picking up” the graph at the vertex that is the start node, with the remaining nodes suspended beneath it, connected by their edges Nodes that are “higher” are visited before nodes that
are “lower”
There is no special start vertex – we arbitrarily choose the vertex with label 0
200
![Page 172: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/172.jpg)
Breadth-First Search, (cont.)
If the graph is not connected, the process is repeated for the unconnected component(s) by selecting an unidentified vertex
201
![Page 173: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/173.jpg)
Example of a Breadth-First Search
0
2
3 1
9 8
4
7
6
5
0 visited 0 identified0 unvisited
![Page 174: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/174.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
Select the start node
0 visited 0 identified0 unvisited
![Page 175: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/175.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
While visiting it, identify its
adjacent nodes
0 visited 0 identified0 unvisited
![Page 176: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/176.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5Identify its adjacent nodes
and add them to a queue of identified
nodes
Visit sequence:0
0 visited 0 identified0 unvisited
![Page 177: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/177.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
Visit sequence:0
Queue:1, 3
0 visited 0 identified0 unvisited
Identify its adjacent nodes
and add them to a queue of identified
nodes
![Page 178: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/178.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
Visit sequence:0
Queue:1, 3
0 visited 0 identified0 unvisited
Color the node as visited
![Page 179: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/179.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
Visit sequence:0
Queue:1, 3
The queue determines which nodes to visit next
0 visited 0 identified0 unvisited
![Page 180: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/180.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
Visit the first node in the queue, 1
Visit sequence:0
Queue:1, 3
0 visited 0 identified0 unvisited
![Page 181: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/181.jpg)
Example of a Breadth-First Search (cont.)
0
2
3 1
9 8
4
7
6
5
Visit sequence:0, 1
Queue:3
Visit the first node in the queue, 1
0 visited 0 identified0 unvisited
![Page 182: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/182.jpg)
Example of a Breadth-First Search (cont.)
Select all its adjacent nodes that
have not been visited or identified
Visit sequence:0, 1
Queue:3
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 183: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/183.jpg)
Example of a Breadth-First Search (cont.)
Select all its adjacent nodes that
have not been visited or identified
Visit sequence:0, 1
Queue:3, 2, 4, 6, 7
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 184: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/184.jpg)
Example of a Breadth-First Search (cont.)
Now that we are done with 1, we color it as visited
Visit sequence:0, 1
Queue:3, 2, 4, 6, 7
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 185: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/185.jpg)
Example of a Breadth-First Search (cont.)
and then visit the next node in the queue, 3 (which was identified in the first selection)
Visit sequence:0, 1
Queue:3, 2, 4, 6, 7
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 186: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/186.jpg)
Example of a Breadth-First Search (cont.)
Visit sequence:0, 1, 3
Queue:2, 4, 6, 7
0 visited 0 identified0 unvisited
and then visit the next node in the queue, 3 (which was identified in the first selection)
0
2
3 1
9 8
4
7
6
5
![Page 187: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/187.jpg)
Example of a Breadth-First Search (cont.)
3 has two adjacent vertices. 0 has been visited
already and 2 has been identified
already. We are done with 3
Visit sequence:0, 1, 3
Queue:2, 4, 6, 7
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 188: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/188.jpg)
Example of a Breadth-First Search (cont.)
The next node in the queue is 2
Visit sequence:0, 1, 3
Queue:2, 4, 6, 7
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 189: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/189.jpg)
Example of a Breadth-First Search (cont.)
The next node in the queue is 2
Visit sequence:0, 1, 3, 2
Queue:4, 6, 7
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 190: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/190.jpg)
Example of a Breadth-First Search (cont.)
8 and 9 are the only adjacent vertices not
already visited or identified
Visit sequence:0, 1, 3, 2
Queue:4, 6, 7, 8, 9
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 191: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/191.jpg)
Example of a Breadth-First Search (cont.)
4 is next
Visit sequence:0, 1, 3, 2, 4
Queue:6, 7, 8, 9
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 192: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/192.jpg)
Example of a Breadth-First Search (cont.)
5 is the only vertex not already visited
or identified
Visit sequence:0, 1, 3, 2, 4
Queue:6, 7, 8, 9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 193: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/193.jpg)
Example of a Breadth-First Search (cont.)
6 has no vertices not already visited
or identified
Visit sequence:0, 1, 3, 2, 4, 6
Queue:7, 8, 9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 194: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/194.jpg)
Example of a Breadth-First Search (cont.)
6 has no vertices not already visited
or identified
Visit sequence:0, 1, 3, 2, 4, 6
Queue:7, 8, 9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 195: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/195.jpg)
Example of a Breadth-First Search (cont.)
7 has no vertices not already visited
or identified
Visit sequence:0, 1, 3, 2, 4, 6, 7
Queue:8, 9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 196: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/196.jpg)
Example of a Breadth-First Search (cont.)
7 has no vertices not already visited
or identified
Visit sequence:0, 1, 3, 2, 4, 6, 7
Queue:8, 9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 197: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/197.jpg)
Example of a Breadth-First Search (cont.)
Go back to the vertices of 2 and
visit them
Visit sequence:0, 1, 3, 2, 4, 6, 7
Queue:8, 9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 198: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/198.jpg)
Example of a Breadth-First Search (cont.)
8 has no vertices not already visited
or identified
Visit sequence:0, 1, 3, 2, 4, 6, 7, 8
Queue:9, 5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 199: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/199.jpg)
Example of a Breadth-First Search (cont.)
9 has no vertices not already visited
or identified
Visit sequence:0, 1, 3, 2, 4, 6, 7, 8, 9
Queue:5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 200: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/200.jpg)
Example of a Breadth-First Search (cont.)
Finally, visit 5
Visit sequence:0, 1, 3, 2, 4, 6, 7, 8, 9
Queue:5
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 201: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/201.jpg)
Example of a Breadth-First Search (cont.)
which has no vertices not
already visited or identified
Visit sequence:0, 1, 3, 2, 4, 6, 7, 8, 9, 5
Queue:empty
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 202: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/202.jpg)
Example of a Breadth-First Search (cont.)
The queue is empty; all vertices have been visited
Visit sequence:0, 1, 3, 2, 4, 6, 7, 8, 9, 5
Queue:empty
0 visited 0 identified0 unvisited
0
2
3 1
9 8
4
7
6
5
![Page 203: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/203.jpg)
Depth-First Search
In a depth-first search, start at a vertex, visit it, choose one adjacent vertex to visit; then, choose a vertex adjacent to that vertex to visit, and so on until you can go no further; then back up and see whether a new vertex can be
found
![Page 204: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/204.jpg)
Example of a Depth-First Search
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
![Page 205: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/205.jpg)
Example of a Depth-First Search (cont.)
Mark 0 as being visited
0 visited 0 being visited0 unvisited
0
12
3 4 5 6Finish order:
Discovery (Visit) order:0
![Page 206: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/206.jpg)
Example of a Depth-First Search (cont.)
Choose an adjacent vertex that is not being
visited
0 visited 0 being visited0 unvisited
0
12
3 4 5 6Finish order:
Discovery (Visit) order:0
![Page 207: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/207.jpg)
Example of a Depth-First Search (cont.)
Choose an adjacent vertex that is not being
visited
0 visited 0 being visited0 unvisited
0
12
3 4 5 6Finish order:
Discovery (Visit) order:0, 1
![Page 208: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/208.jpg)
Example of a Depth-First Search (cont.)
(Recursively) choose an adjacent vertex
that is not being visited
0 visited 0 being visited0 unvisited
0
12
3 4 5 6Finish order:
Discovery (Visit) order:0, 1, 3
![Page 209: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/209.jpg)
Example of a Depth-First Search (cont.)
(Recursively) choose an adjacent vertex
that is not being visited
0 visited 0 being visited0 unvisited
0
12
3 4 5 6Finish order:
Discovery (Visit) order:0, 1, 3
![Page 210: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/210.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
(Recursively) choose an adjacent vertex
that is not being visited
Finish order:
Discovery (Visit) order:0, 1, 3, 4
![Page 211: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/211.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
There are no vertices adjacent to
4 that are not being visited
Finish order:
Discovery (Visit) order:0, 1, 3, 4
![Page 212: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/212.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 4 as visited
Finish order:4
Discovery (Visit) order:0, 1, 3, 4
![Page 213: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/213.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Return from the recursion to 3; all adjacent nodes to 3 are being visited
Finish order:4
![Page 214: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/214.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 3 as visited
Finish order:4, 3
![Page 215: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/215.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Return from the recursion to 1
Finish order:4, 3
![Page 216: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/216.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
All vertices adjacent to 1 are
being visited
Finish order:4, 3
![Page 217: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/217.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 1 as visited
Finish order:4, 3, 1
![Page 218: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/218.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Return from the recursion to 0
Finish order:4, 3, 1
![Page 219: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/219.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
2 is adjacent to 1 and is not being
visited
Finish order:4, 3, 1
![Page 220: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/220.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
2 is adjacent to 1 and is not being
visited
Finish order:4, 3, 1
Discovery (Visit) order:0, 1, 3, 4, 2
![Page 221: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/221.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
5 is adjacent to 2 and is not being
visited
Finish order:4, 3, 1
Discovery (Visit) order:0, 1, 3, 4, 2
![Page 222: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/222.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
5 is adjacent to 2 and is not being
visited
Finish order:4, 3, 1
Discovery (Visit) order:0, 1, 3, 4, 2, 5
![Page 223: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/223.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
6 is adjacent to 5 and is not being
visited
Finish order:4, 3, 1
Discovery (Visit) order:0, 1, 3, 4, 2, 5
![Page 224: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/224.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
6 is adjacent to 5 and is not being
visited
Finish order:4, 3, 1
Discovery (Visit) order:0, 1, 3, 4, 2, 5, 6
![Page 225: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/225.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
There are no vertices adjacent to 6 not being visited; mark 6 as visited
Finish order:4, 3, 1
Discovery (Visit) order:0, 1, 3, 4, 2, 5, 6
![Page 226: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/226.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
There are no vertices adjacent to 6 not being visited; mark 6 as visited
Finish order:4, 3, 1, 6
Discovery (Visit) order:0, 1, 3, 4, 2, 5, 6
![Page 227: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/227.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Return from the recursion to 5
Finish order:4, 3, 1, 6
![Page 228: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/228.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 5 as visited
Finish order:4, 3, 1, 6
![Page 229: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/229.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 5 as visited
Finish order:4, 3, 1, 6, 5
![Page 230: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/230.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Return from the recursion to 2
Finish order:4, 3, 1, 6, 5
![Page 231: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/231.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 2 as visited
Finish order:4, 3, 1, 6, 5
![Page 232: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/232.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 2 as visited
Finish order:4, 3, 1, 6, 5, 2
![Page 233: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/233.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Return from the recursion to 0
Finish order:4, 3, 1, 6, 5, 2
![Page 234: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/234.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
There are no nodes adjacent to 0 not
being visited
Finish order:4, 3, 1, 6, 5, 2
![Page 235: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/235.jpg)
Example of a Depth-First Search (cont.)
0 visited 0 being visited0 unvisited
0
12
3 4 5 6
Mark 0 as visited
Finish order:4, 3, 1, 6, 5, 2, 0
Discovery (Visit) order:0, 1, 3, 4, 2, 5, 6, 0
![Page 236: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/236.jpg)
Search Terms
The discovery order is the order in which the vertices are discovered 0, 1, 3, 4, 2, 5, 6 in this example
The finish order is the order in which the vertices are finished 4, 3, 1, 6, 5, 2, 0 in this example
Back edges connect a vertex with its ancestors in a depth-first search tree
![Page 237: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/237.jpg)
Search Terms (cont.)
![Page 238: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/238.jpg)
Algorithm for Depth-First Search
![Page 239: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/239.jpg)
Section 12.6
Algorithms Using Weighted Graphs
![Page 240: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/240.jpg)
Finding the Shortest Path from a Vertex to All Other Vertices
The breadth-first search found the shortest path from the start vertex to all other vertices, assuming that the length or weight of each edge was the same
Dijkstra's algorithm finds the shortest path in a weighted directed graph
![Page 241: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/241.jpg)
Finding the Shortest Path from a Vertex to All Other Vertices (cont.)
We need 2 sets and 2 arrays Set S will contain the vertices for which we have computed
the shortest distance Initialize S by placing the start vertex s into it
Set V-S will contain the vertices we still need to process Initialize V-S by placing the remaining vertices into it
d[v] will contain the shortest distance from s to v For each v in V-S, set d[v] to the weight of the edge w(s,v) for
each vertex v adjacent to s and to ∞for each vertex not adjacent to s
p[v] will contain the predecessor of v in the path from s to v Initialize p[v] to s for each v in V-S
![Page 242: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/242.jpg)
Dijkstra's Algorithm
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { }
V-S = { }
s
![Page 243: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/243.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { }
V-S = { }
s is the start vertex
s
![Page 244: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/244.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { }
V-S = { }
Set S will contain the vertices for which we have computed the shortest distance
s
![Page 245: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/245.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = {}
V-S = { }
Set V-S will contain the vertices that we still
need to process
s
![Page 246: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/246.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { }
V-S = { }
v will contain the list of vertices not including s
s
![Page 247: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/247.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { }
V-S = { }
d[v] will contain the shortest distance from s to
v
s
![Page 248: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/248.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { }
V-S = { }
p[v] will contain the predecessor of v in the path
from s to v
s
![Page 249: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/249.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { 0 }
V-S = { 1, 2, 3, 4 }
At initialization, the start vertex s is placed in S, and the remaining vertices into V-S
s
![Page 250: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/250.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1
2
3
4
S = { 0 }
V-S = { 1, 2, 3, 4 }
For each v in V-S, we initialize d by setting d[v] equal to the weight of the edge w(s, v) for each
vertex v, adjacent to s
s
![Page 251: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/251.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10
2
3
4
S = { 0 }
V-S = { 1, 2, 3, 4 }
For each v in V-S, we initialize d by setting d[v] equal to the weight of the edge w(s, v) for each
vertex v, adjacent to s
s
![Page 252: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/252.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10
2
3 30
4
S = { 0 }
V-S = { 1, 2, 3, 4 }
For each v in V-S, we initialize d by setting d[v] equal to the weight of the edge w(s, v) for each
vertex v, adjacent to s
s
![Page 253: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/253.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10
2
3 30
4 100
S = { 0 }
V-S = { 1, 2, 3, 4 }
For each v in V-S, we initialize d by setting d[v] equal to the weight of the edge w(s, v) for each
vertex v, adjacent to s
s
![Page 254: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/254.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10
2
3 30
4 100
S = { 0 }
V-S = { 1, 2, 3, 4 }
For each v not adjacent to s, we set d[v] equal to ∞
s
![Page 255: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/255.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10
2 ∞
3 30
4 100
S = { 0 }
V-S = { 1, 2, 3, 4 }
For each v not adjacent to s, we set d[v] equal to ∞
s
![Page 256: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/256.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10
2 ∞
3 30
4 100
S = { 0 }
V-S = { 1, 2, 3, 4 }
We initialize each p[v] to s
s
![Page 257: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/257.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 ∞ 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
We initialize each p[v] to s
s
![Page 258: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/258.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 ∞ 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
We now find the vertex u in V-S that has the smallest value of d[u]
s
![Page 259: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/259.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 ∞ 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
We now find the vertex u in V-S that has the smallest value of d[u]
u = 1 u
s
![Page 260: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/260.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 ∞ 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
Consider the vertices v that are adjacent to u
u = 1 u
s
![Page 261: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/261.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 ∞ 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
If the distance from s to u (d[u]) plus the distance from u to v is smaller than d[v] we update d[v] to that
value
u = 1 u
v
s
![Page 262: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/262.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 ∞ 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
If the distance from s to u (d[u]) plus the distance from u to v is smaller than d[v] we update d[v] to that
value
u = 1
60 < ∞
v
u
s
![Page 263: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/263.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 0
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
If the distance from s to u (d[u]) plus the distance from u to v is smaller than d[v] we update d[v] to that
value
u = 1
60 < ∞
v
u
s
![Page 264: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/264.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 1
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
and set p[v] to u
u = 1
v
u
s
![Page 265: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/265.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 1
3 30 0
4 100 0
S = { 0 }
V-S = { 1, 2, 3, 4 }
Remove u from V-S and place it in S
u = 1
v
u
s
![Page 266: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/266.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 1
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
Repeat until V-S is empty
u = 1
s
![Page 267: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/267.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 1
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
The smallest d[v] in V-S is vertex 3
u = 3
u
s
![Page 268: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/268.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 1
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
The distance from s to u plus the distance from u to v is 50
u = 3
v u
s
![Page 269: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/269.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 60 1
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
50 < d[2] (which is 60)
u = 3
v u
s
![Page 270: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/270.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
Set d[2] to 50 and p[2] to u
u = 3
v u
s
![Page 271: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/271.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
Continue to the next adjacent vertex
u = 3
v u
s
![Page 272: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/272.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
Continue to the next adjacent vertex
u = 3 v
u
s
![Page 273: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/273.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 100 0
S = { 0, 1 }
V-S = { 2, 3, 4 }
90 < 100
u = 3 v
u
s
![Page 274: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/274.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1 }
V-S = { 2, 3, 4 }
90 < 100
u = 3 v
u
s
![Page 275: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/275.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1 }
V-S = { 2, 3, 4 }
Move u from V-S to S
u = 3 v
u
s
![Page 276: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/276.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1, 3 }
V-S = { 2, 4 }
Move u from V-S to S
u = 3 v
u
s
![Page 277: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/277.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1, 3 }
V-S = { 2, 4 }
Select vertex 2 from V-S
u = 2 v
u
s
![Page 278: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/278.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1, 3 }
V-S = { 2, 4 }
d[2] + w(2,4) = 50 + 10 = 60
u = 2 v
u
s
![Page 279: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/279.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1, 3 }
V-S = { 2, 4 }
60 < 90 (d[4])
u = 2 v
u
s
![Page 280: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/280.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 90 3
S = { 0, 1, 3 }
V-S = { 2, 4 }
update d[4] to 60 and p[4] to 2
u = 2 v
u
s
![Page 281: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/281.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3 }
V-S = { 2, 4 }
update d[4] to 60 and p[4] to 2
u = 2 v
u
s
![Page 282: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/282.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3 }
V-S = { 2, 4 }
Remove 2 from V-S
u = 2 v
u
s
![Page 283: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/283.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3, 2 }
V-S = { 4 }
Remove 2 from V-S
u = 2 v
u
s
![Page 284: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/284.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3, 2 }
V-S = { 4 }
The final vertex in V-S is 4
u = 2 v
u
s
![Page 285: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/285.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3, 2 }
V-S = { 4 }
The final vertex in V-S is 4
u = 2 v
u
s
![Page 286: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/286.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3, 2 }
V-S = { 4 }
4 has no adjacent vertices; we move 4 into S
u = 2 v
u
s
![Page 287: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/287.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3, 2, 4 }
V-S = { }
4 has no adjacent vertices; we move 4 into S
u = 2 v
u
s
![Page 288: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/288.jpg)
Dijkstra's Algorithm (cont.)
0
1 4
2 3
10
1050
20
60
30
100
v d[v] p[v]
1 10 0
2 50 3
3 30 0
4 60 2
S = { 0, 1, 3, 2, 4 }
V-S = { }
We are finished
u = 2 v
u
s
![Page 289: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/289.jpg)
Minimum Spanning Trees
A spanning tree is a subset of the edges of a graph such that there is only one edge between any two vertices, and all of the vertices are connected
If we have a spanning tree for a graph, then we can access all the vertices of the graph from the start node
The cost of a spanning tree is the sum of the weights of the edges
We want to find the minimum spanning tree or the spanning tree with the smallest cost
![Page 290: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/290.jpg)
Minimum Spanning Trees (cont.)
If we want to start up our own long-distance phone company and need to connect the cities shown below, finding the minimum spanning tree would allow us to build the cheapest network
The solution to this problem was formulated by R.C. Prim and is very similar to Dijkstra’s algorithm
320
130
180
150
180
180 120
148
26040
50
60
155
120
Chicago
Indianapolis Columbus
FortWayne
Ann ArborDetroit
Toledo
Cleveland Pittsburgh
Philadelphia
![Page 291: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/291.jpg)
Prim's Algorithm Example0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0 }
V-S = { 1, 2, 3, 4, 5 }
0
1 3
4 5
2
![Page 292: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/292.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0 }
V-S = { 1, 2, 3, 4, 5 }
0
1 3
4 5
2
The smallest edge from u to v where u is in Sand v is in V-S is the
edge (0,2)
![Page 293: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/293.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0 }
V-S = { 1, 2, 3, 4, 5 }
0
1 3
4 5
2
Add this edge to the spanning tree
1
![Page 294: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/294.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0 }
V-S = { 1, 2, 3, 4, 5 }
0
1 3
4 5
2and move 2 to S
1
![Page 295: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/295.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2 }
V-S = { 1, 3, 4, 5 }
0
1 3
4 5
2and move 2 to S
1
![Page 296: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/296.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2 }
V-S = { 1, 3, 4, 5 }
0
1 3
4 5
2
Consider all edges (u, v) where u is in S and v is
in V-S(there are 8 possible
edges)
1
![Page 297: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/297.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2 }
V-S = { 1, 3, 4, 5 }
0
1 3
4 5
2The smallest is (2, 5)
1
![Page 298: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/298.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2 }
V-S = { 1, 3, 4, 5 }
0
1 3
4 5
2
Add (2,5) to the spanning tree
1
4
![Page 299: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/299.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2 }
V-S = { 1, 3, 4, 5 }
0
1 3
4 5
2Move 5 from V-S to S
1
4
![Page 300: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/300.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5 }
V-S = { 1, 3, 4 }
0
1 3
4 5
2Move 5 from V-S to S
1
4
![Page 301: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/301.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5 }
V-S = { 1, 3, 4 }
0
1 3
4 5
2
1
4
Find the smallest edge (u, v) where u is in S and
v is in V-S
![Page 302: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/302.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5 }
V-S = { 1, 3, 4 }
0
1 3
4 5
2
1
4
The smallest edge is (5, 3)
![Page 303: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/303.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5 }
V-S = { 1, 3, 4 }
0
1 3
4 5
2
1
4
The smallest edge is (5, 3)
2
![Page 304: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/304.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5 }
V-S = { 1, 3, 4 }
0
1 3
4 5
2
1
4
Move 3 to S
2
![Page 305: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/305.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3 }
V-S = { 1, 4 }
0
1 3
4 5
2
1
4
Move 3 to S
2
![Page 306: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/306.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3 }
V-S = { 1, 4 }
0
1 3
4 5
2
1
4
The next smallest edge is (2, 1)
2
![Page 307: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/307.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3 }
V-S = { 1, 4 }
0
1 3
4 5
2
1
4
The next smallest edge is (2, 1)
2
5
![Page 308: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/308.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3 }
V-S = { 1, 4 }
0
1 3
4 5
2
1
4
Move 1 to S
2
5
![Page 309: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/309.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3, 1 }
V-S = { 4 }
0
1 3
4 5
2
1
4
Move 1 to S
2
5
![Page 310: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/310.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3, 1 }
V-S = { 4 }
0
1 3
4 5
2
1
4
The smallest edge to 4 is (1, 4)
2
5
![Page 311: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/311.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3, 1 }
V-S = { 4 }
0
1 3
4 5
2
1
4
The smallest edge to 4 is (1, 4)
2
5
3
![Page 312: Self-Balancing Search Trees - Kennesaw State …ksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/...Red-Black Trees (cont.) Height is determined by counting only black nodes A Red-Black](https://reader036.vdocuments.net/reader036/viewer/2022062402/5fcade0a3a6ef516587f1093/html5/thumbnails/312.jpg)
Prim's Algorithm Example (cont.)0
1 3
4 5
6
5
3
5
2
15
2
6 4
5
S = { 0, 2, 5, 3, 1, 4 }
V-S = { }
0
1 3
4 5
2
1
4
The spanning tree is complete
2
5
3