【数据结构】二叉树[通俗易懂]

【数据结构】二叉树

大家好,又见面了,我是全栈君。

二叉树节点

#pragma once
#include <stdlib.h>
template<class T>class BinaryTreeNode
{
public:
	T data;
	BinaryTreeNode<T>* leftchild;
	BinaryTreeNode<T>* rightchild;
	BinaryTreeNode():leftchild(NULL),rightchild(NULL){}
	BinaryTreeNode(T d):data(d),leftchild(NULL),rightchild(NULL){}
};

二叉树的全部操作:建树。销毁树,先序后序中序便利的递归和非递归方式层序遍历

#include "BinaryTreeNode.h"
#include <queue>
#include <stack>
#include <iostream>
using namespace std;

/*
void CreatePreOrder(BinaryTreeNode<T>** root)
void LevelOrderTraverse()//层序遍历
void PreOrderTraverse(BinaryTreeNode<T>* root)//先序遍历递归
void PreOrderTraverse()//先序遍历非递归算法
void InOrderTraverse()//中序遍历的非递归方法
void PostOrderTraverse()//后序遍历非递归方法
int depth()//树深度
BinaryTreeNode<T>* Parent(BinaryTreeNode<T>*root,BinaryTreeNode<T>*node)//返回节点的双亲
BinaryTreeNode<T>* SiblingNode(BinaryTreeNode<T>*node)//返回兄弟节点
*/

template<class T>class BinaryTree
{
public:
	BinaryTreeNode<T>* root;
	BinaryTree():root(NULL){}//空树
/********************************先序创建链表****************************************/
	void CreatePreOrder(BinaryTreeNode<T>** root)//或者BinaryTreeNode<T>* &root以下依次改变
	{
		char d;
		cin>>d;
		if (d=='#')
			(*root) = NULL;
		else
		{
			*root = new BinaryTreeNode<T>(d);
			CreatePreOrder(&(*root)->leftchild);
			CreatePreOrder(&(*root)->rightchild);		
		}
 	}
/********************************层序遍历****************************************/
	void LevelOrderTraverse()
	{
		if (root==NULL)
			return;
		BinaryTreeNode<T>* p;
		queue<BinaryTreeNode<T>*> myQueue;
		myQueue.push(root);
		myQueue.push(NULL);
		while (!myQueue.empty())
		{
			p=myQueue.front();
			myQueue.pop();
			if (p==NULL)
			{
				if (myQueue.empty())
					break;
				cout<<endl;
				myQueue.push(NULL);
			}
			else
			{
				cout<<p->data;
				if (p->leftchild)
					myQueue.push(p->leftchild);
				if (p->rightchild)
				myQueue.push(p->rightchild);
			}		
		}	
	}
/********************************先序遍历递归****************************************/
	void PreOrderTraverse(BinaryTreeNode<T>* root)
	{
		if (root)
		{
			cout<<root->data;
			PreOrderTraverse(root->leftchild);
			PreOrderTraverse(root->rightchild);
		}
	}
/********************************中序遍历递归****************************************/
	void InOrderTraverse(BinaryTreeNode<T>* root)
	{
		if (root)
		{
			InOrderTraverse(root->leftchild);
			cout<<root->data;
			InOrderTraverse(root->rightchild);
		}
	}
	/********************************后序遍历递归****************************************/
	void PostOrderTraverse(BinaryTreeNode<T>* root)
	{
		if (root)
		{
			PostOrderTraverse(root->leftchild);
			PostOrderTraverse(root->rightchild);
			cout<<root->data;
		}
	}
/********************************先序遍历非递归****************************************/
	void PreOrderTraverse()
	{
		if (root)
		{
			BinaryTreeNode<T>* p;
			stack<BinaryTreeNode<T>*> myStack;
			myStack.push(root);
			while (!myStack.empty())
			{
				p=myStack.top();
				myStack.pop();
				cout<<p->data;
				if (p->rightchild)
					myStack.push(p->rightchild);
				if (p->leftchild)
					myStack.push(p->leftchild);
			}
		}
	}
/********************************中序遍历非递归****************************************/
	void InOrderTraverse()
	{
		if (root==NULL)
			return;
		BinaryTreeNode<T>* p;
		stack<BinaryTreeNode<T>*> myStack;
		myStack.push(root);
		while (!myStack.empty())
		{
			while (myStack.top())
				myStack.push(myStack.top()->leftchild);
			myStack.pop();
			if (!myStack.empty())
			{
				p=myStack.top();
				myStack.pop();
				cout<<p->data;
				myStack.push(p->rightchild);
			}
		}
	}
/********************************后序遍历非递归****************************************/	
	void PostOrderTraverse()
	{
		if (root==NULL)
			return;
		stack<BinaryTreeNode<T>*> myStack1;
		stack<BinaryTreeNode<T>*> myStack2;
		BinaryTreeNode<T> * p;
		myStack1.push(root);
		while (!myStack1.empty())
		{
			p=myStack1.top();
			myStack1.pop();
			myStack2.push(p);
			if (p->leftchild)
				myStack1.push(p->leftchild);
			if(p->rightchild)
				myStack1.push(p->rightchild);
		}
		while (!myStack2.empty())
		{
			p=myStack2.top();
			myStack2.pop();
			cout<<p->data;
		}
	}
/********************************树的深度****************************************/
	int depth(BinaryTreeNode<T>* root)
	{
		int dep;
		if (root==NULL)
			dep=0;
		else
		{
			dep=1+(depth(root->leftchild)>depth(root->rightchild)?depth(root->leftchild):depth(root->rightchild));
		}
		return dep;
	}
/********************************返回双亲节点****************************************/
	BinaryTreeNode<T>* Parent(BinaryTreeNode<T>*root,BinaryTreeNode<T>*node)
	{
		if (root==NULL || root==node)
			return NULL;
		if (root->leftchild==node||root->rightchild==node)
		{
			return root;
		}
		else if(Parent(root->leftchild,node))
		{
			return Parent(root->leftchild,node);
		}
		else
			return Parent(root->rightchild,node);
	}
/********************************返回双亲节点(重载)****************************************/
	BinaryTreeNode<T>* ParentPre(BinaryTreeNode<T>*root,BinaryTreeNode<T>*node)//通过遍历树来搜索
	{
		if (root==node||root==NULL)
			return NULL;
		if(root)
		{
			if (root->leftchild==node||root->rightchild==node)
				return root;
			if (ParentPre(root->leftchild,node))
				return	ParentPre(root->leftchild,node);
			else
				return ParentPre(root->rightchild,node);
		}
	}
/********************************返回兄弟节点****************************************/
	BinaryTreeNode<T>* SiblingNode(BinaryTreeNode<T>*node)
	{
		BinaryTreeNode<T>*p=Parent(root,node);
		if (p)
		{
			if (p->leftchild==node)
				return p->rightchild;
			else
				return p->leftchild;
		}
		return NULL;
		
	}
/********************************销毁树****************************************/
	void DestroyTree(BinaryTreeNode<T>*root)
	{
		if (root!=NULL)
		{
			DestroyTree(root->leftchild);
			DestroyTree(root->rightchild);
			delete root;
		}
		root=NULL;
	}
};

