Convex and quasiconvex functions on trees and their applications