<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><font size="6" color="#FF0000"><b>LevelPrint</b></font></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.First Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is first<span lang="en-ca"> edition of</span> first question in assignment <span lang="en-ca">3 </span>of <span lang="en-ca">comp</span></b><span lang="en-ca"><b>352 and it is a simple </span>test<span lang="en-ca">.</span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre><b>How to print a binary tree level by level?</b></pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  　</div>
<div align="left">
  <font size="2"><b>Use a queue.</b></font></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="2"><b>The problem is that for such a simple question I have used 7 classes, not including their base abstract classes.</b></font></pre>
</div>
<div align="left">
  <pre><b>And I haven't mentioned that several classes are template class and I already used some I/O class like ofstream</b></pre>
</div>
<div align="left">
  <pre><b>for I/O. Such deluxry!</b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca"><font size="3" color="#FF0000">//the file name is </span>Levelprint<span lang="en-ca">.cpp</span></font></b></pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &quot;book.h&quot;
#include &quot;Compare.h&quot;
#include &quot;BST.h&quot;

#include &quot;BinNode.h&quot;



//#include &quot;queue.h&quot;
#include &quot;AQueue.h&quot;

using namespace std;

class LevelBST: public BST&lt;int, Int, intIntCompare, IntIntCompare&gt;
{
private:
	ifstream in;
	AQueue&lt;BinNode&lt;Int&gt;*&gt; Que;
public:
	void readFile(const char* fileName);
	void preOrder(BinNode&lt;Int&gt;* ptr, void (*visit)(BinNode&lt;Int&gt;*node));
	void LevelPrint();
};




int main()
{
	LevelBST L;
	L.readFile(&quot;c:\\mytree.txt&quot;);
	L.LevelPrint();
	return 0;
}



void LevelBST::LevelPrint()
{
	BinNode&lt;Int&gt;* son;

	Que.enqueue(root);

	while (Que.dequeue(son))
	{
	
		cout&lt;&lt;son-&gt;val().key()&lt;&lt;endl;
		if (son-&gt;left()!=NULL)
		{
			Que.enqueue(son-&gt;left());
	
		}
		if (son-&gt;right()!=NULL)
		{
			Que.enqueue(son-&gt;right());
		
		}
	}
}



void LevelBST::preOrder(BinNode&lt;Int&gt;* ptr, void (*visit)(BinNode&lt;Int&gt;*node))
{
	if (ptr!=NULL)
	{
		visit(ptr);
		preOrder(ptr-&gt;left(), visit);
		preOrder(ptr-&gt;right(), visit);
	}
}

void LevelBST::readFile(const char* fileName)
{
	in.open(fileName);
	Int *myInt;
	int i;
	while (!in.eof())
	{
		in&gt;&gt;i;
		myInt= new Int(i);
		insert(*myInt);
	}
}



</pre>
<pre><b><font color="#FF0000">//file name AQueue.h</font></b></pre>

<pre>// This is the file to include in your code if you want access to the
// complete AQueue template class

// First, get the declaration for the base stack class
#include &quot;queue.h&quot;

// Array-based queue implementation
template &lt;class Elem&gt; class AQueue: public Queue&lt;Elem&gt; {
private:
  int size;                  // Maximum size of queue
  int front;                 // Index of front element
  int rear;                  // Index of rear element
  Elem *listArray;           // Array holding queue elements
public:
  AQueue(int sz =DefaultListSize) { // Constructor 
    // Make list array one position larger for empty slot
    size = sz+1;
    rear = 0;  front = 1;
    listArray = new Elem[size];
  }
  ~AQueue() { delete [] listArray; } // Destructor
  void clear() { front = rear; }
  bool enqueue(const Elem&amp; it) {
    if (((rear+2) % size) == front) return false;  // Full
    rear = (rear+1) % size; // Circular increment
    listArray[rear] = it;
    return true;
  }
  bool dequeue(Elem&amp; it) {
    if (length() == 0) return false;  // Empty
    it = listArray[front];
    front = (front+1) % size; // Circular increment
    return true;
  }
  bool frontValue(Elem&amp; it) const {
    if (length() == 0) return false;  // Empty
    it = listArray[front];
    return true;
  }
  virtual int length() const
   { return ((rear+size) - front + 1) % size; }
};
</pre>
<pre>　</pre>

