C++ object-oriented programming - establishment of binary tree, representation and travel of binary tree (concave method, bracket method to represent binary tree, pre-order, in-order, post-order traversal of binary tree)

The realization function of the program:

  • To realize the function of constructing a binary tree from the pre-order traversal sequence and the in-order traversal sequence and from the post-order traversal sequence and the in-order traversal sequence, it requires:
  • Output this binary tree in parentheses and recessed notation.
        Test example:
        Preorder
        traversal sequence ABDEGCFHI Inorder traversal sequence DBGEACHFI Postorder
        traversal sequence DGEBHIFCA
  • Implements preorder, inorder, postorder and breadth-first traversal of non-recursive binary trees

Function implementation code:

#include<iostream>
#include<string.h>
#include<stack>
#include<queue>
#include<assert.h>
#include<fstream>
using namespace std;
template <class T>
class BinaryTree;
template <class T>
class BinaryTreeNode { //Abstract data type of binary tree node
friend class BinaryTree<T>;//Declare the binary tree as the friend class of the node class for easy access
private:
	T element;//Binary tree node data field, when implemented, you can supplement the left child node and right child node definition of private
	BinaryTreeNode<T> *left, *right;
public:
	BinaryTreeNode() {left=right=NULL;} //default constructor
	BinaryTreeNode(const T& ele) { element = ele; left = right = NULL; }//The given data constructor
	BinaryTreeNode(const T& ele, BinaryTreeNode<T>* l, BinaryTreeNode<T>* r) {//Given the node value and the constructor of the left and right subtrees
		element = ele;
		left = l;
		right = r;
	}
	T value() const {// returns the data of the current node
		return element;
	}
	BinaryTreeNode<T>* leftchild() const {return left;}//Return the current node to point to the left subtree
	BinaryTreeNode<T>* rightchild() const { return right; }//Return the current node points to the right subtree
	BinaryTreeNode<T>& operator=(const BinaryTreeNode<T>& Node) {*this=Node;}//Overload assignment operator
};
template <class T>
class BinaryTree {//Abstract data type of binary tree
private:
	BinaryTreeNode<T>* root;//Binary tree root node pointer
public:
	BinaryTree() { root = NULL; }//Constructor
	~BinaryTree() { DeleteBinaryTree(root); }//Destructor
	bool isEmpty() const; //Determine whether the binary tree is an empty tree
	void Visit(T ele) { cout << ele << " "; }
	BinaryTreeNode<T>* &Root() { return root; }//Return the root node of the binary tree
	void CreatTree(BinaryTreeNode<T> *&root);//Create a binary tree by preorder traversal;
	//void CreatTree11(BinaryTreeNode<T> *&root, string prese, string inse);//Create a binary tree according to the results of preorder traversal and inorder traversal - method 1
	void CreatTree11(BinaryTreeNode<T> *&root,T *prese, T *inse,int length);//Create a binary tree according to the results of preorder traversal and inorder traversal - method 2
	void CreatTree22(BinaryTreeNode<T> *&root, T *post, T* inse,int length);//Create a binary tree according to the results of post-order traversal and in-order traversal
	void PreOrderWithoutRecusion(BinaryTreeNode<T>* root);//Preorder travel around the binary tree or its subtree
	void InOrderWithoutRecusion(BinaryTreeNode<T>* root);//Inorder travel around the binary tree or its subtree
	void PostOrderWithoutRecusion(BinaryTreeNode<T>* root);//Post-order travel around the binary tree or its subtree
	void DeleteBinaryTree(BinaryTreeNode<T>* root);//Delete a binary tree or its subtree
	void display11(ofstream &of,BinaryTreeNode<T> *root);//Bracket method to represent binary tree and save
	void display22(ofstream &out,BinaryTreeNode<T> *root);//The concave method represents the binary tree and saves it
	};
	template<class T>
	bool BinaryTree<T>::isEmpty() const { //Determine whether the binary tree is an empty tree
		return (root != NULL ? false : true);
	}
	template<class T>
	void BinaryTree<T>::CreatTree11(BinaryTreeNode<T>*&root, T *prese, T *inse, int length) {
		if (length <= 0) { root = NULL; return; }
		T rt = prese[0];
		int i = 0;
		while (i < length &&inse[i] != rt) { i++; }
		int left_length = i;
		int right_length = length - left_length - 1;
		root = new BinaryTreeNode<T>(rt);
		if (left_length > 0)
			CreatTree11(root->left, prese + 1, inse, left_length);
		if (right_length > 0)
			CreatTree11(root->right, prese + length - right_length, inse + length - right_length, right_length);
	}
	template<class T>
	void BinaryTree<T>::CreatTree22(BinaryTreeNode<T>*&root,T *post, T* inse,int length) {
		int n =length;
		if (n <= 0) { root = NULL; return; }
		T rt = post[n-1];
		int i=0;
		while (i < n &&inse[i] != rt) { i++; }//The position of the root in the inorder
		int left_length = i;
		int right_length = n-left_length-1;
		root = new BinaryTreeNode<T>(rt);
		if(left_length>0)
			CreatTree22(root->left,post,inse,left_length);
		if(right_length>0)
			CreatTree22(root->right, post+left_length, inse+length-right_length,right_length);
	}
	template<class T>
	void BinaryTree<T>::display11(ofstream &of,BinaryTreeNode<T> *root) //The concave method displays the tree structure
	{
		if (root != NULL){
			cout << root->element;
			of<< root->element;
			if (root->left != NULL){
			   	cout << '(';of << "(";
			    display11(of,root->left);
			   }
			else if (root->right != NULL) {	cout << "(#";	of << "(#";	}
			if (root->right != NULL){	
				cout << ',';of << ",";
				display11(of,root->right);
				cout << ')';of << ")";
			}
			else if (root->left != NULL) {cout << ",#)"; of << ",#)";}
		}
	}
	template<class T>
	void BinaryTree<T>::display22(ofstream &out,BinaryTreeNode<T> *root) {
		if (root != NULL) {
			cout << root->element;
			out<< root->element;
			if (root->left == NULL) {
				cout << "#";
				out << "#";
			}
			else
				display22(out,root->left);
			if (root->right == NULL) {
				cout << "#";
				out << "#";
			}
			else
				display22(out,root->right);
		}
	}
	template<class T>
	void BinaryTree<T>::DeleteBinaryTree(BinaryTreeNode<T>* root) {//Delete a binary tree or its subtrees in a post-order tour
		if (root != NULL) {
			DeleteBinaryTree(root->left);//Recursively delete the left subtree
			DeleteBinaryTree(root->right);//Recursively delete the right subtree
			delete root;//Delete the root node
		}
	}
	template<class T >
	void BinaryTree<T>::PreOrderWithoutRecusion(BinaryTreeNode<T>* root) {//Non-recursive preorder traversal of binary tree or its subtree
using std::stack; //Use the stack in STL
stack< BinaryTreeNode<T> * > aStack;
BinaryTreeNode<T> * pointer = root;
aStack.push(NULL);
while (pointer) {
	Visit(pointer->value());//Visit the current node
	if (pointer->rightchild() != NULL)
		aStack.push(pointer->rightchild());//Non-empty right child into the stack
	if (pointer->leftchild() != NULL)
		pointer = pointer->leftchild();//The current link structure points to the left child
	else {//The left subtree is visited, turn to visit the right subtree
		pointer = aStack.top();
		aStack.pop(); //The top element of the stack is unstacked
	}
}
	}
	template<class T>
	void BinaryTree<T>::InOrderWithoutRecusion(BinaryTreeNode<T>* root) {//Non-recursive in-order traversal of a binary tree or its subtree
		using std::stack; //Use the stack in STL
		stack<BinaryTreeNode<T>* > aStack;
		BinaryTreeNode<T>* pointer = root;
		while (!aStack.empty() || pointer) {
			if (pointer) {
				aStack.push(pointer);//The current node address is pushed into the stack
				pointer = pointer->leftchild();//The current link structure points to the left child
			}//end if
			else {//The left subtree is visited, turn to visit the right subtree
				pointer = aStack.top();
				Visit(pointer->value());//Visit the current node
				pointer = pointer->rightchild();//The current link structure points to the right child
				aStack.pop(); //The top element of the stack is unstacked
			}//end else
		} //end while
	}
	enum Tags { Left, Right }; //Definition of feature identification
	template <class T>
	class StackElement {//The definition of stack element
	public:
		BinaryTreeNode<T>* pointer;//Link to binary tree node
		Tags tag;//Character identification declaration
	};
	template<class T>
	void BinaryTree<T>::PostOrderWithoutRecusion(BinaryTreeNode<T>* root) {//Non-recursive post-order traversal of binary tree or its subtree
		using std::stack;//Use the STL stack part
		StackElement<T> element;
		stack<StackElement<T> > aStack;//栈申明
		BinaryTreeNode<T>* pointer;
		if (root == NULL)
			return;//Empty tree is returned
		else
			pointer = root;
		while (!aStack.empty() || pointer) {//Enter the left subtree   
			while (pointer != NULL) {
				element.pointer = pointer;
				element.tag = Left;
				aStack.push(element); //Walk down the left subtree    
				pointer = pointer->leftchild();
			}
			element = aStack.top();//Put out the top element of the stack
			aStack.pop();
			pointer = element.pointer;
			// return from the right subtree
			if (element.tag == Left) {
				element.tag = Right;
				aStack.push(element); //Turn to visit the right subtree
				pointer = pointer->rightchild();
			}
			else {
				Visit(pointer->value());
				pointer = NULL;
			}
		}
	}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324399976&siteId=291194637