您的位置:首页 > 理论基础 > 数据结构算法

数据结构与算法-第12章二叉树和其他树-004求二叉树的最多结点数及对应的层数

2016-03-19 17:09 585 查看
1.

package chapter12Tree;

import dataStructures.ArrayQueue;

//We perform a level-order traversal. To differentiate among nodes at different levels,
//we use a unique pointer which serves as an end of level marker.
//This pointer is added to the level order queue following the last node at each level.
//As nodes are removed from the queue, a counter is incremented until the end of level
//marker is removed. This strategy enables us to determine the number of nodes
//in a level. The code is given below.
public class BinaryTreeMaxLevel
{
/** @return level with max umber of nodes */
public static int maxLevel(BinaryTreeNode t)
{
if (t == null)
// tree is empty
return 0;

// maxLevel is current level with max nodes
// maxNodes is number of nodes on level maxLevel
int maxLevel = 0;
int maxNodes = 0;

// create a unique pointer to use an end of level marker in queue
BinaryTreeNode endOfLevel = new BinaryTreeNode();

// put t and endOfLevel marker on queue q
ArrayQueue q = new ArrayQueue();
q.put(t);
q.put(endOfLevel);

// do a level order traversal
int currentLevel = 1;    // level of nodes being examined
int numOfNodes = 0;      // number of nodes seen of currentLevel
while (true)
{
BinaryTreeNode p = (BinaryTreeNode) q.remove();
if (p == endOfLevel)
{
// see if currentLevel has more nodes than MaxLevel
if (numOfNodes > maxNodes)
{// found a level with more nodes
maxNodes = numOfNodes;
maxLevel = currentLevel;
}
else
if (numOfNodes == 0)
// empty level, no more nodes
return maxLevel;

// set currentLevel and numOfNodes to start new level
currentLevel++;
numOfNodes = 0;

q.put(endOfLevel);
}
else
{// continuation of current level
numOfNodes++;

// put p's children on queue
if (p.leftChild != null)
q.put(p.leftChild);
if (p.rightChild != null)
q.put(p.rightChild);
}
}
}
}


2.

/** a queue class that uses a one-dimensional array */

package dataStructures;

public class ArrayQueue implements Queue
{
// data members
int front;          // one counterclockwise from first element
int rear;           // position of rear element of queue
Object [] queue;    // element array

// constructors
/** create a queue with the given initial capacity */
public ArrayQueue(int initialCapacity)
{
if (initialCapacity < 1)
throw new IllegalArgumentException
("initialCapacity must be >= 1");
queue = new Object [initialCapacity + 1];
// default front = rear = 0
}

/** create a queue with initial capacity 10 */
public ArrayQueue()
{// use default capacity of 10
this(10);
}

// methods
/** @return true iff queue is empty */
public boolean isEmpty()
{return front == rear;}

/** @return front element of queue
* @return null if queue is empty */
public Object getFrontElement()
{
if (isEmpty())
return null;
else
return queue[(front + 1) % queue.length];
}

/** @return rear element of queue
* @return null if the queue is empty */
public Object getRearElement()
{
if (isEmpty())
return null;
else
return queue[rear];
}

/** insert theElement at the rear of the queue */
public void put(Object theElement)
{
// increase array length if necessary
if ((rear + 1) % queue.length == front)
{// double array size
// allocate a new array
Object [] newQueue = new Object [2 * queue.length];

// copy elements into new array
int start = (front + 1) % queue.length;
if (start < 2)
// no wrap around
System.arraycopy(queue, start, newQueue, 0,
queue.length - 1);
else
{  // queue wraps around
System.arraycopy(queue, start, newQueue, 0,
queue.length - start);
System.arraycopy(queue, 0, newQueue,
queue.length - start, rear + 1);
}

// switch to newQueue and set front and rear
front = newQueue.length - 1;
rear = queue.length - 2;   // queue size is queue.length - 1
queue = newQueue;
}

// put theElement at the rear of the queue
rear = (rear + 1) % queue.length;
queue[rear] = theElement;
}

/** remove an element from the front of the queue
* @return removed element
* @return null if the queue is empty */
public Object remove()
{
if (isEmpty())
return null;
front = (front + 1) % queue.length;
Object frontElement = queue[front];
queue[front] = null;   // enable garbage collection
return frontElement;
}

/** test program */
public static void main(String [] args)
{
int x;
ArrayQueue q = new ArrayQueue(3);
// add a few elements
q.put(new Integer(1));
q.put(new Integer(2));
q.put(new Integer(3));
q.put(new Integer(4));

// remove and add to test wraparound array doubling
q.remove();
q.remove();
q.put(new Integer(5));
q.put(new Integer(6));
q.put(new Integer(7));
q.put(new Integer(8));
q.put(new Integer(9));
q.put(new Integer(10));
q.put(new Integer(11));
q.put(new Integer(12));

// delete all elements
while (!q.isEmpty())
{
System.out.println("Rear element is " + q.getRearElement());
System.out.println("Front element is " + q.getFrontElement());
System.out.println("Removed the element " + q.remove());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: