binaryTree

#include<iostream>
using namespace std;
//define the node of a tree
struct Node{
	int data;
	Node* leftchild;
	Node* rightchild;
	Node() { leftchild = rightchild = nullptr, data = -1; };
};

//Build a full BinaryTree,4 levels,data >= 0,data++
int Data = 1;
void BuildTree(Node* &root,int level = 1) {
	if (level < 4) {
		level++;
		Node* left = new Node, *right = new Node;
		left->data = Data++, right->data = Data++;
		root->leftchild = left, root->rightchild = right;
		BuildTree(root->leftchild, level + 1);
		BuildTree(root->rightchild, level + 1);
	}
}

void Inorder(Node* &root) {
	if (root) {
		Inorder(root->leftchild);
		cout << root->data << " ";
		Inorder(root->rightchild);
	}
}

void Preorder(Node* &root) {
	if (root) {
		cout << root->data << " ";
		Preorder(root->leftchild);
		Preorder(root->rightchild);
	}
}

void Output(Node* & root) {
	Inorder(root);
	cout << endl;
}

//to make ptr points a new node same as node
void CopyNode(Node* &ptr, Node* &node) {
	Node * temp = new Node;
	temp = node;
	ptr = temp;
}
//inorder to copy the tree
void CopyTree(Node* &root, Node* &newRoot) {
	if (root) {
		CopyNode(newRoot->leftchild, root->leftchild);
		CopyTree(root->leftchild, newRoot->leftchild);
		CopyNode(newRoot->rightchild, root->rightchild);
		CopyTree(root->rightchild, newRoot->rightchild);
	}
}

//preorder and inorder ensure the tree unique
void test_for_copy(Node* &oldRoot,Node* &newRoot) {
	cout << "Old tree's inorder: ";
	Inorder(oldRoot);
	cout << endl << "New tree's inorder: ";
	Inorder(newRoot);
	cout <<endl << "Old tree's preorder: ";
	Preorder(oldRoot);
	cout << endl << "New tree's preorder: ";
	Preorder(newRoot);
	cout << endl;
}

void change(Node* &left, Node* &right) {
	Node* temp = left;
	left = right;
	right = temp;
}
//preorder's strategy
void interchange(Node* &root) {
	if (root) {
		change(root->leftchild, root->rightchild);
		interchange(root->leftchild);
		interchange(root->rightchild);
	}
}

//root->left->root->right
bool flag = true;
void double_order(Node* &root) {
	if (root) {
		cout << root->data << " ";
		double_order(root->leftchild);
		cout << root->data << " ";
		double_order(root->rightchild);
	}
}

// level's width = the sum of (level-1)'s node's children
//compute each level's width
int Width(Node* &root,int level = 1) {
	if (level == 1) {
		if (root->leftchild != nullptr&&root->rightchild != nullptr)
			return 2;
		else if (root->leftchild == nullptr&&root->rightchild == nullptr)
			return 0;
		else
			return 1;
	}
	else {
		return Width(root->leftchild, level - 1) + Width(root->rightchild, level - 1);
	}
}
void max_width(Node* &root, int level = 3) {
	int max = 0;
	for (int i = 1; i <= 3; ++i)
		max = max > Width(root, i) ? max : Width(root, i);
	cout << "Max width is " << max << endl;
}
void test_for_swap(Node* &root) {
	cout << "The original tree: " << endl;
	cout << "  Inorder:\t";
	Inorder(root);
	cout << endl << "  Preorder:\t";
	Preorder(root);
	cout << endl;
	interchange(root);
	cout << "The swap tree: " << endl;
	cout << "  Inorder:\t";
	Inorder(root);
	cout << endl << "  Preorder:\t";
	Preorder(root);
	cout << endl;
}

//inorder to traverse and find the node, then link it
//return -1 to alert,0 for succ
// node to insert on ptr's right
int insert(Node* ptr,Node* & node,Node* &root) {
	if (root) {
		if (root == ptr) {
			if (root->rightchild == nullptr) {
				root->rightchild = node;
				return 0;
			}
			else 
				return -1;
		}
		else {
			if (insert(ptr, node, root->leftchild) == 0)
				return 0;
			else
				return insert(ptr, node, root->rightchild);
		}
	}
}

//return a ptr,most right
Node* rePtr(Node* root) {
	while (root->rightchild)
		root = root->rightchild;
	return root;
}

void test_for_insert(Node*& root) {
	Node* ptr = rePtr(root);
	cout << "The original tree: " << endl;
	cout << "  Inorder:\t";
	Inorder(root);
	cout << endl << "  Preorder:\t";
	Preorder(root);
	cout << endl;
	Node* temp = new Node;
	insert(ptr, temp, root);
	cout << "The inserted tree: " << endl;
	cout << "  Inorder:\t";
	Inorder(root);
	cout << endl << "  Preorder:\t";
	Preorder(root);
	cout << endl;
}
int main() {
	Node* root = new Node;
	root->data = 0;
	BuildTree(root);
	//Output(root);
	Node* newRoot = new Node;
	newRoot = root;
	CopyTree(root, newRoot);
	//test_for_copy(root, newRoot);
	//test_for_swap(newRoot);
	//double_order(root);
	//max_width(root, 3);
	test_for_insert(root);
	cout << endl;
	system("pause");
	return 0;
}

posted @ 2021-04-26 22:41  关河梦断  阅读(90)  评论(0)    收藏  举报