【数据结构】二叉树[通俗易懂]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115881.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • USB OTG简单介绍

    USB OTG简单介绍

    2021年12月4日
    40
  • python网络攻击监测_KRACK Detector 用于检测并防止您网络中KRACK攻击的Python脚本[通俗易懂]

    python网络攻击监测_KRACK Detector 用于检测并防止您网络中KRACK攻击的Python脚本[通俗易懂]KRACKDetectorKRACKDetectorisaPythonscripttodetectpossibleKRACKattacksagainstclientdevicesonyournetwork.ThescriptismeanttoberunontheAccessPointratherthantheclientdevice…

    2022年6月3日
    40
  • bool型函数「建议收藏」

    bool型函数「建议收藏」bool介绍C++中bool函数如果值非零就为True,为零就是False。比如写数据结构的时候,有时候需要判断一下链表是不是为空,这时候需要用到bool函数,再者,你看到bool就知道这个函数返回值只是用于判断真假。bool函数返回的只有true和false。而int会返回各种数字,但是你关心的不是数字的多少,而是这个数字为不为0.所以这种情况用bool会更加简洁,规范,你看到bo…

    2022年6月5日
    33
  • js 给元素添加自定义属性

    js 给元素添加自定义属性给元素添加自定义属性obj.setAttribute(‘attr_name’,’attr_value’);//例如obj.setAttribute(‘class’,’snow-container’)给元素添加class属性的三种方法document.getElementsByTagName(‘body’)[0].className=’snow-container’;//设置为新的…

    2022年6月22日
    136
  • selenium的PO模式

    selenium的PO模式PageObject模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class(封装在一个class类中),其中包含页面中需要测试的所有元素(按钮,输入框,标题等)的属性和操作,这样在Selenium测试页面中可以通过调用页面类来获取页面元素,这样巧妙的避免了当页面元素id或者位置变化时,需要改测试页面代码的情况。当页面元素id变化时,只需要更改测试页Class中页面的属…

    2022年5月20日
    70
  • Es 模糊查询 match,wildcard

    Es 模糊查询 match,wildcardEs 模糊查询的方式要求 Es 查询 查询工单信息 输入 测试 查出 form name 为字段中有查询出含有符合内容的数据 match 分词模糊查询 比如 Everythingwi Alliswell 会被分词一个一个单词 不是单个字母 from 0 size 20 query bool should term f

    2025年12月7日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号