📄 binarytree.h
字号:
// file binary.h
// LevelOrder and the private recursive PreOrder,
// InOrder, and PostOrder mthods have been changed
// from template functions to integer functions because
// Visual C++ is unable to reslove overloaded functions
// when these were template functions
#ifndef BinaryTree_
#define BinaryTree_
int _count;
#include<iostream>
using namespace std;
#include "btnode2.h"
#include "xcept.h"
template<class T>
class BinaryTree {
friend BinaryTree<int> HuffmanTree(T a[], int n);
friend void Code(BinaryTree<int> c, int ch[]);
public:
BinaryTree() {root = 0;};
~BinaryTree(){};
bool IsEmpty() const
{return ((root) ? false : true);}
bool Root(T& x) const;
void MakeTree(const T& element,
BinaryTree<T>& left, BinaryTree<T>& right);
void BreakTree(T& element, BinaryTree<T>& left,
BinaryTree<T>& right);
void PreOrder(void(*Visit)(BinaryTreeNode<T> *u))
{PreOrder(Visit, root);}
void InOrder(void(*Visit)(BinaryTreeNode<T> *u))
{InOrder(Visit, root);}
void PostOrder(void(*Visit)(BinaryTreeNode<T> *u))
{PostOrder(Visit, root);}
void PreOutput() {PreOrder(Output, root); cout << endl;}
void InOutput() {InOrder(Output, root); cout << endl;}
void PostOutput() {PostOrder(Output, root); cout << endl;}
void LevelOutput() {LevelOrder(Output); cout << endl;}
void Delete() {PostOrder(Free, root); root = 0;}
int Height() const {return Height(root);}
int Size()
{_count = 0; PreOrder(Add1, root); return _count;}
private:
BinaryTreeNode<T> *root; // pointer to root
void PreOrder(void(*Visit)
(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
void InOrder(void(*Visit)
(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
void PostOrder(void(*Visit)
(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
static void Free(BinaryTreeNode<T> *t) {delete t;}
static void Output(BinaryTreeNode<T> *t)
{cout << t->data << ' ';}
static void Add1(BinaryTreeNode<T> *t) {_count++;}
int Height(BinaryTreeNode<T> *t) const;
};
template<class T>
bool BinaryTree<T>::Root(T& x) const
{// Return root data in x.
// Return false if no root.
if (root) {x = root->data;
return true;}
else return false; // no root
}
template<class T>
void BinaryTree<T>::MakeTree(const T& element,
BinaryTree<T>& left, BinaryTree<T>& right)
{// Combine left, right, and element to make new tree.
// left, right, and this must be different trees.
// create combined tree
root = new BinaryTreeNode<T>
(element, left.root, right.root);
// deny access from trees left and right
left.root = right.root = 0;
}
template<class T>
void BinaryTree<T>::BreakTree(T& element,
BinaryTree<T>& left, BinaryTree<T>& right)
{// left, right, and this must be different trees.
// check if empty
if (!root) throw BadInput(); // tree empty
// break the tree
element = root->data;
left.root = root->LeftChild;
right.root = root->RightChild;
delete root;
root = 0;
}
// template<class T>
// void BinaryTree<T>::PreOrder(
// void(*Visit)(BinaryTreeNode<T> *u),
// BinaryTreeNode<T> *t)
void BinaryTree<int>::PreOrder(
void(*Visit)(BinaryTreeNode<int> *u),
BinaryTreeNode<int> *t)
{// Preorder traversal.
if (t) {Visit(t);
PreOrder(Visit, t->LeftChild);
PreOrder(Visit, t->RightChild);
}
}
template <class T>
void BinaryTree<int>::InOrder(
void(*Visit)(BinaryTreeNode<int> *u),
BinaryTreeNode<int> *t)
{// Inorder traversal.
if (t) {InOrder(Visit, t->LeftChild);
Visit(t);
InOrder(Visit, t->RightChild);
}
}
template <class T>
void BinaryTree<int>::PostOrder(
void(*Visit)(BinaryTreeNode<int> *u),
BinaryTreeNode<int> *t)
{// Postorder traversal.
if (t) {PostOrder(Visit, t->LeftChild);
PostOrder(Visit, t->RightChild);
Visit(t);
}
}
template <class T>
int BinaryTree<T>::Height(BinaryTreeNode<T> *t) const
{// Return height of tree *t.
if (!t) return 0; // empty tree
int hl = Height(t->LeftChild); // height of left
int hr = Height(t->RightChild); // height of right
if (hl > hr) return ++hl;
else return ++hr;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -