非原创,稍做总结,保存起来以便日后查看,希望能帮到大家。
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
/**
* A BinaryTree consists of "nodes"--each "node" is itself a BinaryTree. Each
* node has a parent (unless it is the root), may have a left child, and may
* have a right child. This class implements loop-free binary trees, allowing
* shared subtrees.
*
* @author David Matuszek
* @version Jan 25, 2004
* @param <V>
* The type of values contained in this BinaryTree.
*/
public class BinaryTree<V>
{
/**
* The value (data) in this node of the binary tree; may be of any object
* type.
*/
public V value;
private BinaryTree<V> leftChild;
private BinaryTree<V> rightChild;
/**
* Constructor for BinaryTree.
*
* @param value
* The value to be placed in the root.
* @param leftChild
* The left child of the root (may be null).
* @param rightChild
* The right child of the root (may be null).
*/
public BinaryTree(V value, BinaryTree<V> leftChild, BinaryTree<V> rightChild)
{
this.value = value;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
/**
* Constructor for a BinaryTree leaf node (that is, with no children).
*
* @param value
* The value to be placed in the root.
*/
public BinaryTree(V value)
{
this(value, null, null);
}
/**
* Getter method for the value in this BinaryTree node.
*
* @return The value in this node.
*/
public V getValue()
{
return value;
}
/**
* Getter method for left child of this BinaryTree node.
*
* @return The left child (<code>null</code> if no left child).
*/
public BinaryTree<V> getLeftChild()
{
return leftChild;
}
/**
* Getter method for right child of this BinaryTree node.
*
* @return The right child (<code>null</code> if no right child).
*/
public BinaryTree<V> getRightChild()
{
return rightChild;
}
/**
* Sets the left child of this BinaryTree node to be the given subtree. If
* the node previously had a left child, it is discarded. Throws an
* <code>IllegalArgumentException</code> if the operation would cause a loop
* in the binary tree.
*
* @param subtree
* The node to be added as the new left child.
* @throws IllegalArgumentException
* If the operation would cause a loop in the binary tree.
*/
public void setLeftChild(BinaryTree<V> subtree)
throws IllegalArgumentException
{
if (contains(subtree, this))
{
throw new IllegalArgumentException("Subtree " + this
+ " already contains " + subtree);
}
leftChild = subtree;
}
/**
* Sets the right child of this BinaryTree node to be the given subtree. If
* the node previously had a right child, it is discarded. Throws an
* <code>IllegalArgumentException</code> if the operation would cause a loop
* in the binary tree.
*
* @param subtree
* The node to be added as the new right child.
* @throws IllegalArgumentException
* If the operation would cause a loop in the binary tree.
*/
public void setRightChild(BinaryTree<V> subtree)
throws IllegalArgumentException
{
if (contains(subtree, this))
{
throw new IllegalArgumentException("Subtree " + this
+ " already contains " + subtree);
}
rightChild = subtree;
}
/**
* Sets the value in this BinaryTree node.
*
* @param value
* The new value.
*/
public void setValue(V value)
{
this.value = value;
}
/**
* Tests whether this node is a leaf node.
*
* @return <code>true</code> if this BinaryTree node has no children.
*/
public boolean isLeaf()
{
return leftChild == null && rightChild == null;
}
/**
* Tests whether this BinaryTree is equal to the given object. To be
* considered equal, the object must be a BinaryTree, and the two binary
* trees must have equal values in their roots, equal left subtrees, and
* equal right subtrees.
*
* @return <code>true</code> if the binary trees are equal.
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o)
{
if (o == null || !(o instanceof BinaryTree))
{
return false;
}
BinaryTree<?> otherTree = (BinaryTree<?>) o;
return equals(value, otherTree.value)
&& equals(leftChild, otherTree.getLeftChild())
&& equals(rightChild, otherTree.getRightChild());
}
/**
* Tests whether its two arguments are equal. This method simply checks for
* <code>null</code> before calling <code>equals(Object)</code> so as to
* avoid possible <code>NullPointerException</code>s.
*
* @param x
* The first object to be tested.
* @param y
* The second object to be tested.
* @return <code>true</code> if the two objects are equal.
*/
private boolean equals(Object x, Object y)
{
if (x == null)
return y == null;
return x.equals(y);
}
/**
* Tests whether the <code>tree</code> argument contains within itself the
* <code>targetNode</code> argument.
*
* @param tree
* The root of the binary tree to search.
* @param targetNode
* The node to be searched for.
* @return <code>true</code> if the <code>targetNode</code> argument can be
* found within the binary tree rooted at <code>tree</code>.
*/
protected static <T> boolean contains(BinaryTree<T> tree,
BinaryTree<T> targetNode)
{
if (tree == null)
return false;
if (tree == targetNode)
return true;
return contains(tree.getLeftChild(), targetNode)
|| contains(tree.getRightChild(), targetNode);
}
/**
* Returns a String representation of this BinaryTree.
*
* @see java.lang.Object#toString()
* @return A String representation of this BinaryTree.
*/
@Override
public String toString()
{
if (isLeaf())
{
return value.toString();
} else
{
String root, left = "null", right = "null";
root = value.toString();
if (getLeftChild() != null)
{
left = getLeftChild().toString();
}
if (getRightChild() != null)
{
right = getRightChild().toString();
}
return root + " (" + left + ", " + right + ")";
}
}
/**
* Computes a hash code for the complete binary tree rooted at this
* BinaryTree node.
*
* @return A hash code for the binary tree with this root.
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
int result = value.hashCode();
if (leftChild != null)
{
result += 3 * leftChild.hashCode();
}
if (rightChild != null)
{
result += 7 * rightChild.hashCode();
}
return result;
}
/**
* Prints the binary tree rooted at this BinaryTree node.
*/
public void print()
{
print(this, 0);
}
private void print(BinaryTree<V> root, int indent)
{
for (int i = 0; i < indent; i++)
{
System.out.print(" ");
}
if (root == null)
{
System.out.println("null");
return;
}
System.out.println(root.value);
if (root.isLeaf())
return;
print(root.leftChild, indent + 1);
print(root.rightChild, indent + 1);
}
// -------------------------- Methods added for Assignment 3, CIT 594-2008
/**
* Returns the leftmost descendant of this binary tree. That is, return the
* leaf that is the left child of the left child of ... the left child of
* this binary tree. If this binary tree has no left child, return this
* binary tree.
*
* @return The leftmost descendant of this BinaryTree.
*/
public BinaryTree<V> leftmostDescendant()
{
if (this.leftChild == null)
return this;
else
return leftChild.leftmostDescendant();
}
/**
* Returns the rightmost descendant of this binary tree. That is, return the
* leaf that is the right child of the right child of ... the right child of
* this binary tree. If this binary tree has no right child, return this
* binary tree.
*
* @return The rightmost descendant of this BinaryTree.
*/
public BinaryTree<V> rightmostDescendant()
{
if (this.rightChild == null)
return this;
else
return rightChild.rightmostDescendant();
}
/**
* Returns the total number of nodes in this binary tree (include the root
* in the count).
*
* @return The number of nodes in this BinaryTree.
*/
public int numberOfNodes()
{
int leftCount = this.leftChild == null ? 0 : leftChild.numberOfNodes();
int rightCount = this.rightChild == null ? 0 : rightChild
.numberOfNodes();
return 1 + leftCount + rightCount;
}
/**
* Returns the depth of this binary tree. The depth of a binary tree is the
* length of the longest path from this node to a leaf. The depth of a
* binary tree with no descendants (that is, just a leaf) is zero.
*
* @return The depth of this BinaryTree.
*/
public int depth()
{
int leftDepth = this.leftChild == null ? 0 : leftChild.depth() + 1;
int rightDepth = this.rightChild == null ? 0 : rightChild.depth() + 1;
return Math.max(leftDepth, rightDepth);
}
/**
* Returns true if and only if some node in this binary tree contains a
* value that is equal to the parameter.
*
* @param value
* The value to be searched for.
* @return <code>true</code> if this BinaryTree contains an equal value.
*/
public boolean containsEqualValue(V value)
{
if (this.value == null && value == null)
return true;
if (this.value != null && this.value.equals(value))
return true;
if (leftChild != null && leftChild.containsEqualValue(value))
return true;
if (rightChild != null && rightChild.containsEqualValue(value))
return true;
return false;
}
/**
* Returns true if and only if some node in this binary tree contains the
* same object (not just an equal object) as the one given for the value
* parameter.
*
* @param value
* The value to be searched for.
* @return <code>true</code> if this BinaryTree contains the identical
* value.
*/
public boolean containsSameValue(V value)
{
if (this.value == null && value == null)
return true;
if (this.value != null && this.value == value)
return true;
if (leftChild != null && leftChild.containsSameValue(value))
return true;
if (rightChild != null && rightChild.containsSameValue(value))
return true;
return false;
}
/**
* Returns a Set of all the leaves of this binary tree.
*
* @return The leaves of this BinaryTree.
*/
public Set<BinaryTree<V>> leaves()
{
Set<BinaryTree<V>> set = new HashSet<BinaryTree<V>>();
if (this.isLeaf())
{
set.add(this);
}
if (leftChild != null)
{
set.addAll(leftChild.leaves());
}
if (rightChild != null)
{
set.addAll(rightChild.leaves());
}
return set;
}
/**
* Returns a Set of the values (of type V) in this binary tree.
*
* @return The values in this BinaryTree.
*/
public Set<V> valuesOf()
{
Set<V> values = new HashSet<V>();
values.add(this.value);
if (leftChild != null)
{
values.addAll(leftChild.valuesOf());
}
if (rightChild != null)
{
values.addAll(rightChild.valuesOf());
}
return values;
}
/**
* Returns a List of the values (of type V) in the leaves of this binary
* tree, in left-to-right order.
*
* @return The values in the leaves of this BinaryTree.
*/
public List<V> fringe()
{
List<V> values = new ArrayList<V>();
if (this.isLeaf())
{
values.add(this.value);
return values;
}
if (leftChild != null)
{
values.addAll(leftChild.fringe());
}
if (rightChild != null)
{
values.addAll(rightChild.fringe());
}
return values;
}
/**
* Returns a new BinaryTree equal to (but not the same as) this binary tree.
* Every node in this new BinaryTree will be created by the copy method;
* values will be identical (==) to values in the given binary tree.
*
* @return An exact copy of this BinaryTree; the values in the new
* BinaryTree are == to the values in this BinaryTree.
*/
public BinaryTree<V> copy()
{
BinaryTree<V> left = null, right = null;
if (this.leftChild != null)
{
left = this.leftChild.copy();
}
if (this.rightChild != null)
{
right = this.rightChild.copy();
}
return new BinaryTree<V>(this.value, left, right);
}
/**
* Returns a new binary tree which is the mirror image of the binary tree
* whose root is at this binary tree. That is, for every node in the new
* binary tree, its children are in reverse order (left child becomes right
* child, right child becomes left child).
*
* @return A mirror image copy of this BinaryTree; values in the new
* BinaryTree are == to the values in this BinaryTree.
*/
public BinaryTree<V> reverse()
{
BinaryTree<V> left = null, right = null;
if (this.leftChild != null)
{
left = this.leftChild.reverse();
}
if (this.rightChild != null)
{
right = this.rightChild.reverse();
}
return new BinaryTree<V>(this.value, right, left);
}
/**
* Rearranges the binary tree rooted at this binary tree to be the mirror
* image of its original structure. No new BinaryTree nodes are created in
* this process.
*/
public void reverseInPlace()
{
if (this.leftChild != null)
{
leftChild.reverseInPlace();
}
if (this.rightChild != null)
{
rightChild.reverseInPlace();
}
BinaryTree<V> temp = this.leftChild;
this.setLeftChild(this.rightChild);
this.setRightChild(temp);
}
public void preOrderTraverseIterative()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
BinaryTree<V> subTree = this;
while (!stack.empty() || subTree != null)
{
if (subTree != null)
{
stack.push(subTree);
System.out.print(subTree.value + ", ");
subTree = subTree.leftChild;
} else
{
subTree = stack.pop();
subTree = subTree.rightChild;
}
}
}
public void preOrderTraverse()
{
preOrderTraverse(this);
}
private void preOrderTraverse(BinaryTree<V> subTree)
{
if (subTree != null)
{
System.out.print(subTree.value + ", ");
preOrderTraverse(subTree.leftChild);
preOrderTraverse(subTree.rightChild);
}
}
public void inOrderTraverseIterative()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
BinaryTree<V> subTree = this;
while (!stack.empty() || subTree != null)
{
if (subTree != null)
{
stack.push(subTree);
subTree = subTree.leftChild;
} else
{
subTree = stack.pop();
System.out.print(subTree.value + ", ");
subTree = subTree.rightChild;
}
}
}
public void inOrderTraverse()
{
inOrderTraverse(this);
}
private void inOrderTraverse(BinaryTree<V> subTree)
{
if (subTree != null)
{
inOrderTraverse(subTree.leftChild);
System.out.print(subTree.value + ", ");
inOrderTraverse(subTree.rightChild);
}
}
public void postOrderTraverseIterative()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
Stack<BinaryTree<V>> result = new Stack<BinaryTree<V>>();
BinaryTree<V> current;
stack.push(this);
while (!stack.empty())
{
current = stack.pop();
result.push(current);
if (current.leftChild != null)
stack.push(current.leftChild);
if (current.rightChild != null)
stack.push(current.rightChild);
}
while (!result.empty())
{
System.out.print(result.pop().value + ", ");
}
}
public void postOrderTraverse()
{
postOrderTraverse(this);
}
private void postOrderTraverse(BinaryTree<V> subTree)
{
if (subTree != null)
{
postOrderTraverse(subTree.leftChild);
postOrderTraverse(subTree.rightChild);
System.out.print(subTree.value + ", ");
}
}
public void display()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
stack.push(this);
int nBlanks = 32;
boolean isRowEmpty = false;
while (isRowEmpty == false)
{
Stack<BinaryTree<V>> localStack = new Stack<BinaryTree<V>>();
isRowEmpty = true;
for (int j = 0; j < nBlanks; j++)
System.out.print(' ');
while (stack.isEmpty() == false)
{
BinaryTree<V> subTree = stack.pop();
if (subTree != null)
{
System.out.print(subTree.value);
localStack.push(subTree.leftChild);
localStack.push(subTree.rightChild);
if (subTree.leftChild != null || subTree.rightChild != null)
isRowEmpty = false;
} else
{
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for (int j = 0; j < nBlanks * 2 - 2; j++)
System.out.print(' ');
}
System.out.println();
nBlanks /= 2;
while (localStack.isEmpty() == false)
stack.push(localStack.pop());
}
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Stack;
/**
* A BinaryTree consists of "nodes"--each "node" is itself a BinaryTree. Each
* node has a parent (unless it is the root), may have a left child, and may
* have a right child. This class implements loop-free binary trees, allowing
* shared subtrees.
*
* @author David Matuszek
* @version Jan 25, 2004
* @param <V>
* The type of values contained in this BinaryTree.
*/
public class BinaryTree<V>
{
/**
* The value (data) in this node of the binary tree; may be of any object
* type.
*/
public V value;
private BinaryTree<V> leftChild;
private BinaryTree<V> rightChild;
/**
* Constructor for BinaryTree.
*
* @param value
* The value to be placed in the root.
* @param leftChild
* The left child of the root (may be null).
* @param rightChild
* The right child of the root (may be null).
*/
public BinaryTree(V value, BinaryTree<V> leftChild, BinaryTree<V> rightChild)
{
this.value = value;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
/**
* Constructor for a BinaryTree leaf node (that is, with no children).
*
* @param value
* The value to be placed in the root.
*/
public BinaryTree(V value)
{
this(value, null, null);
}
/**
* Getter method for the value in this BinaryTree node.
*
* @return The value in this node.
*/
public V getValue()
{
return value;
}
/**
* Getter method for left child of this BinaryTree node.
*
* @return The left child (<code>null</code> if no left child).
*/
public BinaryTree<V> getLeftChild()
{
return leftChild;
}
/**
* Getter method for right child of this BinaryTree node.
*
* @return The right child (<code>null</code> if no right child).
*/
public BinaryTree<V> getRightChild()
{
return rightChild;
}
/**
* Sets the left child of this BinaryTree node to be the given subtree. If
* the node previously had a left child, it is discarded. Throws an
* <code>IllegalArgumentException</code> if the operation would cause a loop
* in the binary tree.
*
* @param subtree
* The node to be added as the new left child.
* @throws IllegalArgumentException
* If the operation would cause a loop in the binary tree.
*/
public void setLeftChild(BinaryTree<V> subtree)
throws IllegalArgumentException
{
if (contains(subtree, this))
{
throw new IllegalArgumentException("Subtree " + this
+ " already contains " + subtree);
}
leftChild = subtree;
}
/**
* Sets the right child of this BinaryTree node to be the given subtree. If
* the node previously had a right child, it is discarded. Throws an
* <code>IllegalArgumentException</code> if the operation would cause a loop
* in the binary tree.
*
* @param subtree
* The node to be added as the new right child.
* @throws IllegalArgumentException
* If the operation would cause a loop in the binary tree.
*/
public void setRightChild(BinaryTree<V> subtree)
throws IllegalArgumentException
{
if (contains(subtree, this))
{
throw new IllegalArgumentException("Subtree " + this
+ " already contains " + subtree);
}
rightChild = subtree;
}
/**
* Sets the value in this BinaryTree node.
*
* @param value
* The new value.
*/
public void setValue(V value)
{
this.value = value;
}
/**
* Tests whether this node is a leaf node.
*
* @return <code>true</code> if this BinaryTree node has no children.
*/
public boolean isLeaf()
{
return leftChild == null && rightChild == null;
}
/**
* Tests whether this BinaryTree is equal to the given object. To be
* considered equal, the object must be a BinaryTree, and the two binary
* trees must have equal values in their roots, equal left subtrees, and
* equal right subtrees.
*
* @return <code>true</code> if the binary trees are equal.
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o)
{
if (o == null || !(o instanceof BinaryTree))
{
return false;
}
BinaryTree<?> otherTree = (BinaryTree<?>) o;
return equals(value, otherTree.value)
&& equals(leftChild, otherTree.getLeftChild())
&& equals(rightChild, otherTree.getRightChild());
}
/**
* Tests whether its two arguments are equal. This method simply checks for
* <code>null</code> before calling <code>equals(Object)</code> so as to
* avoid possible <code>NullPointerException</code>s.
*
* @param x
* The first object to be tested.
* @param y
* The second object to be tested.
* @return <code>true</code> if the two objects are equal.
*/
private boolean equals(Object x, Object y)
{
if (x == null)
return y == null;
return x.equals(y);
}
/**
* Tests whether the <code>tree</code> argument contains within itself the
* <code>targetNode</code> argument.
*
* @param tree
* The root of the binary tree to search.
* @param targetNode
* The node to be searched for.
* @return <code>true</code> if the <code>targetNode</code> argument can be
* found within the binary tree rooted at <code>tree</code>.
*/
protected static <T> boolean contains(BinaryTree<T> tree,
BinaryTree<T> targetNode)
{
if (tree == null)
return false;
if (tree == targetNode)
return true;
return contains(tree.getLeftChild(), targetNode)
|| contains(tree.getRightChild(), targetNode);
}
/**
* Returns a String representation of this BinaryTree.
*
* @see java.lang.Object#toString()
* @return A String representation of this BinaryTree.
*/
@Override
public String toString()
{
if (isLeaf())
{
return value.toString();
} else
{
String root, left = "null", right = "null";
root = value.toString();
if (getLeftChild() != null)
{
left = getLeftChild().toString();
}
if (getRightChild() != null)
{
right = getRightChild().toString();
}
return root + " (" + left + ", " + right + ")";
}
}
/**
* Computes a hash code for the complete binary tree rooted at this
* BinaryTree node.
*
* @return A hash code for the binary tree with this root.
* @see java.lang.Object#hashCode()
*/
public int hashCode()
{
int result = value.hashCode();
if (leftChild != null)
{
result += 3 * leftChild.hashCode();
}
if (rightChild != null)
{
result += 7 * rightChild.hashCode();
}
return result;
}
/**
* Prints the binary tree rooted at this BinaryTree node.
*/
public void print()
{
print(this, 0);
}
private void print(BinaryTree<V> root, int indent)
{
for (int i = 0; i < indent; i++)
{
System.out.print(" ");
}
if (root == null)
{
System.out.println("null");
return;
}
System.out.println(root.value);
if (root.isLeaf())
return;
print(root.leftChild, indent + 1);
print(root.rightChild, indent + 1);
}
// -------------------------- Methods added for Assignment 3, CIT 594-2008
/**
* Returns the leftmost descendant of this binary tree. That is, return the
* leaf that is the left child of the left child of ... the left child of
* this binary tree. If this binary tree has no left child, return this
* binary tree.
*
* @return The leftmost descendant of this BinaryTree.
*/
public BinaryTree<V> leftmostDescendant()
{
if (this.leftChild == null)
return this;
else
return leftChild.leftmostDescendant();
}
/**
* Returns the rightmost descendant of this binary tree. That is, return the
* leaf that is the right child of the right child of ... the right child of
* this binary tree. If this binary tree has no right child, return this
* binary tree.
*
* @return The rightmost descendant of this BinaryTree.
*/
public BinaryTree<V> rightmostDescendant()
{
if (this.rightChild == null)
return this;
else
return rightChild.rightmostDescendant();
}
/**
* Returns the total number of nodes in this binary tree (include the root
* in the count).
*
* @return The number of nodes in this BinaryTree.
*/
public int numberOfNodes()
{
int leftCount = this.leftChild == null ? 0 : leftChild.numberOfNodes();
int rightCount = this.rightChild == null ? 0 : rightChild
.numberOfNodes();
return 1 + leftCount + rightCount;
}
/**
* Returns the depth of this binary tree. The depth of a binary tree is the
* length of the longest path from this node to a leaf. The depth of a
* binary tree with no descendants (that is, just a leaf) is zero.
*
* @return The depth of this BinaryTree.
*/
public int depth()
{
int leftDepth = this.leftChild == null ? 0 : leftChild.depth() + 1;
int rightDepth = this.rightChild == null ? 0 : rightChild.depth() + 1;
return Math.max(leftDepth, rightDepth);
}
/**
* Returns true if and only if some node in this binary tree contains a
* value that is equal to the parameter.
*
* @param value
* The value to be searched for.
* @return <code>true</code> if this BinaryTree contains an equal value.
*/
public boolean containsEqualValue(V value)
{
if (this.value == null && value == null)
return true;
if (this.value != null && this.value.equals(value))
return true;
if (leftChild != null && leftChild.containsEqualValue(value))
return true;
if (rightChild != null && rightChild.containsEqualValue(value))
return true;
return false;
}
/**
* Returns true if and only if some node in this binary tree contains the
* same object (not just an equal object) as the one given for the value
* parameter.
*
* @param value
* The value to be searched for.
* @return <code>true</code> if this BinaryTree contains the identical
* value.
*/
public boolean containsSameValue(V value)
{
if (this.value == null && value == null)
return true;
if (this.value != null && this.value == value)
return true;
if (leftChild != null && leftChild.containsSameValue(value))
return true;
if (rightChild != null && rightChild.containsSameValue(value))
return true;
return false;
}
/**
* Returns a Set of all the leaves of this binary tree.
*
* @return The leaves of this BinaryTree.
*/
public Set<BinaryTree<V>> leaves()
{
Set<BinaryTree<V>> set = new HashSet<BinaryTree<V>>();
if (this.isLeaf())
{
set.add(this);
}
if (leftChild != null)
{
set.addAll(leftChild.leaves());
}
if (rightChild != null)
{
set.addAll(rightChild.leaves());
}
return set;
}
/**
* Returns a Set of the values (of type V) in this binary tree.
*
* @return The values in this BinaryTree.
*/
public Set<V> valuesOf()
{
Set<V> values = new HashSet<V>();
values.add(this.value);
if (leftChild != null)
{
values.addAll(leftChild.valuesOf());
}
if (rightChild != null)
{
values.addAll(rightChild.valuesOf());
}
return values;
}
/**
* Returns a List of the values (of type V) in the leaves of this binary
* tree, in left-to-right order.
*
* @return The values in the leaves of this BinaryTree.
*/
public List<V> fringe()
{
List<V> values = new ArrayList<V>();
if (this.isLeaf())
{
values.add(this.value);
return values;
}
if (leftChild != null)
{
values.addAll(leftChild.fringe());
}
if (rightChild != null)
{
values.addAll(rightChild.fringe());
}
return values;
}
/**
* Returns a new BinaryTree equal to (but not the same as) this binary tree.
* Every node in this new BinaryTree will be created by the copy method;
* values will be identical (==) to values in the given binary tree.
*
* @return An exact copy of this BinaryTree; the values in the new
* BinaryTree are == to the values in this BinaryTree.
*/
public BinaryTree<V> copy()
{
BinaryTree<V> left = null, right = null;
if (this.leftChild != null)
{
left = this.leftChild.copy();
}
if (this.rightChild != null)
{
right = this.rightChild.copy();
}
return new BinaryTree<V>(this.value, left, right);
}
/**
* Returns a new binary tree which is the mirror image of the binary tree
* whose root is at this binary tree. That is, for every node in the new
* binary tree, its children are in reverse order (left child becomes right
* child, right child becomes left child).
*
* @return A mirror image copy of this BinaryTree; values in the new
* BinaryTree are == to the values in this BinaryTree.
*/
public BinaryTree<V> reverse()
{
BinaryTree<V> left = null, right = null;
if (this.leftChild != null)
{
left = this.leftChild.reverse();
}
if (this.rightChild != null)
{
right = this.rightChild.reverse();
}
return new BinaryTree<V>(this.value, right, left);
}
/**
* Rearranges the binary tree rooted at this binary tree to be the mirror
* image of its original structure. No new BinaryTree nodes are created in
* this process.
*/
public void reverseInPlace()
{
if (this.leftChild != null)
{
leftChild.reverseInPlace();
}
if (this.rightChild != null)
{
rightChild.reverseInPlace();
}
BinaryTree<V> temp = this.leftChild;
this.setLeftChild(this.rightChild);
this.setRightChild(temp);
}
public void preOrderTraverseIterative()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
BinaryTree<V> subTree = this;
while (!stack.empty() || subTree != null)
{
if (subTree != null)
{
stack.push(subTree);
System.out.print(subTree.value + ", ");
subTree = subTree.leftChild;
} else
{
subTree = stack.pop();
subTree = subTree.rightChild;
}
}
}
public void preOrderTraverse()
{
preOrderTraverse(this);
}
private void preOrderTraverse(BinaryTree<V> subTree)
{
if (subTree != null)
{
System.out.print(subTree.value + ", ");
preOrderTraverse(subTree.leftChild);
preOrderTraverse(subTree.rightChild);
}
}
public void inOrderTraverseIterative()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
BinaryTree<V> subTree = this;
while (!stack.empty() || subTree != null)
{
if (subTree != null)
{
stack.push(subTree);
subTree = subTree.leftChild;
} else
{
subTree = stack.pop();
System.out.print(subTree.value + ", ");
subTree = subTree.rightChild;
}
}
}
public void inOrderTraverse()
{
inOrderTraverse(this);
}
private void inOrderTraverse(BinaryTree<V> subTree)
{
if (subTree != null)
{
inOrderTraverse(subTree.leftChild);
System.out.print(subTree.value + ", ");
inOrderTraverse(subTree.rightChild);
}
}
public void postOrderTraverseIterative()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
Stack<BinaryTree<V>> result = new Stack<BinaryTree<V>>();
BinaryTree<V> current;
stack.push(this);
while (!stack.empty())
{
current = stack.pop();
result.push(current);
if (current.leftChild != null)
stack.push(current.leftChild);
if (current.rightChild != null)
stack.push(current.rightChild);
}
while (!result.empty())
{
System.out.print(result.pop().value + ", ");
}
}
public void postOrderTraverse()
{
postOrderTraverse(this);
}
private void postOrderTraverse(BinaryTree<V> subTree)
{
if (subTree != null)
{
postOrderTraverse(subTree.leftChild);
postOrderTraverse(subTree.rightChild);
System.out.print(subTree.value + ", ");
}
}
public void display()
{
Stack<BinaryTree<V>> stack = new Stack<BinaryTree<V>>();
stack.push(this);
int nBlanks = 32;
boolean isRowEmpty = false;
while (isRowEmpty == false)
{
Stack<BinaryTree<V>> localStack = new Stack<BinaryTree<V>>();
isRowEmpty = true;
for (int j = 0; j < nBlanks; j++)
System.out.print(' ');
while (stack.isEmpty() == false)
{
BinaryTree<V> subTree = stack.pop();
if (subTree != null)
{
System.out.print(subTree.value);
localStack.push(subTree.leftChild);
localStack.push(subTree.rightChild);
if (subTree.leftChild != null || subTree.rightChild != null)
isRowEmpty = false;
} else
{
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for (int j = 0; j < nBlanks * 2 - 2; j++)
System.out.print(' ');
}
System.out.println();
nBlanks /= 2;
while (localStack.isEmpty() == false)
stack.push(localStack.pop());
}
}
}
相关推荐
用C++写的二叉树先序遍历、中序遍历和后序遍历非递归算法
通过《BiTree》这个文件,我们可以实践以上理论,编写递归和非递归的遍历算法,并通过实际示例加深理解。这有助于提升对数据结构的理解,增强算法设计和实现能力。在学习过程中,可以尝试不同的数据结构和优化策略,...
用C++写的,包括二叉树的构建,二叉树的先序遍历、中序遍历和后序遍历非递归算法。
在IT领域,尤其是在数据结构与算法的学习中,中序遍历二叉树的非递归算法是一个关键且实用的知识点。通常,我们首先通过递归来实现二叉树的遍历,但递归方法可能因深度过大导致栈溢出,因此掌握非递归版本的遍历算法...
这是数据结构中二叉树的后序遍历的非递归算法的源代码。
C语言二叉树遍历前序非递归算法,简单易懂,正确无误
通过递归算法实现这三种遍历方式,有助于深入理解二叉树的性质和操作,为后续的算法学习和问题解决打下坚实基础。在编程实践中,我们可以根据具体需求选择合适的遍历策略,以高效地处理二叉树结构的数据。
通过对二叉树遍历过程的深入分析,本文提出了一种基于栈的非递归算法,该算法不仅有效地实现了二叉树的先序、中序和后序遍历,而且在一次遍历中即可获得所有遍历结果,大大提高了算法的效率和实用性。这对于处理大...
使用C++模板、类的技术实现了二叉树的中序遍历,在BC3.1已经测试成功
二叉树的非递归遍历,使用C++实现二叉树的非递归遍历,对正在学习算法的同学应该挺有帮助的
本资料“数据结构 二叉树三种遍历的非递归算法(背诵版)”着重介绍了二叉树的三种主要遍历方法——前序遍历、中序遍历和后序遍历的非递归实现。 **前序遍历**: 前序遍历的顺序是:根节点 -> 左子树 -> 右子树。非...
//二叉树的二叉链表存储表示 typedef struct BiTNode { TElemType data; BiTNode *lchild, *rchild; //左右孩子指针 } BiTNode, *BiTree; typedef struct { BiTree *base; BiTree *top; int stacksize; //当前...
### 二叉树遍历的通用非递归算法解析 #### 一、引言 二叉树作为一种重要的数据结构,在计算机科学中应用广泛。对于二叉树的操作,遍历是最基本也是最常用的一种。传统的遍历算法通常采用递归方式实现,这种方式...
} }}//二叉树后序遍历递归算法void postorder(bintree t){ if(t) { postorder(t->lchild); postorder(t->rchild); cout<<t->data "; }}//二叉树后序遍历非递归算法void postorder1(bintree t,seqstack* s)...
常见的二叉树遍历方法有三种:先序遍历、中序遍历和后序遍历。下面我们将讨论这三种遍历方法的非递归算法。 一、先序遍历非递归算法 先序遍历是指先访问根结点,然后访问左子树,最后访问右子树。非递归算法使用栈...
前序遍历是二叉树遍历的一种方式,其顺序为“根节点 -> 左子树 -> 右子树”。具体而言,在遍历过程中,首先访问根节点,然后递归地遍历左子树,最后递归地遍历右子树。对于非递归的前序遍历,通常会利用栈来辅助实现...
由先根次序和中跟次序建立二叉树,以及各种遍历的递归、非递归算法
以递归方式按先序序列建立二叉树的二叉链表结构,再分别输出先序、中序、后序的遍历结果。
二叉树遍历的递归和非递归实现都有其优缺点。递归方法代码简洁,但会消耗更多的系统资源,因为每次函数调用都会产生一定的开销。非递归方法虽然代码相对复杂,但能有效地控制内存使用,适合处理大容量的数据。 在...
非递归中序遍历二叉树(算法2): 层次遍历二叉树: 递归计算单分支结点: 递归计算双分支结点: 递归计算叶子数: 二叉数的深度: 交换二叉树的左右子树: 二叉树已左右交换。 递归先序遍历二叉树: 递归...