数据结构--二叉树(1)

二叉树

构建:二叉树的构建采用的是先序遍历,->先储存根节点然后左右节点,用递归的思想将所有数据放在树中。

代码实现:实现了4种访问方法,先序,中序,后序,和层序的访问方法都采用递归的方式。

#include<iostream>
#include<queue>
#include<stack>
using namespace std;

template<class T> 
struct rootnode
{
	T _value;
	rootnode<T> *_leftnode;
	rootnode<T> *_rightnode;
	rootnode<T>(T value)
		: _value(value),
		_leftnode(NULL),
		_rightnode(NULL)
	{}
};

template <class T>
class BinaryTree
{
public:
	BinaryTree<T>( T *str)
	{
		T *tmp = str;
		_root = _BinaryTree(tmp);
	}
	~BinaryTree()
	{
		_Clear(_root);
	}
	BinaryTree<T> (BinaryTree &t)
	{
		_root=_Copy(t._root);
	}
	BinaryTree<T>& operator=(BinaryTree t)
	{
		if (*this != t)
		{
			swap(_root, t._root);
		}
	}
	void Fastorder()
	{
		_Fastorder(_root);
	}
	void Inorder()
	{
		_Inorder(_root);
	}
	void Postorder()
	{
		_Postorder(_root);
	}
	void Levelorder()
	{
		queue<rootnode<T>*> q;
			if (_root == NULL)
			{
				return;
			}
			q.push(_root);
			while (!q.empty())
			{
				rootnode<T>* root = q.front();
				cout << root->_value;
				q.pop();
				if (root->_leftnode != NULL)
				{
					q.push(root->_leftnode);
				}
				if (root->_rightnode != NULL)
				{
					q.push(root->_rightnode);
				}
			}
	}
	int leafnum()
	{
		int num = 0;
		num=_Leafnum(_root,num);
		return num;
	}
	int Size()
	{
		int size = 0;
		_Size(_root,size);
		return size;
	}
	int Depth()
	{
		int Depth = _Depth(_root);
		return Depth;
	}
	void NRfastorder()
	{
		stack<rootnode<T>*> s;
		if (_root != NULL)
		{
			s.push(_root);
		}
		while (!s.empty())
		{
			rootnode<T>* front=s.top();
			cout<<front->_value;
			s.pop();
			if (front->_rightnode != NULL)
			{
				s.push(front->_rightnode);
			}
			if (front->_leftnode != NULL)
			{
				s.push(front->_leftnode);
			}
		}
	}
	void NRinorder()
	{
		stack<rootnode<T>*>s;
		rootnode<T>*cur = _root;
		rootnode<T>* top = NULL;
		while (cur||!s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_leftnode;
			}
			if (top != s.top()->_rightnode)
			{
				top = s.top();
				cout << top->_value;
				s.pop();
				cur = top->_rightnode;
			}
			else
			{
				top = s.top();
				cout << top->_value;
				s.pop();
			}

		}
	}

	void NRpostorder()
	{
		rootnode<T>*cur = _root;
		stack<rootnode<T>*> s;
		rootnode<T>*top = NULL;
		while (cur || !s.empty())
		{
			while (cur)
			{
				s.push(cur);
				cur = cur->_leftnode;
			}
			if (s.top()->_rightnode != NULL&&top != s.top()->_rightnode)
			{

				top = s.top();
				cur = top->_rightnode;

			}
			else
			{
				top = s.top();
				s.pop();
				cout << top->_value;
			}

		}
	}
