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

📄 w11_9_10.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

#include "treelib.h"
#include "treescan.h"
#include "bstree.h"

// print character data in a tree node
void print(char& c)
{
	cout << c << " ";
}

// RLN tree scan
template <class T>
void RLN (TreeNode<T> *t, void visit(T& item))
{
   // the recursive scan terminates on a empty subtree
   if (t != NULL)
   {
      RLN(t->Right(), visit); // descend right
      RLN(t->Left(), visit);  // descend left
      visit(t->data);         // visit the node
   }
}

// RNL tree scan
template <class T>
void RNL (TreeNode<T> *t, void visit(T& item))
{
   // the recursive scan terminates on a empty subtree
   if (t != NULL)
   {
      RNL(t->Right(), visit); // descend right
      visit(t->data);         // visit the node
      RNL(t->Left(), visit);  // descend left
   }
}

// NRL tree scan
template <class T>
void NRL (TreeNode<T> *t, void visit(T& item))
{
   // the recursive scan terminates on a empty subtree
   if (t != NULL)
   {
      visit(t->data);               // visit the node
      NRL(t->Right(), visit); // descend right
      NRL(t->Left(), visit);  // descend left
   }
}

void main (void)
{
	// data for three trees
	char s1[] = "MTVFUN";
	char s2[] = "FLORIDA";
	char s3[] = "ROTARYCLUB";
	// declare 3 binary search trees
	BinSTree<char> t1, t2, t3;
	int i= 0;
	
	// insert data into the three trees
	while(s1[i])
		t1.Insert(s1[i++]);
	i = 0;
	while(s2[i])
		t2.Insert(s2[i++]);
	i = 0;
	while(s3[i])
		t3.Insert(s3[i++]);
		
	// traverse each tree in preorder, inorder
	// postorder, RLN, RNL and NRL
	
	cout << "Tree 1" << endl << endl;
	Preorder(t1.GetRoot(),print);
	cout << endl;
	Inorder(t1.GetRoot(),print);
	cout << endl;
	Postorder(t1.GetRoot(),print);
	cout << endl;
	RLN(t1.GetRoot(),print);
	cout << endl;
	RNL(t1.GetRoot(),print);
	cout << endl;
	NRL(t1.GetRoot(),print);
	cout << endl;
	LevelScan(t1.GetRoot(),print);
	cout << endl;
	
	cout << endl << "Tree 2" << endl << endl;
	Preorder(t2.GetRoot(),print);
	cout << endl;
	Inorder(t2.GetRoot(),print);
	cout << endl;
	Postorder(t2.GetRoot(),print);
	cout << endl;
	RLN(t2.GetRoot(),print);
	cout << endl;
	RNL(t2.GetRoot(),print);
	cout << endl;
	NRL(t2.GetRoot(),print);
	cout << endl;
	LevelScan(t2.GetRoot(),print);
	cout << endl;
	
	cout << endl << "Tree 3" << endl << endl;
	Preorder(t3.GetRoot(),print);
	cout << endl;
	Inorder(t3.GetRoot(),print);
	cout << endl;
	Postorder(t3.GetRoot(),print);
	cout << endl;
	RLN(t3.GetRoot(),print);
	cout << endl;
	RNL(t3.GetRoot(),print);
	cout << endl;
	NRL(t3.GetRoot(),print);
	cout << endl;
	LevelScan(t3.GetRoot(),print);
	cout << endl;
}

/*
<Run>

Tree 1

M F T N V U 
F M N T U V 
F N U V T M 
U V N T F M 
V U T N M F 
M T V U N F 
M F T N V U 

Tree 2

F D A L I O R 
A D F I L O R 
A D I R O L F 
R O I L A D F 
R O L I F D A 
F L O R I D A 
F D L A I O R 

Tree 3

R O A C B L T R Y U 
A B C L O R R T U Y 
B L C A O R U Y T R 
U Y R T L B C A O R 
Y U T R R O L C B A 
R T Y U R O A C L B 
R O T A R Y C U B L 				
*/

⌨️ 快捷键说明

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