标签:operation count() his inter gre 数据 png 目录 思想
二叉树的属性操作:
二叉树中结点的数目:
在BTree.h中实现统计结点数目:
protected:
int count(BTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
ret = count(node->left) + count(node->right) + 1;
}
return ret;
}
public:
int count() const
{
return count(root());
}
优化代码:
protected:
int count(BTreeNode<T>* node) const
{
return (node != NULL) ? (count(node->left) + count(node->right) + 1) : 0;
}
public:
int count() const
{
return count(root());
}
二叉树的高度:
在BTree.h中实现获取二叉树的高度:
protected:
int height(BTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
int lh = height(node->left);
int rh = height(node->right);
ret = ((lh > rh) ? lh : rh) + 1;
}
return ret;
}
public:
int height() const
{
return height(root());
}
树的度数:
在BTree.h中实现获取二叉树的度数:
protected:
int degree(BTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
int dl = degree(node->left);
int dr = degree(node->right);
ret = (!!node->left + !!node->right);
if( ret < dl )
{
ret = dl;
}
if( ret < dr )
{
ret = dr;
}
}
return ret;
}
public:
int degree() const
{
return degree(root());
}
优化代码:
protected:
int degree(BTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
BTreeNode<T>* child[] = { node->left, node->right };
ret = (!!node->left + !!node->right);
for(int i=0; (i<2) && (ret<2); i++)
{
int d = degree(child[i]);
if( ret < d )
{
ret = d;
}
}
}
return ret;
}
public:
int degree() const
{
return degree(root());
}
统一mian.cpp测试:
#include <iostream>
#include "BTree.h"
using namespace std;
using namespace StLib;
int main()
{
BTree<int> bt;
BTreeNode<int>* n = NULL;
bt.insert(1, NULL);
n = bt.find(1);
bt.insert(2, n);
bt.insert(3, n);
n = bt.find(2);
bt.insert(4, n);
bt.insert(5, n);
n = bt.find(4);
bt.insert(8, n);
bt.insert(9, n);
n = bt.find(5);
bt.insert(10, n);
n = bt.find(3);
bt.insert(6, n);
bt.insert(7, n);
cout << bt.count() << endl;
cout << bt.height() << endl;
cout << bt.degree() << endl;
return 0;
}
运行结果为:
10
4
2
二叉树的遍历:
需要考虑的问题:
设计思路 ( 游标 ):
层次遍历算法:
层次遍历算法示例:
在BTree.h中实现二叉树结构的层次遍历:
(并且在clear()函数和remove()函数中要加上清空队列的操作。另外,将遍历操作的四个函数在父类中声明为虚函数。)
public:
bool begin()
{
bool ret = (root() != NULL);
if( ret )
{
m_queue.clear();
m_queue.add(root());
}
return ret;
}
bool end()
{
return (m_queue.length() == 0);
}
bool next()
{
bool ret = (m_queue.length() > 0);
if( ret )
{
BTreeNode<T>* node = m_queue.front();
m_queue.remove();
if( node->left != NULL )
{
m_queue.add(node->left);
}
if( node->right != NULL )
{
m_queue.add(node->right);
}
}
return ret;
}
T current()
{
if( !end() )
{
return m_queue.front()->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException, "No value at current position ...");
}
}
mian.cpp测试:
#include <iostream>
#include "BTree.h"
using namespace std;
using namespace StLib;
int main()
{
BTree<int> bt;
BTreeNode<int>* n = NULL;
bt.insert(1, NULL);
n = bt.find(1);
bt.insert(2, n);
bt.insert(3, n);
n = bt.find(2);
bt.insert(4, n);
bt.insert(5, n);
n = bt.find(4);
bt.insert(8, n);
bt.insert(9, n);
n = bt.find(5);
bt.insert(10, n);
n = bt.find(3);
bt.insert(6, n);
bt.insert(7, n);
for(bt.begin(); !bt.end(); bt.next())
{
cout << bt.current() << " ";
}
cout << endl;
return 0;
}
运行结果为:
1 2 3 4 5 6 7 8 9 10
问题:
典型的二叉树遍历方式:
先序遍历 ( Pre-Order Traversal ):
先序遍历功能定义:
中序遍历 ( In-Order Traversal ):
中序遍历功能定义:
后序遍历 ( Post-Order Traversal ):
后序遍历功能定义:
需要考虑的问题:
设计要点:
函数接口设计
SharedPointer< Array<T> > traversal(BTTraversal order)
典型遍历示例:
二叉树的典型遍历方式:
#include "LinkQueue.h"
#include "DynamicArray.h"
namespace StLib
{
enum BTTraversal
{
PreOrder,
InOrder,
PostOrder
};
template <typename T>
class BTree : public Tree<T>
{
protected:
void preOrderTraversal(BTreeNode<T>* node, LinkQueue<BTreeNode<T>*>& queue)
{
if( node != NULL )
{
queue.add(node);
preOrderTraversal(node->left, queue);
preOrderTraversal(node->right, queue);
}
}
void inOrderTraversal(BTreeNode<T>* node, LinkQueue<BTreeNode<T>*>& queue)
{
if( node != NULL )
{
inOrderTraversal(node->left, queue);
queue.add(node);
inOrderTraversal(node->right, queue);
}
}
void postOrderTraversal(BTreeNode<T>* node, LinkQueue<BTreeNode<T>*>& queue)
{
if( node != NULL )
{
postOrderTraversal(node->left, queue);
postOrderTraversal(node->right, queue);
queue.add(node);
}
}
public:
SharedPointer< Array<T> > traversal(BTTraversal order)
{
DynamicArray<T>* ret = NULL;
LinkQueue<BTreeNode<T>*> queue;
switch (order) {
case PreOrder:
preOrderTraversal(root(), queue);
break;
case InOrder:
inOrderTraversal(root(), queue);
break;
case PostOrder:
postOrderTraversal(root(), queue);
break;
default:
THROW_EXCEPTION(InvalidParameterException, "Parameter order is invalid ...");
break;
}
ret = new DynamicArray<T>(queue.length());
if( ret != NULL )
{
for(int i=0; i<ret->length(); i++, queue.remove())
{
ret->set(i, queue.front()->value);
}
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create return array ...");
}
return ret;
}
};
}
main.cpp测试:
#include <iostream>
#include "BTree.h"
using namespace std;
using namespace StLib;
int main()
{
BTree<int> bt;
BTreeNode<int>* n = NULL;
bt.insert(1, NULL);
n = bt.find(1);
bt.insert(2, n);
bt.insert(3, n);
n = bt.find(2);
bt.insert(4, n);
bt.insert(5, n);
n = bt.find(4);
bt.insert(8, n);
bt.insert(9, n);
n = bt.find(5);
bt.insert(10, n);
n = bt.find(3);
bt.insert(6, n);
bt.insert(7, n);
cout << bt.count() << endl;
cout << bt.height() << endl;
cout << bt.degree() << endl;
cout << "层次遍历:" << endl;
for(bt.begin(); !bt.end(); bt.next())
{
cout << bt.current() << " ";
}
cout << endl;
SharedPointer< Array<int> > sp = NULL;
sp = bt.traversal(PreOrder);
cout << "先序遍历:" << endl;
for(int i=0; i<(*sp).length(); i++)
{
cout << (*sp)[i] << " ";
}
cout << endl;
sp = bt.traversal(InOrder);
cout << "中序遍历:" << endl;
for(int i=0; i<(*sp).length(); i++)
{
cout << (*sp)[i] << " ";
}
cout << endl;
sp = bt.traversal(PostOrder);
cout << "后序遍历:" << endl;
for(int i=0; i<(*sp).length(); i++)
{
cout << (*sp)[i] << " ";
}
cout << endl;
return 0;
}
运行结果为:
10
4
2
层次遍历:
1 2 3 4 5 6 7 8 9 10
先序遍历:
1 2 4 8 9 5 10 3 6 7
中序遍历:
8 4 9 2 10 5 1 6 3 7
后序遍历:
8 9 4 10 5 2 6 7 3 1
最终的BTree.h代码:
BTree.h
#ifndef BTREE_H
#define BTREE_H
#include "Tree.h"
#include "BTreeNode.h"
#include "Exception.h"
#include "LinkQueue.h"
#include "DynamicArray.h"
namespace StLib
{
enum BTTraversal
{
PreOrder,
InOrder,
PostOrder
};
template <typename T>
class BTree : public Tree<T>
{
protected:
LinkQueue<BTreeNode<T>*> m_queue;
virtual BTreeNode<T>* find(BTreeNode<T>* node, const T& value) const
{
BTreeNode<T>* ret = NULL;
if( node != NULL )
{
if( node->value == value )
{
return node;
}
else
{
if( ret == NULL )
{
ret = find(node->left, value);
}
if( ret == NULL )
{
ret = find(node->right, value);
}
}
}
return ret;
}
virtual BTreeNode<T>* find(BTreeNode<T>* node, BTreeNode<T>* obj) const
{
BTreeNode<T>* ret = NULL;
if( node == obj )
{
return node;
}
else
{
if( node != NULL )
{
if( ret == NULL )
{
ret = find(node->left, obj);
}
if( ret == NULL )
{
ret = find(node->right, obj);
}
}
}
return ret;
}
virtual bool insert(BTreeNode<T>* n, BTreeNode<T>* np, BTNodePos pos)
{
bool ret = true;
if( pos == ANY )
{
if( np->left == NULL )
{
np->left = n;
}
else if( np->right == NULL )
{
np->right = n;
}
else
{
ret = false;
}
}
else if( pos == LEFT )
{
if( np->left == NULL )
{
np->left = n;
}
else
{
ret = false;
}
}
else if( pos == RIGHT )
{
if( np->right == NULL )
{
np->right = n;
}
else
{
ret = false;
}
}
else
{
ret = false;
}
return ret;
}
virtual void remove(BTreeNode<T>* node, BTree<T>*& ret)
{
ret = new BTree<T>();
if( ret == NULL )
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new tree ...");
}
else
{
if( root() == node )
{
this->m_root = NULL;
}
else
{
BTreeNode<T>* parent = dynamic_cast<BTreeNode<T>*>(node->parent);
if( parent->left == node )
{
parent->left = NULL;
}
else if( parent->right == node )
{
parent->right = NULL;
}
node->parent = NULL;
}
ret->m_root = node;
}
}
virtual void free(BTreeNode<T>* node)
{
if( node != NULL )
{
free(node->left);
free(node->right);
if( node->flag() )
{
delete node;
}
}
}
int count(BTreeNode<T>* node) const
{
return (node != NULL) ? (count(node->left) + count(node->right) + 1) : 0;
}
int height(BTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
int lh = height(node->left);
int rh = height(node->right);
ret = ((lh > rh) ? lh : rh) + 1;
}
return ret;
}
int degree(BTreeNode<T>* node) const
{
int ret = 0;
if( node != NULL )
{
BTreeNode<T>* child[] = { node->left, node->right };
ret = (!!node->left + !!node->right);
for(int i=0; (i<2) && (ret<2); i++)
{
int d = degree(child[i]);
if( ret < d )
{
ret = d;
}
}
}
return ret;
}
void preOrderTraversal(BTreeNode<T>* node, LinkQueue<BTreeNode<T>*>& queue)
{
if( node != NULL )
{
queue.add(node);
preOrderTraversal(node->left, queue);
preOrderTraversal(node->right, queue);
}
}
void inOrderTraversal(BTreeNode<T>* node, LinkQueue<BTreeNode<T>*>& queue)
{
if( node != NULL )
{
inOrderTraversal(node->left, queue);
queue.add(node);
inOrderTraversal(node->right, queue);
}
}
void postOrderTraversal(BTreeNode<T>* node, LinkQueue<BTreeNode<T>*>& queue)
{
if( node != NULL )
{
postOrderTraversal(node->left, queue);
postOrderTraversal(node->right, queue);
queue.add(node);
}
}
public:
bool insert(TreeNode<T>* node)
{
return insert(node, ANY);
}
virtual bool insert(TreeNode<T>* node, BTNodePos pos)
{
bool ret = true;
if( node != NULL )
{
if( this->m_root == NULL )
{
node->parent = NULL;
this->m_root = node;
}
else
{
BTreeNode<T>* np = find(node->parent);
if( np != NULL )
{
ret = insert(dynamic_cast<BTreeNode<T>*>(node), np, pos);
}
else
{
THROW_EXCEPTION(InvalidParameterException, "Invalid parent tree node ...");
}
}
}
else
{
THROW_EXCEPTION(InvalidParameterException, "Parameter node can not be NULL ...");
}
return ret;
}
bool insert(const T& value, TreeNode<T>* parent)
{
return insert(value, parent, ANY);
}
virtual bool insert(const T& value, TreeNode<T>* parent, BTNodePos pos)
{
bool ret = true;
BTreeNode<T>* node = BTreeNode<T>::NewNode();
if( node == NULL )
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new node ...");
}
else
{
node->value = value;
node->parent = parent;
ret = insert(node, pos);
if( !ret )
{
delete node;
}
}
return ret;
}
SharedPointer< Tree<T> > remove(const T& value)
{
BTree<T>* ret = NULL;
BTreeNode<T>* node = find(value);
if( node == NULL )
{
THROW_EXCEPTION(InvalidParameterException, "Can not find the tree node via value ...");
}
else
{
remove(node, ret);
m_queue.clear();
}
return ret;
}
SharedPointer< Tree<T> > remove(TreeNode<T>* node)
{
BTree<T>* ret = NULL;
node = find(node);
if( node == NULL )
{
THROW_EXCEPTION(InvalidParameterException, "Parameter node is invalid ...");
}
else
{
remove(dynamic_cast<BTreeNode<T>*>(node), ret);
m_queue.clear();
}
return ret;
}
BTreeNode<T>* find(const T& value) const
{
return find(root(), value);
}
BTreeNode<T>* find(TreeNode<T>* node) const
{
return find(root(), dynamic_cast<BTreeNode<T>*>(node));
}
BTreeNode<T>* root() const
{
return dynamic_cast<BTreeNode<T>*>(this->m_root);
}
int degree() const
{
return degree(root());
}
int count() const
{
return count(root());
}
int height() const
{
return height(root());
}
void clear()
{
free(root());
m_queue.clear();
this->m_root = NULL;
}
bool begin()
{
bool ret = (root() != NULL);
if( ret )
{
m_queue.clear();
m_queue.add(root());
}
return ret;
}
bool end()
{
return (m_queue.length() == 0);
}
bool next()
{
bool ret = (m_queue.length() > 0);
if( ret )
{
BTreeNode<T>* node = m_queue.front();
m_queue.remove();
if( node->left != NULL )
{
m_queue.add(node->left);
}
if( node->right != NULL )
{
m_queue.add(node->right);
}
}
return ret;
}
T current()
{
if( !end() )
{
return m_queue.front()->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException, "No value at current position ...");
}
}
SharedPointer< Array<T> > traversal(BTTraversal order)
{
DynamicArray<T>* ret = NULL;
LinkQueue<BTreeNode<T>*> queue;
switch (order) {
case PreOrder:
preOrderTraversal(root(), queue);
break;
case InOrder:
inOrderTraversal(root(), queue);
break;
case PostOrder:
postOrderTraversal(root(), queue);
break;
default:
THROW_EXCEPTION(InvalidParameterException, "Parameter order is invalid ...");
break;
}
ret = new DynamicArray<T>(queue.length());
if( ret != NULL )
{
for(int i=0; i<ret->length(); i++, queue.remove())
{
ret->set(i, queue.front()->value);
}
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create return array ...");
}
return ret;
}
~BTree()
{
clear();
}
};
}
#endif // BTREE_H
标签:operation count() his inter gre 数据 png 目录 思想
原文地址:https://www.cnblogs.com/PyLearn/p/10164169.html