树的深度自顶向下计算,树的高度自底向上计算。

1. 二叉树

1.1 二叉树定义

struct TreeNode{
	ElementType data;
	TreeNode *leftChild;
	TreeNode *rightChild
}

1.2 其他理论部分

二叉树不等于度为2的树,二叉树的左右子树有严格的定义

1.3 完全二叉树

满二叉树:每一层的结点个数都达到了当层能达到的最大结点数

完全二叉树:除了最下面一层外,其余层的结点个数都达到了当层能达到的最大结点数

  • 完全二叉树的左孩子是2x,右孩子是2x+1,

  • 完全二叉树可以用一个大小为2的k次方的数组来存放

  • 存放顺序恰好是层次遍历的顺序

  • 判断某个结点是否为叶结点:该结点的左子结点的编号root*2大于结点总个数n

  • 判断某个结点是否为空结点的标志为:该结点下标root大于结点总个数n

1.4 二叉树遍历

先序遍历

void PreOrder(TreeNode* root){
	if(root == NULL){
		return;
	}
	Visit(root->data);
	PreOrder(root->leftChild);
	PreOrder(root->rightChild);
	return;
}

中序遍历

void InOrder(TreeNode* root){
	if(root == NULL){
		return;
	}
	InOrder(root->leftChild);
	Visit(root->data);
	InOrder(root->rightChild);
	return;
}

后序遍历

void PostOrder(TreeNode* root){
	if(root == NULL){
		return;
	}
	PostOrder(root->leftChild);
	PostOrder(root->rightChild);
	Visit(root->data);
	return;
}

层次遍历

void LevelOrder(TreeNode* root){
	queue<TreeNode*> myQueue;
	if(root != NULL){
		myQueue.push(root);
	}
	while(!myQueue.empty()){
		TreeNode* current = myQueue.front();
		myQueue.pop();
    visit(current->data);
    if(current->leftChild != NULL){
    	myQueue.push(current->leftChild);
    }
    if(current->rightChild != NULL){
    	myQueue.push(current->rightChild);
    }
	}
	return;
}

重构二叉树

先序遍历+中序遍历 二叉树遍历(华中科技大学)
vector<int> pre,inL;   //pre,inL已经放好了序列
Node* creat(int preL,int preR,int inL,int inR){
	if(preL>preR)
		return NULL;
	Node* root=new Node();
	root->data=pre[preL];
	int k;
	for(k=inL,k<=inR;k++){
		if(in[k]==pre[preL])
			break;
	}
	int numLeft = k-inL;
	root->lchild = creat(preL+1,preL+numLeft,inL,k-1);
	root->rchild=creat(preL+numLeft+1,preR,k+1,inR);
	return root;
}
后序遍历+中序遍历 Tree Traversals(A1020)
vector<int> pre,inL;   //pre,inL已经放好了序列
Node* creat(int postL,int postR,int inL,int inR){
	if(postL>postR)
		return NULL;
	Node* root=new Node();
	root->data=post[postR];
	int k;
	for(k=inL,k<=inR;k++){
		if(in[k]==post[postR])
			break;
	}
	int numLeft = k-inL;
	root->lchild = creat(postL,postL+numLeft-1,inL,k-1);
	root->rchild=creat(postL+numLeft,postR-1,k+1,inR);
	return root;
}
层序遍历+中序遍历

1.5 应用练习

Tree Traversals Again(A1086)

Invert a Binary Tree(A1102)

二叉树遍历(清华大学)

2.树

2.1 树的先根遍历(DFS)

void PreOrder(int root){
	printf("%d ", Node[root].data);
  for(int i = 0; i < Node[root].child.size(); i++){
		PreOrder(Node[root].child[i]);
  }
}

2.2 树的层次遍历(BFS)

