Tutorial “Trees”, CMPT 115

Created by Svetlana Slavova, July 2006


1. What is a tree

• The tree is a fundamental structure. The operating systems store files in trees
or treelike structures. Tees are also used in text processing and searching
• A tree consists of a set of nodes and a set of directed edges that connect pairs
of nodes. One node is distinguished as the root. Every node c, except the root,
is connected by an edge from exactly one other node p. Node p is the c’s
parent, and c is one of p’s children.
• Another definition of a tree: Either a tree is empty or consists of a root and
zero or more non-empty sub-trees.
• A unique path traverse from the root to each node. The number of edges that
must be followed is the path length.
• The depth of a node in a tree is the length of the path from the root to the
node. Thus the depth of the root is always 0. The depth of any node is 1 more
than the depth of its parents.
• The height of a node in a tree is the length of the path from the node to the
deepest leaf.
• Nodes with the same parent are called siblings.
• A binary tree consists of nodes, each of which has two child nodes. All nodes
in a binary search tree fulfill the property that the descendants to the left have
smaller data values, than the node data value, and the descendants to the right
have larger data values. Figure 1 shows an example of a binary search tree.


A binary search tree 2 . Tutorial “Trees”. July 2006 Figure 1. CMPT 115 Created by Svetlana Slavova.

CMPT 115 Created by Svetlana Slavova.right = null. July 2006 2. */ public Comparable data. //The right child of the node public Node right. //Insert the new node in the tree if (root == null) root = newNode. Example /** * An example of a binary search tree. newNode. } /** * Inserts a new node into the tree */ public void insert(Comparable object) { //Create a new node Node newNode = new Node(). newNode. //The left child of the node public Node left. 3 . Print out the elements of the tree in sorted order. * (All data in the left sub-tree of any node must come before the node and before all data in the right sub-tree.) */ public class Tree { private Node root. newNode. } /** * Prints the content of the tree in sorted order */ public void print() { //Print the tree if (root != null) root.left = null. else root.insertNode(newNode).printNodes(). * It provides a method called compareTo that compares this object with the specified object for order. Tutorial “Trees”.data = object. public Tree() { root = null. } private class Node { /** * Comparable is an interface that can be used for ordering objects.

zero. CMPT 115 Created by Svetlana Slavova. Print the right subtree */ public void printNodes() { if (left != null) left.printNodes(). } else { //To insert in the right if (right == null) right = newNode. equal to. Prints the left subtree * 2.println(data).insertNode(newNode).printNodes(). or a positive integer as this object is less than. if (right != null) right. Print the data (the node) * 3. else left.data.insertNode(newNode). else right. } } /** * Prints this node and all of its descendants.out. * 1. July 2006 /** * Inserts a new node as a descendant of this node */ public void insertNode(Node newNode) { //CompareTo returns a negative integer. } } } 4 . Tutorial “Trees”. System.compareTo(data) < 0) { //Negative => To insert in the left if (left == null) left = newNode. or greater than the specified object if (newNode.