r = sortFct(newCustomer, customer1) //Result r is < 0, =0, or > 0
int r; r = sortFct.compare(newCustomer, customer1); //Result r is < 0, ==0, or > 0
//Pseudocode r = sortFct(newNode.getData(), customer1.getData()); //r is < 0, =0, or > 0
//Java int r; r = sortFct.compare(newCustomer.getData(), customer1.getData()); //r is < 0, =0, or > 0
r = searchFct(searchKey, customer1) //Result r is < 0, =0, or > 0
int r; r = searchFct.apply(searchKey, customer1); //Result r is < 0, ==0, or > 0
sortFct(elem1, elem2)returns an integer:
Condition | Function Result |
---|---|
if "key" sorts before "elem" | < 0 |
if "key" matches "elem" | = 0 |
if "key" sorts after "elem" | > 0 |
//Step #1 r = sortFct(newCustomer, customer1) //r is < 0, so we continue search left //Step #2 r = searchFct(newCustomer, customer2) //r is > 0, and right is null //Thus, we insert newCustomer to right
1Customer1 (name="Chin") | --------------+-------------- | |2Customer2 Customer3 (name="Asha") (name="Riya") | -----+ | Customer4 (name="Kofi")
Customer1 (name="Chin") | --------------+-------------- | | Customer2 Customer3 (name="Asha") (name="Riya") | | +---- -----+ | | Customer5 Customer4 (name="Awusi") (name="Kofi")
searchFct(key, elem)returns an integer:
Condition | Function Result |
---|---|
if "key" sorts before "elem" | < 0 |
if "key" matches "elem" | = 0 |
if "key" sorts after "elem" | > 0 |
r = searchFct(searchKey, customer1) //r is > 0, so we continue search right r = searchFct(searchKey, customer3) //r is < 0, so we continue search left r = searchFct(searchKey, customer4) //r = 0, so we have match and return customer4
Customer1 (id=20) | --------------+-------------- | | Customer6 Customer3 (id=10) (id=40) | | -------+------- -----+ | | | Customer5 Customer2 Customer4 (id=5) (id=15) (id=30)
r = searchFct(searchKey, customer1) //r is > 0, so we continue search right r = searchFct(searchKey, customer3) //r = 0, so we have match and return customer3
Customer1 (name="Chin") | --------------+-------------- | | Customer2 Customer3 (name="Asha") (name="Riya") | -----+ | Customer4 (name="Kofi")