[数据结构] 二叉树及其编码实现

1.几个易混淆的概念

:节点的有限结合。如下:
在这里插入图片描述
二叉树:所有结点的度都小于等于2的树。如下:
在这里插入图片描述
完全二叉树:叶子节点只能出现在最下层和次下层,并且最下面一层的结点都集中在该层最左边的若干位置的二叉树。如下:
在这里插入图片描述
完全二叉树的特性
完全二叉树是效率很高的数据结构,具有广泛的应用。
完全二叉树是一种特殊的二叉树,每一个元素与其子节点(孩子)有非常好的对应关系。对于编号为i的元素,有如下关系
(1)若i>1,则该元素的父节点为[i/2]
(2)若 2i <= n,则左孩子的编号为2i
(3)若2i+1 <= n ,则右孩子的编号为2i+1

满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。如下:
在这里插入图片描述
包含关系:树 > 二叉树 > 完全二叉树 > 满二叉树

2.二叉树的遍历方式

在这里插入图片描述

3. 二叉树的数组描述(编码)

编码中由于将根节点索引设为0,故此例中节点索引与第一节中满二叉树的节点关系特性稍微有所变化。
数组描述较为简单,当然功能也单一。主要就是建立一个一维数组,数组下标代表节点索引。

Tree.h


#ifndef TREE_H
#define TREE_H
class Tree
{
public:
	Tree(int size,int *pRoot);//创建树
	~Tree();//销毁树
	int *SearchNode(int nodeIndex);//根据索引寻找节点
	bool AddNode(int nodeIndex, int direction, int *pNode);//添加节点
	bool DeleteNode(int nodeIndex, int *pNode); //删除节点
	void TreeTraverse();//遍历节点
private:
	int *m_pTree;
	int m_iSize;
};
#endif

Tree.cpp

#include"Tree.h"
#include<iostream>
using namespace std;

Tree::Tree(int size, int *pRoot)
{
	m_iSize = size;
	m_pTree = new int[size];
	for (int i = 0; i < size; i++)
	{
		m_pTree[i] = 0;
	}
	m_pTree[0] = *pRoot;
}

Tree::~Tree()
{
	delete[]m_pTree;
	m_pTree = NULL;
}

int *Tree::SearchNode(int nodeindex)
{
	if (nodeindex < 0 || nodeindex >= m_iSize)
	{
		return NULL;
	}
	if (m_pTree[nodeindex] == 0)
	{
		return NULL;
	}
	return &m_pTree[nodeindex];//由索引取值后取地址
}

bool Tree::AddNode(int nodeindex, int direction, int *pNode)
// direction决定往左插,还是往右插。
{
	if (nodeindex < 0 || nodeindex >= m_iSize || m_pTree[nodeindex] == 0)
	//节点合法性
	{
		return false;
	}
	switch (direction)
	{
	// 0值定义为左孩子
	case 0:
		// 不等于0,说明插入过了,这里我们的处理是,如果插过了,不允许替换。
		if (nodeindex * 2 + 1 >= m_iSize || m_pTree[nodeindex * 2 + 1] != 0)
		{
			return false;
		}
		m_pTree[nodeindex * 2 + 1] = *pNode;
		break;
	case 1:
		if (nodeindex * 2 + 2 >= m_iSize || m_pTree[nodeindex * 2 + 2] != 0)
		{
			return false;
		}
		m_pTree[nodeindex * 2 + 2] = *pNode;
		break;
	}
	return true;
}

bool Tree::DeleteNode(int nodeindex, int * pNode)
{
	if (nodeindex < 0 || nodeindex >= m_iSize)
	{
		return false;
	}
	if (m_pTree[nodeindex] == 0)
	{
		return false;
	}
	*pNode = m_pTree[nodeindex];
	m_pTree[nodeindex] = 0;

	return true;
}

void Tree::TreeTraverse()
{
	for (int i =0;i<m_iSize;i++)
	{
		cout << m_pTree[i] << " ";
	}
}

在主函数中测试
main.cpp

#include <iostream>
#include <stdlib.h>
#include "Tree.h"
using namespace std;
/********************************************************************
              数组---树  Tree【】=3 5 8 2 6 9 7
              3(0)                                左孩子小标=父节点下标*2+1
    5(1)               8(2)                       右孩子小标=父节点下标*2+2
 2(3)      6(4)  9(5)        7(6)
 
 **********************************************************************/
int main()
{
    int root = 3;
    Tree *pTree = new Tree(10, &root);
    int node1 = 5;
    int node2 = 8;
    //0号节点插入左孩子。
    pTree->AddNode(0, 0, &node1);
    //0号节点插入右孩子。
    pTree->AddNode(0, 1, &node2);
    
    int node3 = 2;
    int node4 = 6;
    int node5 = 9;
    int node6 = 7;
    pTree->AddNode(1, 0, &node3);
    pTree->AddNode(1, 1, &node4);
    pTree->AddNode(2, 0, &node5);
    pTree->AddNode(2, 1, &node6);
    
    pTree->TreeTraverse();
    
    // 传入节点值,找到index
    int *p = pTree->SearchNode(2);
    cout << endl;
    cout << "***************"<<endl;
    cout << "index:" << *p<<endl;
    cout << "***************"<<endl;
    // 删除传入index
    int temp;
    pTree->DeleteNode(6, &temp);
    cout << endl;
    cout << "delete node=" << temp << endl;
    
    pTree->TreeTraverse();
    cout << endl;
    delete pTree;
    pTree = NULL;
    
    return 0;
}

运行结果:
在这里插入图片描述

4. 二叉树的链表描述(编码)

结点要素:索引、数据、左孩子指针、右孩子指针
Node.h