<pre><b><font color="#FF0000">//file name Binnode.h</font></b></pre>

<pre>#ifndef BINNODE_H
#define BINNODE_H

// Binary tree node abstract class
template &lt;class Elem&gt; class BinNode {
public:
  // Return the node's element
  virtual Elem&amp; val() = 0;
  // Set the node's element
  virtual void setVal(const Elem&amp;) = 0;
  // Return the node's left child
  virtual BinNode* left() const = 0;
  // Set the node's left child
  virtual void setLeft(BinNode*) = 0;
  // Return the node's right child
  virtual BinNode* right() const = 0;
  // Set the node's right child
  virtual void setRight(BinNode*) = 0;
  // Return true iff the node is a leaf
  virtual bool isLeaf() = 0;
};

// Binary tree node class
template &lt;class Elem&gt;
class BinNodePtr : public BinNode&lt;Elem&gt; {
private:
  Elem it;                     // The node's value
  BinNodePtr* lc;              // Pointer to left child
  BinNodePtr* rc;              // Pointer to right child
public:
  // Two constructors -- with and without initial values
  BinNodePtr() { lc = rc = NULL; }
  BinNodePtr(Elem e, BinNodePtr* l =NULL,
                     BinNodePtr* r =NULL)
    { it = e; lc = l; rc = r; }
  ~BinNodePtr() {}             // Destructor
  Elem&amp; val() { return it; }
  void setVal(const Elem&amp; e) { it = e; }
  inline BinNode&lt;Elem&gt;* left() const { return lc; }
  void setLeft(BinNode&lt;Elem&gt;* b) { lc = (BinNodePtr*)b; }
  inline BinNode&lt;Elem&gt;* right() const { return rc; }
  void setRight(BinNode&lt;Elem&gt;* b) { rc = (BinNodePtr*)b; }
  bool isLeaf() { return (lc == NULL) &amp;&amp; (rc == NULL); }
};

#endif</pre>
<pre><b><font color="#FF0000">//filename BST.h</font></b></pre>

<pre>// This file includes all of the pieces of the BST implementation

#include &quot;dictionary.h&quot;

#include &quot;binnode.h&quot;

// Binary Search Tree implementation for the Dictionary ADT
template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
class BST : public Dictionary&lt;Key, Elem, KEComp, EEComp&gt; {
protected:
  BinNode&lt;Elem&gt;* root;   // Root of the BST
  int nodecount;         // Number of nodes in the BST
  // Private &quot;helper&quot; functions
  void clearhelp(BinNode&lt;Elem&gt;*);
  BinNode&lt;Elem&gt;* inserthelp(BinNode&lt;Elem&gt;*, const Elem&amp;);
  BinNode&lt;Elem&gt;* deletemin(BinNode&lt;Elem&gt;*, BinNode&lt;Elem&gt;*&amp;);
  BinNode&lt;Elem&gt;* removehelp(BinNode&lt;Elem&gt;*, const Key&amp;,
                            BinNode&lt;Elem&gt;*&amp;);
  bool findhelp(BinNode&lt;Elem&gt;*, const Key&amp;, Elem&amp;) const;
  void printhelp(BinNode&lt;Elem&gt;*, int) const;
public:
  BST() { root = NULL; nodecount = 0; }  // Constructor
  ~BST() { clearhelp(root); }            // Destructor
  void clear()
    { clearhelp(root); root = NULL; nodecount = 0; }
  bool insert(const Elem&amp; e) {
    root = inserthelp(root, e);
    nodecount++;
    return true;
  }
  bool remove(const Key&amp; K, Elem&amp; e) {
    BinNode&lt;Elem&gt;* t = NULL;
    root = removehelp(root, K, t);
    if (t == NULL) return false;  // Nothing found
    e = t-&gt;val();
    nodecount--;
    delete t;
    return true;
  }
  bool removeAny(Elem&amp; e) { // Delete min value
    if (root == NULL) return false; // Empty tree
    BinNode&lt;Elem&gt;* t;
    root = deletemin(root, t);
    e = t-&gt;val();
    delete t;
    nodecount--;
    return true;
  }
  bool find(const Key&amp; K, Elem&amp; e) const
    { return findhelp(root, K, e); }
  int size() { return nodecount; }
  void print() const {
    if (root == NULL) cout &lt;&lt; &quot;The BST is empty.\n&quot;;
    else printhelp(root, 0);
  }
};

