⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preorder.cpp

📁 数据结构与算法分析(C++)(版第二版)源码
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>
#include "book.h"

#include "binnode.h"

// Stick these in to test the preorder routine
#define visit(X) (cout << X->val() << endl)

template <class Elem>
void preorder(BinNode<Elem>* subroot) {
  if (subroot == NULL) return;  // Empty subtree, do nothing
  visit(subroot);  // Perform whatever action is desired
  preorder(subroot->left());
  preorder(subroot->right());
}

template <class Elem>
int count(BinNode<Elem>* subroot) {
  if (subroot == NULL) return 0;  // Nothing to count
  return 1 + count(subroot->left())
           + count(subroot->right());
}

// Test the preorder traversal routines
int main()
{
  BinNodePtr<Int>* root = new BinNodePtr<Int>(10);
  root->setLeft(new BinNodePtr<Int>(15));
  root->setRight(new BinNodePtr<Int>(20));
  preorder(root);
  cout << " Count is: " << count(root) << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -