2021-07-24二叉树


前言


一、二叉树的相关概念与性质

  1. 每个节点都有一个活多个子节点

  2. 每个没有父节点的节点成为根节点

  3. 每一个非根节点有且只有一个父节点

  4. 除了根节点外,每个子节点可以分为多个不相交的子树

  5. 节点的度:节点拥有的子树的数目

  6. 叶子:度为零的节点

  7. 分支节点:度不为零的节点

  8. 树的度:树中结点的最大度

  9. 层次:根结点的层次为1,其余节点的层次等于该节点的双亲结点的层次加一

  10. 树的高度:树中结点的最大层次加一。

  11. 无序树:如果书中结点的格子数之间的次序是不重要的,可以交换位置。

  12. 有序树:如果树中结点的格子数之间的次序是重要的,不可以交换位置。

  13. 森林:0个或多个不相交的树组成。对森林加上一个根,森林即为树;山区跟,树几位森林。

  • 性质1:二叉树第i层上的结点数目最多为2^(i-1)(i>=1);
  • 性质2:深度为k的二叉树至多又2^k-1个结点(k>=1);
  • 性质3:包含n个结点的二叉树的高度至少为log2(n+1).
  • 性质4:在任意一颗二叉树中,若终端你节点的个数为n0,度为2的节点数为n2,则n0=n^2+1。

二、代码

#pragma once
#include <iostream>
#include <vector>
#include<stack>
#include<queue>
using namespace std;

class TreeNode {
public:
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class BTree {
public:
	TreeNode* root;

	void buildTree(vector<int>& a);
	TreeNode* getRoot();
	void firstRoot(TreeNode* node);
	void midRoot(TreeNode* node);
	void backRoot(TreeNode* node);
	//非递归方法遍历二叉树
	void firstRootno(TreeNode* node);
	void midRootno(TreeNode* node);
	void backRootno(TreeNode* node);
	void layerOrder(TreeNode* root);

};
#include"BTree.h"

void BTree::buildTree(vector<int>& a) {
	if (a.size()==0) {
		return;
	}
	root = new TreeNode(a[0]);
	for (int i = 1; i < a.size(); i++) {
		TreeNode* newNode = new TreeNode(a[i]);
		TreeNode* node = root;
		while (node) {
			if (node->val > a[i]) {
				if (node->left == NULL) {
					node->left = newNode;
					break;
				}
				node = node->left;
			}
			else {
				if (node->right == NULL) {
					node->right = newNode;
					break;
				}
				node = node->right;
			}
		}
	}

}

//递归遍历二叉树
void BTree::firstRoot(TreeNode* node) {
	if (node==nullptr) {
		return;
	}
	cout << node->val << endl;
	firstRoot(node->left);
	firstRoot(node->right);
}
void BTree::midRoot(TreeNode* node) {
	if (node==NULL) {
		return;
	}
	
	midRoot(node->left);
	cout << node->val << endl;
	midRoot(node->right);
}
void BTree::backRoot(TreeNode* node) {
	if (!node) {
		return;
	}
	
	backRoot(node->left);
	backRoot(node->right);
	cout << node->val << endl;
}

TreeNode* BTree::getRoot() {
	return root;
}

void BTree::firstRootno(TreeNode* node) {
	stack<TreeNode*> stk;
	stk.push(node);
	while (!stk.empty()) {
		TreeNode* cur = stk.top();
		stk.pop();
		cout << cur->val;
		if (cur->right) {
			stk.push(cur->right);
		}
		if (cur->left) {
			stk.push(cur->left);
		}

	}

}

void BTree::midRootno(TreeNode* head) {
	stack<TreeNode*> stk;
	TreeNode* cur = head;
	while (!stk.empty() || cur) {
		while (cur) {
			stk.push(cur);
			cur = cur->left;
		}
		TreeNode* node = stk.top();
		stk.pop();
		cout << node->val << endl;
		cur = node->right;
	}
}

void BTree::backRootno(TreeNode* node) {
	stack<TreeNode*> s1;
	stack<TreeNode*> s2;
	TreeNode* cur = node;
	s1.push(node);
	while (!s1.empty()) {
		cur = s1.top();
		s2.push(cur);
		s1.pop();
		if (cur->left) {
			s1.push(cur->left);
		}
		if (cur->right) {
			s1.push(cur->right);
		}
	}
	while (!s2.empty()) {
		cout << s2.top()->val << endl;
		s2.pop();
	}
	
	
}
//
//void BTree::backRootno(TreeNode* node) {
//	stack<TreeNode*> s1;
//
//	TreeNode* h, * c;
//	h = node;
//	c = NULL;
//	s1.push(node);
//	while(!s1.empty()){
//		c = s1.top();
//		if (c->left && h != c->left && h != c->right) {
//			s1.push(c->left);
//		}
//		else if(c->right&&h!=c->right){
//			s1.push(c->right);
//		}
//		else {
//			cout << s1.top()->val << endl;
//			s1.pop();
//			h = c;
//		}
//	}
//
//}
void BTree::layerOrder(TreeNode* root) {
	if (root == nullptr) {
		return;
	}
	queue<TreeNode*> que;
	que.push(root);
	TreeNode* last = root;
	TreeNode* nlast = nullptr;
	while (!que.empty()) {
		TreeNode* cur = que.front();
		cout << cur->val << " ";
		que.pop();
		if (cur->left) {
			que.push(cur->left);
			nlast = cur->left;
		}
		if (cur->right) {
			que.push(cur->right);
			nlast = cur->right;
		}
		if (cur == last) {
			last = nlast;
			cout << endl;
		}
	}
}

主函数


int main() {

	vector<int> m = { 5,4,7,2,1,3,8,6,9 };
	BTree tree;
	tree.buildTree(m);
	TreeNode* root = tree.getRoot();
	tree.backRoot(root);
	cout << endl;
	tree.layerOrder(root);
	/*int m[] = {4 ,4,7,2,1,3,8,6,9 };
	quickSort2(m, 0, 8);
	for (int i = 0; i < 9; i++) {
		cout << m[i] << " ";
	}*/

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值