Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Tutorial “Trees”, CMPT 115 Created by Svetlana Slavova, July 2006 Trees 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 algorithms. 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. 1 Tutorial “Trees”, CMPT 115 Created by Svetlana Slavova, July 2006 Figure 1. A binary search tree 2 Tutorial “Trees”, CMPT 115 Created by Svetlana Slavova, July 2006 2. Example /** * An example of a binary search tree. 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; public Tree() { root = null; } /** * Inserts a new node into the tree */ public void insert(Comparable object) { //Create a new node Node newNode = new Node(); newNode.data = object; newNode.left = null; newNode.right = null; //Insert the new node in the tree if (root == null) root = newNode; else root.insertNode(newNode); } /** * Prints the content of the tree in sorted order */ public void print() { //Print the tree if (root != null) root.printNodes(); } private class Node { /** * Comparable is an interface that can be used for ordering objects. * It provides a method called compareTo that compares this object with the specified object for order. */ public Comparable data; //The left child of the node public Node left; //The right child of the node public Node right; 3 Tutorial “Trees”, CMPT 115 Created by Svetlana Slavova, July 2006 /** * Inserts a new node as a descendant of this node */ public void insertNode(Node newNode) { //CompareTo returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object if (newNode.data.compareTo(data) < 0) { //Negative => To insert in the left if (left == null) left = newNode; else left.insertNode(newNode); } else { //To insert in the right if (right == null) right = newNode; else right.insertNode(newNode); } } /** * Prints this node and all of its descendants. * 1. Prints the left subtree * 2. Print the data (the node) * 3. Print the right subtree */ public void printNodes() { if (left != null) left.printNodes(); System.out.println(data); if (right != null) right.printNodes(); } } } 4