protected:
	rootnode<T>* _BinaryTree(T *&str)
	{
		rootnode<T> *root = NULL;
		if (*str != ‘#‘&&*str != ‘\0‘)
		{
			root = new rootnode<T>(*str);
			str++;
			root->_leftnode = _BinaryTree(str);
			str++;
			root->_rightnode = _BinaryTree(str);
		}
		return root;
	}
	void _Fastorder(rootnode<T> *&root)
	{
		if (root == NULL)
		{

			return;
		}
		else
		{
			cout << root->_value;
			_Fastorder(root->_leftnode);
			_Fastorder(root->_rightnode);

		}
	}
	void _Inorder(rootnode<T> *root)
	{
		if (root == NULL)
		{
			return;
		}
		_Inorder(root->_leftnode);
		cout << root->_value;
		_Inorder(root->_rightnode);
	}

	void _Postorder(rootnode<T> *root)
	{
		if (root == NULL)
		{
			return;
		}
		_Postorder(root->_leftnode);
		_Postorder(root->_rightnode);
		cout << root->_value;
	}
	void _Clear(rootnode<T> *root)
	{
		if (root == NULL)
		{
			return;
		}
		rootnode<T> *tmp = root->_leftnode;
		rootnode<T> *tmp2 = root->_rightnode;
		delete root;
		_Clear(tmp);
		_Clear(tmp2);
	}
	rootnode<T>* _Copy(rootnode<T> *root)
	{
		rootnode<T> *newroot = NULL;
		if (root == NULL)
		{
			return newroot;
		}
		newroot = new rootnode<T>(root->_value);
		newroot->_leftnode = _Copy(root->_leftnode);
		newroot->_rightnode = _Copy(root->_rightnode);
		return newroot;
	}
	int _Size(rootnode<T> *root,int &size)
	{
		if (root == NULL)
		{
			return 0;
		}
		size++;
		_Size(root->_leftnode,size);
		_Size(root->_rightnode,size);
		return size;
	}
	int _Depth(rootnode<T> *root)
	{
		if (root==NULL)
		{
			return 0;
		}
		int hight = 1;
		int left = 0;
		int right = 0;
		left += _Depth(root->_leftnode) + hight;
		right += _Depth(root->_rightnode) + hight;
		if (left > right)
		{
			return left;
		}
		else
		{
			return right;
		}

	}
	int _Leafnum(rootnode<T>* root,int &num)
	{
		if (root == NULL)
		{
			return 0;
		}
		if (root->_leftnode == NULL&&root->_rightnode == NULL)
		{
			num++;
		}
		_Leafnum(root->_leftnode, num);
		_Leafnum(root->_rightnode, num);
		return num;
	}
private:
	rootnode<T> *_root;
};

void Test1()
{
	char *str = "123##45##6##78###";
	BinaryTree<char> b1(str);
	BinaryTree<char> b2(b1);
	BinaryTree<char> b3 = b2;
	b1.Fastorder();
	cout << endl;
	b1.Inorder();
	cout << endl;
	b1.Postorder();
	cout << endl;
	b2.Fastorder();
	cout << endl;
	b3.Fastorder();
	cout << endl;
	cout << b3.Size()<<endl;
	cout << b3.Depth() << endl;
	b3.Levelorder();
	cout << endl;
	cout << b3.leafnum()<<endl;
}
int main()
{    
    Test1();
}
时间: 2024-08-07 04:31:30

数据结构--二叉树(1)的相关文章

数据结构二叉树——建立二叉树、中序递归遍历、非递归遍历、层次遍历

数据结构二叉树-- 编写函数实现:建立二叉树.中序递归遍历.借助栈实现中序非递归遍历.借助队列实现层次遍历.求高度.结点数.叶子数及交换左右子树. ("."表示空子树) #include<stdio.h> #include<stdlib.h> //***********二叉树链表节点结构 typedef char DataType; typedef struct Node {  DataType data;  struct Node*LChild;  struc

POJ 3367 Expressions(数据结构-二叉树)

Expressions Description Arithmetic expressions are usually written with the operators in between the two operands (which is called infix notation). For example, (x+y)*(z-w) is an arithmetic expression in infix notation. However, it is easier to write

[数据结构] 二叉树的建立及其基本操作

如图: 代码: #include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; char ch; typedef struct BinNode { char data; struct BinNode *lchild,*rchild; }BinNode,*BinTree; //二叉树链式存储结构 void CreateBin

数据结构--二叉树(定义与存储结构)

什么是二叉树 是具有n个节点的有限集合,由一个根节点和两棵互不相交二叉树组成.如图 从名字简单理解,就是具有2个树叉的树形结构,当然这不是绝对的,正如上图所示,我也可以只有一个树叉. 二叉树具有五种基本形态: (1)空二叉树 (2)只有一个根结点的二叉树 (3)只有左子树 (4)只有右子树 (5)既有左子树又有右子树 完全二叉树 这种二叉树,是二叉树中常用的专业术语,不是说一个完整的二叉树就是完全二叉树,这种二叉树叫满二叉树,如图 简单理解就像图片中,完全二叉树中每个节点的编号,都能映射到满二叉