template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
void BST&lt;Key, Elem, KEComp, EEComp&gt;::
clearhelp(BinNode&lt;Elem&gt;* subroot) {
  if (subroot == NULL) return;
  clearhelp(subroot-&gt;left());
  clearhelp(subroot-&gt;right());
  delete subroot;
}

template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
BinNode&lt;Elem&gt;* BST&lt;Key, Elem, KEComp, EEComp&gt;::
inserthelp(BinNode&lt;Elem&gt;* subroot, const Elem&amp; val) {
  if (subroot == NULL)            // Empty tree: create node
    return (new BinNodePtr&lt;Elem&gt;(val, NULL, NULL));
  if (EEComp::lt(val, subroot-&gt;val())) // Insert on left
    subroot-&gt;setLeft(inserthelp(subroot-&gt;left(), val));
  else subroot-&gt;setRight(inserthelp(subroot-&gt;right(), val));
  return subroot;  // Return subtree with node inserted
}

template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
BinNode&lt;Elem&gt;* BST&lt;Key, Elem, KEComp, EEComp&gt;::
deletemin(BinNode&lt;Elem&gt;* subroot, BinNode&lt;Elem&gt;*&amp; min) {
  if (subroot-&gt;left() == NULL) { // Found min
    min = subroot;
    return subroot-&gt;right();
  }
  else {                         // Continue left
    subroot-&gt;setLeft(deletemin(subroot-&gt;left(), min));
    return subroot;
  }
}

template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
BinNode&lt;Elem&gt;* BST&lt;Key, Elem, KEComp, EEComp&gt;::
removehelp(BinNode&lt;Elem&gt;* subroot, const Key&amp; K,
           BinNode&lt;Elem&gt;*&amp; t) {
  if (subroot == NULL) return NULL; // Val is not in tree
  else if (KEComp::lt(K, subroot-&gt;val())) // Check left
    subroot-&gt;setLeft(removehelp(subroot-&gt;left(), K, t));
  else if (KEComp::gt(K, subroot-&gt;val())) // Check right
    subroot-&gt;setRight(removehelp(subroot-&gt;right(), K, t));
  else {                           // Found it: remove it
    BinNode&lt;Elem&gt;* temp;
    t = subroot;
    if (subroot-&gt;left() == NULL)       // Only a right child
      subroot = subroot-&gt;right();      //  so point to right
    else if (subroot-&gt;right() == NULL) // Only a left child
      subroot = subroot-&gt;left();       //  so point to left
    else {                    // Both children are non-empty
      subroot-&gt;setRight(deletemin(subroot-&gt;right(), temp));
      Elem te = subroot-&gt;val();
      subroot-&gt;setVal(temp-&gt;val());
      temp-&gt;setVal(te);
      t = temp;
    }
  }
  return subroot;
}

template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
bool BST&lt;Key, Elem, KEComp, EEComp&gt;:: findhelp(
      BinNode&lt;Elem&gt;* subroot, const Key&amp; K, Elem&amp; e) const {
  if (subroot == NULL) return false;         // Empty tree
  else if (KEComp::lt(K, subroot-&gt;val()))    // Check left
    return findhelp(subroot-&gt;left(), K, e);
  else if (KEComp::gt(K, subroot-&gt;val()))    // Check right
    return findhelp(subroot-&gt;right(), K, e);
  else { e = subroot-&gt;val();  return true; } // Found it
}

template &lt;class Key, class Elem, class KEComp, class EEComp&gt;
void BST&lt;Key, Elem, KEComp, EEComp&gt;::
printhelp(BinNode&lt;Elem&gt;* subroot, int level) const {
  if (subroot == NULL) return;          // Empty tree
  printhelp(subroot-&gt;left(), level+1);  // Do left subtree
  for (int i=0; i&lt;level; i++)           // Indent to level
    cout &lt;&lt; &quot;  &quot;;
  cout &lt;&lt; subroot-&gt;val() &lt;&lt; &quot;\n&quot;;       // Print node value
  printhelp(subroot-&gt;right(), level+1); // Do right subtree
}
</pre>
<pre><b><font color="#FF0000">//file name book.h</font></b></pre>

<pre>#include &lt;time.h&gt;  // Used by timing functions
#include &lt;iostream&gt;
using namespace std;
// Utility functions and macros

