Tree Definitions
- Trees give us a way to maintain O(lg n) access time on
large amounts of data.
- They also give us a way to maintain a parent-child relationship among
data.
- We will do an overview of trees
- Some Terms:
- A tree is a collection on nodes, one of
which is the root and all others are members of sub trees
of this root. A tree can be empty.
- The connection between any two nodes in a tree is called an
edge.
- An edge runs from a parent to a child node.
- A node with no children is known as a leaf node.
- All children of the same node ar known as sibling
- A path from node n1 to nk is defined
as a sequence of nodes n1,n2...nk
such that ni is the parent of ni+1
- The length of a path is the number of edges in the path.
- The path from a parent to a child is of length 1
- Each node has a path to it's self of length 0
- The depth of node ni is the length of a path from the root to this node.
- The root has depth 0
- All children of the root have depth 1
- The height of a node is the length of the longest path
from that node to a leaf node.
- If there is a path from n1 to n2 then n1 is an ancestor of n2 and n2 is a descendant of n1
- If the path from n1 to n2 has length of 1 or more, then the relationship is a proper one, or n1 is an proper ancestor of n2
- Some tree applications
- A family tree. Each node represents a preson.
- The relationship between nodes is a real parent-child relationship
- It doesn't make sense to delete a node in the tree.
- Order among siblings is important.
- Inserting is done at any node?
- A file system can be stored as a tree.
- Each reagular file is a leaf node.
- Each internal node is a directory.
- Empty directories are leaf nodes as well.
- Only leaf nodes may be deleted.
- Order among siblings is not important.
- An insert can be performed at any node.
- A binary search tree is an ADT where the binary search tree property must be followed.
- BST Property
- If x is a node in a BST, then
- If y is in the right subtree of x, then x < y
- If y is in the left subtree of x, then x > y
- The relationship among the siblings is defined by the BST property.
- It makes sense to delte any node, but if it is a non-leaf
node it must be replaced by one of its children.
- An insert is only performed at the leaf nodes.
- Notice insert, delete are different depending on the application.
- ADT Tree:
- Domain: homogenious data where there is a parent-child relationship among the data.
- Operations:
- root(), returns an iterator to the root node of a tree.
- element(i), returns the element at iterator i
- parent(i), child(i), returns an iterator to parent/child
- error for root or leaf nodes respectively
- Children accessors:
- children(i) - iterator to traverse children
- firstChild(i) - iterator to first child of i
- rightSibling(i) - iterator to right sibling of i
- leftSibling(i) -
- IsInternal(), IsLeaf(), IsRoot()
- depth(v), height(v),
- Others depending on the implementation.
- elements() - return an iterator over all elements
- positions() - returns an iterator over all positions.