标签:
1.
1 package chapter12Tree; 2 3 import dataStructures.ArrayQueue; 4 5 //We perform a level-order traversal. To differentiate among nodes at different levels, 6 //we use a unique pointer which serves as an end of level marker. 7 //This pointer is added to the level order queue following the last node at each level. 8 //As nodes are removed from the queue, a counter is incremented until the end of level 9 //marker is removed. This strategy enables us to determine the number of nodes 10 //in a level. The code is given below. 11 public class BinaryTreeMaxLevel 12 { 13 /** @return level with max umber of nodes */ 14 public static int maxLevel(BinaryTreeNode t) 15 { 16 if (t == null) 17 // tree is empty 18 return 0; 19 20 // maxLevel is current level with max nodes 21 // maxNodes is number of nodes on level maxLevel 22 int maxLevel = 0; 23 int maxNodes = 0; 24 25 // create a unique pointer to use an end of level marker in queue 26 BinaryTreeNode endOfLevel = new BinaryTreeNode(); 27 28 // put t and endOfLevel marker on queue q 29 ArrayQueue q = new ArrayQueue(); 30 q.put(t); 31 q.put(endOfLevel); 32 33 // do a level order traversal 34 int currentLevel = 1; // level of nodes being examined 35 int numOfNodes = 0; // number of nodes seen of currentLevel 36 while (true) 37 { 38 BinaryTreeNode p = (BinaryTreeNode) q.remove(); 39 if (p == endOfLevel) 40 { 41 // see if currentLevel has more nodes than MaxLevel 42 if (numOfNodes > maxNodes) 43 {// found a level with more nodes 44 maxNodes = numOfNodes; 45 maxLevel = currentLevel; 46 } 47 else 48 if (numOfNodes == 0) 49 // empty level, no more nodes 50 return maxLevel; 51 52 // set currentLevel and numOfNodes to start new level 53 currentLevel++; 54 numOfNodes = 0; 55 56 q.put(endOfLevel); 57 } 58 else 59 {// continuation of current level 60 numOfNodes++; 61 62 // put p‘s children on queue 63 if (p.leftChild != null) 64 q.put(p.leftChild); 65 if (p.rightChild != null) 66 q.put(p.rightChild); 67 } 68 } 69 } 70 }
2.
1 /** a queue class that uses a one-dimensional array */ 2 3 package dataStructures; 4 5 public class ArrayQueue implements Queue 6 { 7 // data members 8 int front; // one counterclockwise from first element 9 int rear; // position of rear element of queue 10 Object [] queue; // element array 11 12 // constructors 13 /** create a queue with the given initial capacity */ 14 public ArrayQueue(int initialCapacity) 15 { 16 if (initialCapacity < 1) 17 throw new IllegalArgumentException 18 ("initialCapacity must be >= 1"); 19 queue = new Object [initialCapacity + 1]; 20 // default front = rear = 0 21 } 22 23 /** create a queue with initial capacity 10 */ 24 public ArrayQueue() 25 {// use default capacity of 10 26 this(10); 27 } 28 29 // methods 30 /** @return true iff queue is empty */ 31 public boolean isEmpty() 32 {return front == rear;} 33 34 35 /** @return front element of queue 36 * @return null if queue is empty */ 37 public Object getFrontElement() 38 { 39 if (isEmpty()) 40 return null; 41 else 42 return queue[(front + 1) % queue.length]; 43 } 44 45 /** @return rear element of queue 46 * @return null if the queue is empty */ 47 public Object getRearElement() 48 { 49 if (isEmpty()) 50 return null; 51 else 52 return queue[rear]; 53 } 54 55 /** insert theElement at the rear of the queue */ 56 public void put(Object theElement) 57 { 58 // increase array length if necessary 59 if ((rear + 1) % queue.length == front) 60 {// double array size 61 // allocate a new array 62 Object [] newQueue = new Object [2 * queue.length]; 63 64 // copy elements into new array 65 int start = (front + 1) % queue.length; 66 if (start < 2) 67 // no wrap around 68 System.arraycopy(queue, start, newQueue, 0, 69 queue.length - 1); 70 else 71 { // queue wraps around 72 System.arraycopy(queue, start, newQueue, 0, 73 queue.length - start); 74 System.arraycopy(queue, 0, newQueue, 75 queue.length - start, rear + 1); 76 } 77 78 // switch to newQueue and set front and rear 79 front = newQueue.length - 1; 80 rear = queue.length - 2; // queue size is queue.length - 1 81 queue = newQueue; 82 } 83 84 // put theElement at the rear of the queue 85 rear = (rear + 1) % queue.length; 86 queue[rear] = theElement; 87 } 88 89 /** remove an element from the front of the queue 90 * @return removed element 91 * @return null if the queue is empty */ 92 public Object remove() 93 { 94 if (isEmpty()) 95 return null; 96 front = (front + 1) % queue.length; 97 Object frontElement = queue[front]; 98 queue[front] = null; // enable garbage collection 99 return frontElement; 100 } 101 102 /** test program */ 103 public static void main(String [] args) 104 { 105 int x; 106 ArrayQueue q = new ArrayQueue(3); 107 // add a few elements 108 q.put(new Integer(1)); 109 q.put(new Integer(2)); 110 q.put(new Integer(3)); 111 q.put(new Integer(4)); 112 113 // remove and add to test wraparound array doubling 114 q.remove(); 115 q.remove(); 116 q.put(new Integer(5)); 117 q.put(new Integer(6)); 118 q.put(new Integer(7)); 119 q.put(new Integer(8)); 120 q.put(new Integer(9)); 121 q.put(new Integer(10)); 122 q.put(new Integer(11)); 123 q.put(new Integer(12)); 124 125 // delete all elements 126 while (!q.isEmpty()) 127 { 128 System.out.println("Rear element is " + q.getRearElement()); 129 System.out.println("Front element is " + q.getFrontElement()); 130 System.out.println("Removed the element " + q.remove()); 131 } 132 } 133 }
数据结构与算法-第12章二叉树和其他树-004求二叉树的最多结点数及对应的层数
标签:
原文地址:http://www.cnblogs.com/shamgod/p/5295673.html