// Return true iff x is even
inline bool EVEN(int x) { return (x % 2) == 0; }

// Return true iff x is odd
inline bool ODD(int x) { return (x &amp; 1) != 0; }

const int DefaultListSize = 10; // Lists, etc. default size 

// Assert: If boolean expression is false, print a message
//   and terminate the program
void Assert(bool val, char* string) {
  if (!val) { // Assertion failed -- close the program
    cout &lt;&lt; &quot;Assertion Failed: &quot; &lt;&lt; string &lt;&lt; endl;
    exit(-1);
  }
}

// Random number generator functions

inline void Randomize() // Seed the generator
  { srand(1); }

// Return a random value in range 0 to n-1
inline int Random(int n)
  { return rand() % (n); }

// Swap two elements in a generic array
template&lt;class Elem&gt;
inline void swap(Elem A[], int i, int j) {
  Elem temp = A[i];
  A[i] = A[j];
  A[j] = temp;
}

// Swap two objects passed by reference
template&lt;class Elem&gt;
inline void swap(Elem &amp;e1, Elem &amp;e2) {
  Elem temp = e1;
  e1 = e2;
  e2 = temp;
}

// Timing variables and functions
clock_t tstart = 0;  // Time at beginning of timed section

// Initialize the program timer
void Settime()
  { tstart = clock(); }

// Return the elapsed time since the last call to Settime
double Gettime()
  { return (double)((double)clock() - (double)tstart)/
           (double)CLOCKS_PER_SEC; }

// Your basic int type as an object.
class Int {
private:
  int val;
public:
  Int(int input=0) { val = input; }
  // The following is for those times when we actually
  //   need to get a value, rather than compare objects.
  int key() const { return val; }
  // Overload = to support Int foo = 5 syntax
  operator= (int input) { val = input; }
};

// Let us print out Ints easily
ostream&amp; operator&lt;&lt;(ostream&amp; s, const Int&amp; i)
  { return s &lt;&lt; i.key(); }
ostream&amp; operator&lt;&lt;(ostream&amp; s, const Int* i)
  { return s &lt;&lt; i-&gt;key(); }
</pre>
<pre>　</pre>

<pre><b><font color="#FF0000">//file name compare.h</font></b></pre>

<pre>// Some definitions for Comparator classes
class intintCompare {
public:
  static bool lt(int x, int y) { return x &lt; y; }
  static bool eq(int x, int y) { return x == y; }
  static bool gt(int x, int y) { return x &gt; y; }
};

class IntIntCompare {
public:
  static bool lt(Int x, Int y) { return x.key() &lt; y.key(); }
  static bool eq(Int x, Int y) { return x.key() == y.key(); }
  static bool gt(Int x, Int y) { return x.key() &gt; y.key(); }
};

class intIntCompare {
public:
  static bool lt(int x, Int y) { return x &lt; y.key(); }
  static bool eq(int x, Int y) { return x == y.key(); }
  static bool gt(int x, Int y) { return x &gt; y.key(); }
};

class intIntsCompare {
public:
  static bool lt(int x, Int* y) { return x &lt; y-&gt;key(); }
  static bool eq(int x, Int* y) { return x == y-&gt;key(); }
  static bool gt(int x, Int* y) { return x &gt; y-&gt;key(); }
};

class IntsIntsCompare {
public:
  static bool lt(Int* x, Int* y) { return x-&gt;key() &lt; y-&gt;key(); }
  static bool eq(Int* x, Int* y) { return x-&gt;key() == y-&gt;key(); }
  static bool gt(Int* x, Int* y) { return x-&gt;key() &gt; y-&gt;key(); }
};

class CCCompare {
public:
  static bool lt(char* x, char* y) { return strcmp(x, y) &lt; 0; }
  static bool eq(char* x, char* y) { return strcmp(x, y) == 0; }
  static bool gt(char* x, char* y) { return strcmp(x, y) &gt; 0; }
};</pre>
<pre>　</pre>
<pre>　</pre>
<pre><font color="#FF0000"><b><a name="result"></a>//here is the tree input</b></font></pre>
<pre>54 34 2 99 23 33 1 12 19 28 45 55</pre>
<pre>//the output of level printing is like following:</pre>
<pre>54
34
99
2
45
55
1
23
12
33
19
28
Press any key to continue
</pre>
<pre>
</pre>
<pre>　</pre>

<pre>
</pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>