You are on page 1of 9

Trees

Hierarchical Data Representation


Tree Structure
• Trees are a collection of nodes ordered in a hierarchy.
• A tree has a single node at the top called the root.
• Other nodes branch down from the root
• Terminating Nodes at the bottom are called leaves
• Think of an upside down tree branching from the
root to the leaves at the end of the branches
• Any node in the tree also represents a tree if you
remove it from its parent, this is called a sub-tree
Tree Nodes
• Nodes have a parent that they descend from.
• Nodes can have children descending from them.
• The tree has only one root node that has no parent
• Nodes without children are called leaves
• Nodes contain some type of data and the position of
the node in the tree represents the position of that
data in a hierarchy
Examples of Tree Hierarchies
• Family Tree
• Organization Chart reporting structure
• Computer File Directory Structure (Root is C:/)
• Biological Classifications
Binary Tree
• Special Tree where each node has at most two
children.
• Can be used for sorting objects in the tree
• Objects that are smaller than a node are inserted
into the left child branch
• Objects that are larger than a node are inserted into
the right child branch
• A binary tree maintained in this way is called a Binary
Search Tree
Balanced Binary Tree
• A binary tree is perfectly balanced if each level in the
tree is full and the leaves level has all null children.
• The height (h) of the tree is the number of levels or
the number of nodes in a path from the root to a leaf
• The number of nodes at each level is 2(h-1) and the
total number of nodes in the tree is 2h – 1. This
means that each level in the tree has one more node
than all the levels above it combined.
Balanced Binary Search Tree
• Height = 4 so total nodes = 24 – 1 = 15
• Number leaves at level 4 = 2(4 – 1) = 8
Binary Search Tree
• Insert(key) – Add a new object to the tree
• Delete(key) – Removes an object from the tree
• Search(key) – Looks for the object key in the nodes of
the tree
• Minimum() – Finds the smallest object in the tree
• Maximum() – Finds the largest object in the tree
• Successor(node) – Given a node, finds the node that
comes after it in the tree
• Predecessor(node) – Given a node, finds the node
that comes before it in the tree
Hard work beats talent when talent
doesn’t work hard
-- Tim Notke

You might also like