Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
180 CHAPTER 8: OTHER MODELS FOR TREES LEFT OUTER JOIN PostorderTree AS T3 ON T3.lvl = T2.lvl AND T3.postorder_nbr < T2.postorder_nbr AND T3.postorder_nbr > T1.postorder_nbr WHERE T3.postorder_nbr IS NULL; We can then write some of the standard queries easily. Using the preorder tree, get all ancestors of a given node. SELECT * FROM PreorderRelationships WHERE descendant = :my_guy; Using postorder, get all descendants of C SELECT * FROM PostorderRelationships WHERE ancestor = :my_ancestor; 8.4 Path Enumeration Using Prime Number Products This model is credited to P Thomas Roji. It uses a prime number table and . basic mathematical operations to do basic hierarchy operations. Having said that, the disadvantage is that the product of prime numbers gets big very fast. This model depends on two mathematical properties. 1. 2. There is a unique path to every node in a tree from the root node. The prime numbers that can be a divisor of the product of a set of prime numbers are only the prime numbers in the set. That is, let the product of prime numbers be (p(n)), then the factors of (p(n)) can only be the prime numbers, p(n) that participated in the multi- plication, or the subproducts. Maple and other mathematical software tools have a built-in function for finding the ith prime. We are not so lucky in SQL and will use a one column table of primes, PrimeNumbers(prime). The following is the table used in the examples. CREATE TABLE Personnel_Orgchart (emp_name VARCHAR(15) NOT NULL PRIMARY KEY, node_prime BIGINT NOT NULL REFERENCES PrimeNumbers(prime, path_product BIGINT NOT NULL);