Tree ADT
Tree Stores values hierarchically. Each value has one parent value and 0 or more children values. One special value called the root which has no parent value.
Formal definition Tree T is a set of nodes storing values such that the nodes have a parent-child relationship that satisfies the following properties: If T is nonempty, it has a special node, called the root of T, that has no parent. Each node v of T different from the root has a unique parent node w; every node with parent w is a child of w.
Recursive definition Recursive definition of a Tree T Tree T is either empty, no nodes, or not. If T is not empty, it has a node r, the root of T, and a set (possibly empty) of subtrees whose roots are the children of r.
Tree terms Two nodes with the same parent are siblings. A node v is external if it has no children, also known as a leaf node. A node v is internal if it has one or more children. An edge of a tree is a pair of nodes (u, v) such that u is the parent of v. or vice versa. A path from node n 1 to node n k is a sequence of nodes n 1, n 2,, n k such that n i is the parent of n i+1, for 1 i < k. The length of this path is the number edges on the path, namely k-1.
Tree terms For any node v, the depth of v is the length of the unique path from the root to v. The root node r has depth 0. For any node v, the height of v is the length of the longest path from v to a leaf node. All leaf nodes have height 0. The height of the tree is the height of the root node r. The depth of the tree is the depth of the deepest leaf node; this is always equal to the height of the tree. If there is a path from node u to node v, then u is an ancestor of v and v is a descendant of u if u v then u is a proper ancestor of v and v is a proper descendent of u.
General Tree Implementation public class Tree<AnyType> { private static class Node<AnyType> } { private AnyType data; private Node<AnyType> parent; private List<Node<AnyType>> children; } public Node(AnyType newvalue) { data = newvalue; parent = null; children = new List<Node<AnyType>>(); } private Node<AnyType> root; public Tree() { root = null; } public Tree(AnyType newvalue) { root = new Node<AnyType>(newValue); }
Binary Tree A binary tree is an ordered tree with the following properties: 1. Every node has at most two children. 2. Each child node is labeled as being either a left child or a right child. 3. A left child precedes a right child in the order of children of a node.
Binary Tree Recursive Definition An empty tree. A nonempty tree having root node r, which stores a value, and two binary trees that are respectively the left and right subtrees of r. We note that one or both of those subtrees can be empty by this definition.
Binary Tree Properties Left subtree subtree rooted at a left child of an internal node v. Right subtree subtree rooted at a right child of an internal node v. A binary tree is proper (full binary tree) if each node has either zero or two children. Every internal node has exactly two children. A binary tree that is not proper is improper.
Binary Tree Implementation Code is given via a link on the PowerPoints webpage.
Binary Search Tree 1. S is a set of unique elements having an order relation. 2. Binary search tree T is a binary tree such that, for each internal node p of T: Node p stores an element of S, denoted as e. Elements stored in the left subtree of p (if any) are less than e. Elements stored in the right subtree of p (if any) are greater than e.
Binary Search Tree Operations Contains search for a value. FindMin return the smallest value in the tree. FindMax return the largest value in the tree. Insert add a value in its correct position in the tree. Remove remove a value from the tree assuming the value exists in the tree.
Binary Search Tree Insert and Delete Insertion Always occurs at a leaf node. Deletion If the node p to delete is a leaf node, then deletion of node p is simple. Otherwise, we have two cases: 1. If only one child of node p is not null, then deletion of node p is simple. Move the child of node p up the tree to take the place of node p. 2. If node p has two children, the deletion of node p is more complex. Search for the smallest value in the right subtree of node p, call this node r. Replace the value of node p with the value of node r. Then, delete node r. Deletion of node r will be the simpler method of a node with only one child. Node r will not have a left child.
Comparable Interface public interface Comparable<AnyType> { int compareto(anytype a); }
Binary Search Tree Implementation Code is given via a link on the PowerPoints webpage.
Binary Search Tree Typical runtime of search, insertion and deletion is O(log n) if the values inserted into the tree are in a random unsorted order. Worst case runtime of search, insertion and deletion is O(n) for a tree of n values when the values are inserted into the tree in increasing or decreasing order. An inorder traversal of a binary search tree visits each element in nondecreasing order.
AVL Trees To guarantee that the worst case runtime remains O(log n) for the five operations contains, findmin, findmax, insert and remove; balancing is performed on the binary search tree to keep the height of the tree O(log n). Height-Balance Property: For every internal node p of tree T, the heights of the children of p differ by at most 1. A tree that satisfies this property is known as an AVL tree. Named after the initials of its inventors: Adel son-vel skii and Landis.
Maintaining Balance The operations insert and remove can potentially unbalance an AVL tree. Rotation is a simple modification to the AVL tree to rebalance it.
Rebalancing after Insertion Node p is the value inserted into the tree. Recalculate the height of each node up the tree from node p to the root node. x is the closest ancestor node of p which violates the height-balance property. The height imbalance of the two subtrees of x differ by 2.
Rebalancing after Insertion Four cases where the height violation might occur: 1. An insertion into the left subtree of the left child of x. 2. An insertion into the right subtree of the left child of x. 3. An insertion into the left subtree of the right child of x. 4. An insertion into the right subtree of the right child of x. Cases 1 and 4 are symmetric. Cases 2 and 3 are symmetric.
Cases 1 and 4 1. An insertion into the left subtree of the left child of x. 4. An insertion into the right subtree of the right child of x. Require just a single rotation to rebalance the subtree rooted at x.
Cases 2 and 3 2. An insertion into the right subtree of the left child of x. 3. An insertion into the left subtree of the right child of x. Require a double rotation to rebalance the subtree rooted at x.
Rebalancing after Deletion Node p is the value deleted from the tree. Recalculate the height of each node up the tree from position p or the parent of p if p was a leaf node. x is the closest ancestor node of p which violates the height-balance property. The height imbalance of the two subtrees of x differ by 2.
Rebalancing after Deletion Four cases where the height violation might occur: 1. A deletion from the right child of x, where the height of the left subtree of the left child of x is 1 more or equal to the height of the right subtree of the left child of x. 2. An deletion from the right child of x, where the height of the left subtree of the left child of x is 1 less than the height of the right subtree of the left child of x. 3. An deletion from the right child of x, where the height of the right subtree of the left child of x is 1 less than the height of the left subtree of the left child of x. 4. An deletion from the left child of x, where the height of the right subtree of the right child of x is 1 more or equal to the height of the left subtree of the right child of x.
Rebalancing after Deletion Cases 1 and 4 are symmetric. 1. A deletion from the right child of x, where the height of the left subtree of the left child of x is 1 more or equal to the height of the right subtree of the left child of x. 2. An deletion from the left child of x, where the height of the right subtree of the right child of x is 1 more or equal to the height of the left subtree of the right child of x. Require just a single rotation to rebalance the subtree rooted at x.
Rebalancing after Deletion Cases 2 and 3 are symmetric. 1. An deletion from the right child of x, where the height of the left subtree of the left child of x is 1 less than the height of the right subtree of the left child of x. 2. An deletion from the right child of x, where the height of the right subtree of the left child of x is 1 less than the height of the left subtree of the left child of x. Require a double rotation to rebalance the subtree rooted at x.
AVL Tree Implementation Code is given via a link on the PowerPoints webpage.