TL;DR
The hierarchical data model organises records as a tree. Each record type has one parent type, except the root, and each record has exactly one parent record. To find a piece of data you start at the root and follow the links down. The model is fast for questions that follow the tree, and awkward for questions that cut across it.
Where you'll see it: in the design of early mainframe databases such as IBM's IMS, in the way file systems nest directories, and in XML and JSON documents, which are tree-shaped in the same way.
Intuition
Think of a company's filing cabinet. The top drawer holds a folder for each customer. Inside a customer's folder are that customer's orders, and inside each order are its line items. Nothing sits in two places at once. If you want the line items for one order, you open the customer folder, find the order, and look inside. The structure of the cabinet is the structure of the data.
flowchart TD
R[Root: Customer Ann] --> O1[Order 101]
R --> O2[Order 102]
O1 --> L1[Line item: pen]
O1 --> L2[Line item: paper]
O2 --> L3[Line item: ink]
Each box is a record and each arrow is a parent-child link. The layout makes some questions easy. "All orders of Ann" is a single step down from the root. "All line items of order 102" is a walk down two levels. Questions that ask for the data in a different shape, such as "all customers who bought ink", have no direct route, because the tree was never organised around ink.
The model exists because early databases had to be fast on hardware with slow storage and little memory. A parent record could store pointers to its children directly, so reading a tree of related records took a handful of disk reads with no searching. The price of that speed is rigidity, and the rest of this lesson is about where the rigidity bites.
The math
A hierarchical database is a rooted tree. Let be the tree, where is the set of records and the set of parent-child links. Every record except the root has exactly one parent, and there are no cycles.
A hierarchical schema is a rooted tree of record types. Each node is a record type with a set of fields. Each edge goes from a parent type to a child type, and the relation is one-to-many: one parent record has zero or more child records of that type, and each child record has exactly one parent record.
Write for the parent of a record . The defining constraints are
Because every record has one parent, a record can be reached by a unique path from the root. If the path has links, the depth of the record is . A lookup that starts at the root and follows child pointers down to a record at depth takes steps.
Relation to binary trees
A binary tree from Binary Tree is a special case: every node has at most two children. The hierarchical model allows any number of children per parent, so a parent can have child records for any . What both share is the rule that each node has exactly one parent. A binary search tree adds an ordering on keys so that a lookup can discard half the subtree at each step. The hierarchical model does not need that ordering, because the path to a record is given by how the record was attached.
Cost of a path query
Suppose a query asks for all records of one type under a given ancestor, and the records are stored with child pointers. If the ancestor has children of the relevant type, the work is roughly the depth to reach the ancestor plus one step for each child examined. That is pointer follows, which is cheap when the query follows the tree. A query that does not follow the tree, such as "find every record with field value anywhere in the tree", has to visit all records in the worst case, giving work with no index.
Many-to-many relations do not fit
Take students and courses, where each student takes several courses and each course has several students. Because each record has one parent, a tree must choose one side as the root. If students are the parents, each course record must be copied under every student who takes it. The number of stored course records becomes the number of enrolments, not the number of distinct courses. This duplication is the main structural cost of the model.
Worked examples
Use the tree from the diagram, with Ann as the root.
- Start at the root, record Ann. Her children are the orders .
- To list the line items of order 101, go from the root to order 101 (one step) and then to each of its children (two steps). The items are pen and paper.
- To list all line items for Ann, visit each order and then its line items. The walk visits 1 root, 2 orders and 3 line items, a total of 6 records.
- To find every customer who bought ink, the model has no direct route. The only way is to visit every customer and every order and check each item. With records in total, this is work, and the answer comes from a scan, not from the structure.
Interpretation: the tree is efficient for "given a customer, get their orders and items" and inefficient for "given an item, find the customers". The query shape decides which is cheap.
Illustrative numbers. There are 100 students and 20 courses. Each student takes 5 courses.
- Enrolments: pairs of student and course.
- Rooted at students, each enrolment stores a copy of the course record. The number of stored course records is , although there are only 20 distinct courses. Each course is stored, on average, times.
- Rooted at courses, each enrolment stores a copy of the student record instead. That is again 500 stored student records, for 100 distinct students.
- Storing courses once and linking enrolments explicitly, as in the relational model, needs 20 course records, 100 student records and 500 link rows, a total of 620 records with no copies.
Interpretation: for this data, the tree forces either heavy duplication or a second structure, such as pointer chains between parent-child copies, to represent the relation. The duplicated copies must be kept consistent when a course is renamed, which is a common source of errors in hierarchical systems.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Data is naturally one-to-many and queries follow the tree | The model is fast and simple here | Use a hierarchical store, or a document database with embedded children |
| Many-to-many relations are common | Records are duplicated, and updates must touch every copy | Use a relational model with link tables, or a graph model |
| Queries ask for records by a value deep in the tree | Full scans, unless secondary indexes are added | Index the fields, or use a model designed for value lookup |
| The schema changes often | Moving a record type to a new parent means rebuilding the tree | Relational schemas and migrations handle reshaping more easily |
| Records must be shared across branches | A shared record needs a second parent, which the model forbids | Store the shared record once, referenced by key, which is what relational foreign keys do |
The model fails by making some questions cheap and others expensive, and by making the application depend on the shape of the tree. Changing the question often means changing the tree.
History
Edgar F. Codd, who worked at IBM, published the relational model in 1970, in a paper titled A Relational Model of Data for Large Shared Data Banks. Codd argued that applications should not depend on how data is physically linked, and that a model based on relations and keys gives that independence.
The hierarchical model came first in practice. IBM's Information Management System, IMS, was developed in the mid-1960s for the Apollo programme with North American Aviation, and it became one of the most widely used database systems on IBM mainframes. Its data was organised in segments arranged in a tree, and programs navigated the tree through a procedural interface. The model caught on because it matched the hardware of the time, where pointers and sequential storage made tree traversal fast, and because it matched the way many business records are naturally nested. Its weaknesses became clearer as data grew more connected, and most new systems after the 1980s used relational or other models instead.
Check yourself
QuestionIn a hierarchical database, how many parents can a record have, and why does that restriction make a path to the record unique?Show answer
Exactly one parent, except the root, which has none. Because each record has one parent, following parent links upward from any record is deterministic and ends at the root, so the path from the root down to the record is unique.
QuestionA tree stores 100 students and 20 courses, with each student taking 5 courses, and is rooted at students. How many course records are stored, and how many distinct courses are there?Show answer
Each of the enrolments stores a copy of its course record, so 500 course records are stored. There are only 20 distinct courses, so each course is stored on average times.
QuestionWhy is a query for all customers who bought a particular item expensive in a hierarchical database that is organised by customer, and what would make it cheap?Show answer
The tree is organised by customer, so the item is not reachable by a path from the root. Without an index, the query must visit every customer and order, which is work. An index on the item field, or a second structure organised by item, would make the lookup cheap.
QuestionCompare how a binary search tree and a hierarchical database each find one record. Which property of the binary search tree is absent from the hierarchical model, and what does the hierarchical model use instead?Show answer
A binary search tree orders keys so each comparison discards half of the remaining subtree, which gives about steps when balanced. The hierarchical model has no key ordering. It uses the attachment of records to parents: a record is found by following the path from the root to it, which costs one step per level of the tree.
Sources
- Hierarchical database model (Wikipedia): https://en.wikipedia.org/wiki/Hierarchical_database_model
- Edgar F. Codd (Wikipedia): https://en.wikipedia.org/wiki/Edgar_F._Codd
- IBM Information Management System (Wikipedia): https://en.wikipedia.org/wiki/IBM_Information_Management_System