This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Trees - Trees - Combining singletonsCombining singletons
A singleton tree, S, and one or more other non-empty trees can be combined into a new tree by creating a binary relation between S and each of the other trees.– Each binary relation is called an edge.– The root node from the singleton tree is called
the root node of the new tree. The root nodes of the other trees are called non-root nodes in the new tree.
Trees - Trees - forests,disjoint trees and subtreesforests,disjoint trees and subtrees
A forest is a set of trees. Two trees are disjoint if they share no nodes and no edges. A tree T1 is a subtree of a tree T2 if all of the nodes and edges of T1 are also in
Trees - Trees - node relationshipsnode relationships
The root node of a tree is an ancestor of all nodes in that tree.
The parent of a node is an ancestor that is connected to it by an edge.
If node N1 is a parent of node N2, then N2 is a child of N1.
If node N1 is an ancestor of node N2, then N2 is a descendant of N1.
The root node has no parent. A node with no children is called a leaf node. A node with children is called an interior node. Two nodes are siblings if they have the same parent.
Trees - Trees - node relationships examplenode relationships example
The ancestors of node 21 are nodes: 43 and 10. The parent of node 21 is node 43. The children of node 43 are nodes: 21, 17 and 67. The descendants of node 43 are nodes: 21, 17, 67,
15, 11, 88 and 99.
10
55 43
1721 67
25
13 90
15
11 88
The leaf nodes are: 55, 21, 11, 88, 99, 13 and 90.
The interior nodes are: 10, 43, 25, 17, 67 and 15.
Measuring Trees - ExampleMeasuring Trees - Example The path from node 88 to 43 is highlighted in red. The length of the path from node 88 to node 43 is 3. The height of node 43 is 3. The height of the tree is the height of node 10: 4.
10
55 43
1721 67
25
13 90
15
11 88
The depth (level) of node 67 is the length of the path from node 10 to node 67 which is 2.
The degree of node 43 is 3 and the degree of node 25 is 2 and the degree of node 11 is 0.
Binary TreesBinary Trees A binary tree is a tree with arity 2. A binary tree is oriented if every node with 2 children differentiates between the children
by calling them the left child and right child and every node with one child designates it either as a left child or right child.
A node in a binary tree is full if it has arity 2. A full binary tree of height h has leaves only on level h and each of its interior nodes is
full. A complete binary tree of height h is a full binary tree of height h with 0 or more of the
Interface for Binary TreesInterface for Binary Trees The structure package does not define a Java
Interface for BinaryTree.
Instead it defines a BinaryTree class, just as it defines a Vector class.
The BinaryTree class defines the three methods that are in the Store Interface (size, isEmpty, clear), but it is not defined as an implementor of this Interface.
The BinaryTree class does not define the three methods that are in the Collection Interface so BinaryTrees are not Collections (add, remove, contains).
Binary Tree Class - Binary Tree Class - interface 1interface 1
public class BinaryTree {
public BinaryTree ();//post: initializes an empty Binary Tree.
public void clear () {// post: removes all nodes from the tree.
public void insert (Object anObject) {// pre: cursor is invalid and not off the top of a non-empty tree// post: if tree is empty, object is inserted at root. Otherwise,
// object is inserted where cursor last moved off tree.
public Object remove () {// pre: cursor is valid and node has no children.// post: leaf is removed; cursor moved to parent, if any// and object is returned.
BinaryTree Example -BinaryTree Example - evaluateTree() 2evaluateTree() 2
while (iterator.hasMoreElements()) { element = iterator.nextElement(); System.out.println("Element is: " + element); if (element instanceof Operator) {
right = (Integer) stack.pop(); // get right operand
left = (Integer) stack.pop(); // get left operand
result = ((Operator) element).apply(left, right);stack.push(result);System.out.println("result = " + result);
public Integer apply(Integer left, Integer right) {// post: returns the Integer result of applying the // operator to the two Integers.
if (this.name.equals("+")) return new Integer(left.intValue() + right.intValue());else if (this.name.equals("-")) return new Integer(left.intValue() - right.intValue());else if (this.name.equals("*")) return new Integer(left.intValue() * right.intValue());else // divide return new Integer(left.intValue() / right.intValue());
BinaryTree Example - BinaryTree Example - outputoutput
<BinaryTree: (/ (* (+ 2 3) 4) (- 6 1))>Element is: 2Element is: 3Element is: +result = 5Element is: 4Element is: *result = 20Element is: 6Element is: 1Element is: -result = 5Element is: /result = 44
BinaryTreeNode -BinaryTreeNode - Constructor and AccessConstructor and Access
public BinaryTreeNode(Object anObject, left BinaryTreeNode, right BinaryTreeNode) {// post: initializes the BinaryTreeNode to hold the given// object and to have the given Nodes as sub-trees.
Binary Tree - Binary Tree - state and constructorstate and constructor
public class BinaryTree {protected BinaryTreeNode root;protected BinaryTreeNode cursor;protected BinaryTreeNode prior;protected boolean wentLeft;protected int size;
public BinaryTree();//post: initializes an empty Binary Tree.
Binary Tree - Binary Tree - move methodsmove methods
public void moveLeft() {// pre: cursor is valid// post: moves the cursor to the left child of the current// node or off the tree if no left child exists.
Binary Tree - Binary Tree - insert(Object)insert(Object)
public void insert(Object anObject) {// pre: cursor is invalid and not off top of a non-empty tree// post: if tree is empty, object is inserted at root// Otherwise, object is inserted where cursor last moved off
Assert.pre(this.cursor == null, “Insertion does not overwrite node”);if (this.prior == null){
Assert.pre(this.root == null, “Insertion at root only in empty tree”);this.root = new BinaryTreeNode(anObject);this.cursor = this.root;}
else {this.cursor = new BinaryTreeNode(anObject);if (this.wentLeft) { this.prior.setLeft(this.cursor);else this.prior.setRight(this.cursor);
public Object remove() {// pre: cursor is valid and node has no children.// post: leaf is removed; cursor moved to parent, if any// and object is returned
Object current;Assert.pre(this.cursor != null, “Node to be removed exists”);Assert.pre(!(this.hasLeft() || this.hasRight()),
“Node to be removed is a leaf”);current = cursor.value();if (this.isLeftChild()) {
There are four common binary tree traversals:– Preorder: visit node then left then right– Inorder: left then visit node then right– Postorder: left then right then visit node– Levelorder: visit nodes of level i, before visiting
old = (BinaryTreeNode) this.todo.pop();result = old.value();current = old.right();while (current != null) { //push right node and all its this.todo.push(current) //left children on the stack current = current.left();}return result;
BinaryTreeNode - BinaryTreeNode - static methods 1static methods 1public static BinaryTreeNode root(BinaryTreeNode aNode) {//post: returns the root of the tree that contains the given node.