(编程训练)再回首,数据结构——二叉树的前序、中序、后序遍历(非递归)

最近在复习数据结构,顺便看看大一的时候写的代码,看完之后比当初有了更加深刻的体会. 希望这些能提供给初学者一些参考. 在VC++6.0下可运行,当初还写了不少注释. 可以和(编程训练)再回首,数据结构--二叉树的前序.中序.后序遍历(递归)对比着看 [问题描述] 根据顺序存储结构建立二叉树的二叉链表,并对二叉树进行先序.中序.后序遍历. [基本要求] ·功能:根据顺序存储结构建立二叉树的二叉链表,并进行先序.中序.后序遍历. ·输入:输入二叉树的顺序存储. ·输出:二叉树的先序.中序.后序遍历序

浅谈数据结构-二叉树

浅谈数据结构-二叉树 二叉树是树的特殊一种,具有如下特点:1.每个结点最多有两颗子树,结点的度最大为2.2.左子树和右子树是有顺序的,次序不能颠倒.3.即使某结点只有一个子树,也要区分左右子树. 一.特殊的二叉树及特点 1.斜树 所有的结点都只有左子树(左斜树),或者只有右子树(右斜树).这就是斜树,应用较少 2.满二叉树 所有的分支结点都存在左子树和右子树,并且所有的叶子结点都在同一层上,这样就是满二叉树.就是完美圆满的意思,关键在于树的平衡. 根据满二叉树的定义,得到其特点为: 叶子只能出现

数据结构——二叉树遍历之“递归与非递归遍历”

简述 二叉树的遍历分为先序遍历.中序遍历和后序遍历.如下图所示: 递归遍历 private void bianli1(List<Integer> list, TreeNode root) { // 先序遍历 if (root == null) { return; } list.add(root.val); bianli1(list, root.left); bianli1(list, root.right); } private void bianli2(List<Integer>

数据结构——二叉树概述及其数组(顺序存储)表达法

树与二叉树: 什么是树呢?就是一个节点上会有很多分叉的数据结构.一般的,对于一棵树,我们需要的结构体为一个数据块和几个指针块,这就相当于很多个链表交织在了一起,实际上,链表也可以算是一种特殊的树,而我要讲的,也是一种特殊的树--二叉树. 对于树的各个节点,都有两个属性,称为度(degree),他的意思就是这个节点所拥有的子节点的数量.还有一个属性,称为深度(depth),指节点到根的距离. 什么是二叉树呢?顾名思义,就是度为二的树,它长这样: 如图所示,在链表中我们需要头(head),而在树中我

Java数据结构-二叉树及其遍历

二叉树的定义:n(n>=0)个结点的有限集合,该集合或者为空集(称为空二叉树),或者由一个根结点和两棵互相不相交的.分别称为根结点的左子树和右子树的二叉树组成. 二叉树的特点: 0<=度<=2: 左右子树是有顺序的,不能颠倒: 不论有几棵子树,也要区分它是左子树还是右子树. 二叉树的五种基本形态: 空二叉树: 只有一个根结点: 根结点只有左子树: 根结点只有右子树: 根结点既有左子树又有右子树. 举例3个结点的二叉树的形态有: 下面说一些特殊的二叉树. 斜树:所有的结点都只有左子树的二叉

数据结构——二叉树

要研究二叉查找树(binary search tree)首先要熟悉二叉树(binary tree)的概念与性质,二叉查找树是在二叉树基础上衍生出的数据结构. 二叉树 二叉树是一棵树,其中每个节点都不能有多于两个(<=2)的儿子.二叉树的一个性质是平均二叉树的深度要比N小得多.分析表明,这个平均深度为O(√N),而对于特殊的二叉树,即二叉查找树,其深度的平均值是O(logN).但是二叉树的深度也是可以达到N-1的(所有的节点都在同一侧,最坏情况). 二叉树的实现 因为二叉树中的节点最多有两个儿子,