void LayerOrder(int root){
	queue<int> Q;
	Q.push(root);
	while(!Q.empty()){
		int front = Q.front();
		printf("%d ", Node[front].data);
		Q.pop();
		for(int i = 0; i < Node[front].child.size(); i++){
			Q.push(Node[front].child[i]);
		}
	}
}

2.3 应用练习(考虑结合BFS,DFS)

BFS,DFS遍历vector Childs

Total Sales of Supply Chain(A1079)

Highest Price in Supply Chain(A1090)

Lowest Price in Supply Chain(A1106)

The Largest Generation(A1094)

Counting Leaves(A1004)

Path of Equal Weight(A1053)

3.二叉排序树

3.1查找

void search(node* root, int x){
	if(root == NULL){
		printf("search failed\n");
		return;
	}
	if(x == root->data){
		printf("%d\n", root->data);
	}else if(x < root->data){
		search(root->lchild, x);
	}else{
		search(root->rchild, x);
	}
}

3.2插入

void insert(node* &root, int x){
	if(root == NULL){
		root = newNode(x);
		return;
	}
	if(x == root->data){
		return;
	} else if(x < root->data){
		insert(root->lchild, x);
	} else{
		insert(root->rchild, x);
	}
}

3.3建立

反复插入即可,但是即便是同一组数字,如果插入顺序不同,最后生成的二叉查找树也可能不同。

3.4 删除

  • 寻找以root为根结点的树中的最大权值结点
node* findMax(node* root){
	while(root->rchild != NULL){
		root = root->rchild;
	}
	return root;
}
  • 寻找以root为根结点的树中的最小权值结点
node* findMin(node* root){
	while(root->lchild != NULL){
		root = root->lchild;
	}
	return root;
}
  • 删除以root为根结点的树中权值为x的结点
void deleteNode(node* &root, int x){
	if(root == NULL) // 不存在权制为x的结点 
		return;
	if(root->data == x){
		if(root->lchild == NULL && root->rchild == NULL){ // root为叶子结点,直接删除
			root = NULL;
		} else if (root->lchild != NULL){
			node* pre = findMax(root->lchild);
			root->data = pre->data;
			deleteNode(root->lchild, pred->data);
		} else {
			node* next = findMin(root->rchild);
			root->data = next->data;
			deleteNode(root->rchild, next->data);
		}
	} else if(root->data > x){
		deleteNode(root->lchild, x);
	} else {
		deleteNode(root->rchild, x);
	}
}

3.5应用练习

Is It a Binary Search Tree(A1043)

Complete Binary Search Tree (A1064)

二叉排序树(华中科技大学)

二叉排序树(华中科技大学)

二叉搜索树(浙江大学)

注:由先序遍历和中序遍历可以唯一确定一颗二叉树,数字相同的序列构成的二叉搜索树的中序遍历必定一样,因此只要判断其先序遍历是否一样即可

4.平衡二叉树

4.1 查找

跟二叉排序树的相同

4.2 插入

  • 左旋
void L(node* &root){
	node* temp = root->rchild;
	root->rchild = temp->lchild;
	temp->lchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}
  • 右旋
void R(node* &root){
	node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateHeight(root);
	updateHeight(temp);
	root = temp;
}
  • 插入
void insert(node* &root, int v){
	if(root == NULL){
		root = newNode(v);
		return;
	}
	if(v < root->v){
		insert(root->lchild, v);
		updateHeight(root);
		if(getBalanceFactor(root) == 2){
			if(getBalanceFactor(root->lchild) == 1){
				R(root);
			} else if(getBalanceFactor(root->lchild) == -1){
				L(root->lchild);
				R(root);
			}
		}
	}else{
    insert(root->rchild, v);
    updateHeight(root);
		if(getBalanceFactor(root) == -2){
			if(getBalanceFactor(root->rchild) == -1){
				L(root);
			} else if(getBalanceFactor(root->rchild) == 1){
				R(root->rchild);
				L(root);
			}
		}
  }
}

4.3 练习

Root of AVL Tree(A1066)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值