What Is A Tree
What Is A Tree
Non-linear data structure Hierarchical arrangement of data Has components named after natural trees root branches leaves Drawn with root at the top Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen Components of a Tree Node: stores a data element Parent: single node that directly precedes a node all nodes have 1 parent except root (has 0) Child: one or more nodes that directly follow a node Ancestor: any node which precedes a node itself, its parent, or an ancestor of its parent Descendent: any node which follows a node itself, its child, or a descendent of its child Johns Hopkins Department of Computer Science Course 600.226: Data Structures, Professor: Jonathan Cohen More Tree Terminology Leaf (external) node: node with no children Internal node: non-leaf node Siblings: nodes which share same parent Subtree: a node and all its descendents Ignoring the nodes parent, this is itself a tree Ordered tree: tree with defined order of children enables ordered traversal Binary tree: ordered tree with up to two children per node Examples of Trees Directory tree Organizes directories and files hierarchically
Directories are internal nodes, files are leaf nodes (usually) Class hierarchy Object is root, other classes are descendents Decision tree Binary tree Path taken determined by boolean expression Expression tree Operators are internal nodes, variables and constants are leaf nodes