#ifndef NODE_H
#define NODE_H

class Node
{
public:
	Node();
	
	Node *SearchNode(int nodeIndex);
	void DeleteNode();
	void PreorderTraversal();   // 前序遍历
	void InorderTraversal();    // 中序遍历
	void PostorderTraversal();  // 后序遍历
	int index; //索引
	int data; // 数据
	Node *pLChild; // 左孩子指针
	Node *pRChild; // 右孩子指针
	Node *pParent; // 父节点指针
};

#endif

Node.cpp

#include "Node.h"
#include <iostream>
using namespace std;
Node::Node()
{
	index = 0;
	data = 0;
	pLChild = NULL;
	pRChild = NULL;
	pParent = NULL;
}

/*
*
*/
Node *Node::SearchNode(int nodeIndex)
{
  // 看自己是不是
	if (this->index == nodeIndex)
	{
		return this;
	}
	// 左右节点是不是
	Node *temp = NULL;
	if (this->pLChild != NULL)
	{
		if (this->pLChild->index == nodeIndex)
		{
			return this->pLChild;
		}
		// 注意没找到的情况继续往下找
		else
		{
			temp = this->pLChild->SearchNode(nodeIndex);
			if (temp != NULL)
			{
				return temp;
			}
		}
	}
	if (this->pRChild != NULL)
	{
		if (this->pRChild->index == nodeIndex)
		{
			return this->pRChild;
		}
		// 注意没找到的情况继续往下找
		else
		{
			temp = this->pRChild->SearchNode(nodeIndex);
			if (temp != NULL)
			{
				return temp;
			}
		}

	}

	return NULL;
}

void Node::DeleteNode()
{
	if (this->pLChild != NULL)
	{
		this->pLChild->DeleteNode();
	}
	if (this->pRChild != NULL)
	{
		this->pRChild->DeleteNode();
	}
	if (this->pParent != NULL)
	{
	  // 判断自己是父节点的左孩子还是右孩子。
		if (this->pParent->pLChild == this)
		{
			this->pParent->pLChild = NULL;
		}
		if (this->pParent->pRChild == this)
		{
			this->pParent->pRChild = NULL;
		}
	}
  // 自杀
	delete this;

}
//先自己,然后左边然后右边
void Node::PreorderTraversal()
{
	cout << this->index << " " << this->data << endl;
	if (this->pLChild != NULL)
	{
		this->pLChild->PreorderTraversal();
	}
	if (this->pRChild != NULL)
	{
		this->pRChild->PreorderTraversal();
	}
}

void Node::InorderTraversal()
{
	
	if (this->pLChild != NULL)
	{
		this->pLChild->InorderTraversal();
	}
	cout << this->index << " " << this->data << endl;
	if (this->pRChild != NULL)
	{
		this->pRChild->InorderTraversal();
	}
}
void Node::PostorderTraversal()
//后序遍历
{
	if (this->pLChild != NULL)
	{
		this->pLChild->PostorderTraversal();
	}
	if (this->pRChild != NULL)
	{
		this->pRChild->PostorderTraversal();
	}
	cout << this->index << " " << this->data << endl;
}

Tree.h


#ifndef TREE_H
#define TREE_H
#include "Node.h"

class Tree
{
public:
	Tree(); //创建树
	~Tree();//销毁树 del node 删除最根节点即可
	Node *SearchNode(int nodeIndex); //搜索节点
	bool AddNode(int nodeIndex, int direction, Node *pNode);//搜索节点基础上添加
	bool DeleteNode(int nodeIndex, Node *pNode);//删除结点
	void PreorderTraversal();   //前序遍历
	void InorderTraversal();    //中序遍历
	void PostorderTraversal();  //后序遍历

private:
	Node *m_pRoot;
};

#endif

Tree.cpp:


#include "Tree.h"
#include <iostream>
using namespace std;
Tree::Tree()
{
	m_pRoot = new Node();// 第一个节点
}

Tree::~Tree()
{
	DeleteNode(0, NULL);
	//m_pRoot->DeleteNode();
}

//删除结点为头结点。整棵树销毁
//删除和添加都需要找到节点

Node *Tree::SearchNode(int nodeIndex)
{
	return m_pRoot->SearchNode(nodeIndex);
}


bool Tree::AddNode(int nodeIndex, int direction, Node *pnode) {
	Node *temp = SearchNode(nodeIndex);
	// 挂载节点不存在
	if (temp == NULL)
	{
		return false;
	}

	Node *node = new Node();
	if (node == NULL)
	{
		return false;
	}
	// 要把pnode值保存下来。
	node->index = pnode->index;
	node->data = pnode->data;
	node->pParent = temp; //注意,要在添加时把父节点也记着登记上。
	
	//0挂左,1挂右
	if (direction == 0)
	{
		temp->pLChild = node;
	}
	if (direction == 1)
	{
		temp->pRChild = node;
	}
	
	return true;
};

// 删除节点(级联删除子节点)& 析构函数

bool Tree::DeleteNode(int nodeIndex, Node *pNode)
{
	Node *temp = SearchNode(nodeIndex);
	if (temp == NULL)
	{
		return false;
	}
	if (pNode != NULL)
	{
		pNode->data = temp->data;
	}
	// 把树中删除节点的操作下移到node中来解决
	temp->DeleteNode();
	return true;

}

void Tree::PreorderTraversal()
{
	m_pRoot->PreorderTraversal();
}
void Tree::InorderTraversal()
{
	m_pRoot->InorderTraversal();
}
void Tree::PostorderTraversal()
{
	m_pRoot->PostorderTraversal();
}

5.参考

《慕课网》
Programming-Notebook

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章