二叉树ADT,周游二叉树,递归非递归,求节点的父节点,兄弟节点

本文介绍了二叉树的基本抽象数据类型(ADT),包括如何周游二叉树(递归与非递归方法),以及如何在二叉树结构中查找指定节点的父节点和兄弟节点,深入理解二叉树的操作技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
#include "BinaryTreeNode.h"
#include<stack>
enum Tags{Left,Right};

template<class T>
class StackElem{
public:
	BinaryTreeNode<T>* pointer;
	Tags tag;
};

template <class T>
class BinaryTree
{
public:
	BinaryTree(){root=NULL;};
	~BinaryTree(){};
	BinaryTreeNode<T>* Root(){return root;};
	void createTree(const T& info,BinaryTree<T>& left,BinaryTree<T>& right);
	bool isEmpty()const;
	//先序遍历递归
	void preOrder(BinaryTreeNode<T>* current);
	//中序遍历递归
	void inOrder(BinaryTreeNode<T>* current);
	//后序遍历递归
	void postOrder(BinaryTreeNode<T>* current);
	void Visit(T value){cout<<value;};

	//先序遍历非递归
	void preOrderWithoutRec(BinaryTreeNode<T>* current);
	//中序遍历非递归
	void inOrderWithoutRec(BinaryTreeNode<T>* current);
	//后序遍历非递归
	void postOrderWithoutRec(BinaryTreeNode<T>* current);

	//寻找父节点
	BinaryTreeNode<T>* parent(BinaryTreeNode<T>* current);
	//寻找左兄弟
	BinaryTreeNode<T>* leftSibling(BinaryTreeNode<T>* current);
	//寻找右兄弟
	BinaryTreeNode<T>* rightSibling(BinaryTreeNode<T>* current);

private:
	BinaryTreeNode<T> *root;
};

template<class T>
void BinaryTree<T>::createTree(const T& info,BinaryTree<T>& left,BinaryTree<T>& right){
	root=new BinaryTreeNode<T>(info,left.root,right.root);
	left.root=NULL;
	right.root=NULL;
}

template<class T>
bool BinaryTree<T>::isEmpty()const{
	return root?false:true;
}

template<class T>
void BinaryTree<T>::preOrder(BinaryTreeNode<T>* current){
	if(current != NULL){
		Visit(current->val());
		preOrder(current->leftChild());
		preOrder(current->rightChild());
	}
}

template<class T>
void BinaryTree<T>::inOrder(BinaryTreeNode<T>* current){
	if(current!=NULL){
		inOrder(current->leftChild());
		Visit(current->val());
		inOrder(current->rightChild());
	}
}

template<class T>
void BinaryTree<T>::postOrder(BinaryTreeNode<T>* current){
	if(current!=NULL){
		postOrder(current->leftChild());
		postOrder(current->rightChild());
		Visit(current->val());
	}
}
template<class T>
void BinaryTree<T>::preOrderWithoutRec(BinaryTreeNode<T>* current){
	BinaryTreeNode<T>* pointer = current;
	stack<BinaryTreeNode<T>*> astack;
	while(pointer || !astack.empty()){
		while(pointer){
			astack.push(pointer);
			Visit(pointer->val());
			pointer = pointer->leftChild();
		}
		pointer = astack.top();
		astack.pop();
		pointer=pointer->rightChild();
	}
}

template<class T>
void BinaryTree<T>::inOrderWithoutRec(BinaryTreeNode<T>* current){
	BinaryTreeNode<T>* pointer = current;
	stack<BinaryTreeNode<T>*> astack;
	while(pointer || !astack.empty()){
		while(pointer){
			astack.push(pointer);
			pointer = pointer->leftChild();
		}
		pointer = astack.top();
		astack.pop();
		Visit(pointer->val());
		pointer=pointer->rightChild();
	}
}

template<class T>
void BinaryTree<T>::postOrderWithoutRec(BinaryTreeNode<T>* current){
	StackElem<T> element;
	using std::stack;
	stack<StackElem<T>> astack;
	BinaryTreeNode<T>* pointer;
	if(current == NULL)
		return;
	pointer = current;
	while(pointer || !astack.empty()){
		while(pointer){
			element.pointer=pointer;
			element.tag=Left;
			astack.push(element);
			pointer=pointer->leftChild();
		}
		element=astack.top();
		astack.pop();
		pointer=element.pointer;
		if(element.tag==Left){
			element.tag=Right;
			astack.push(element);
			pointer=pointer->rightChild();
		}else{
			Visit(pointer->val());
			pointer=NULL;
		}
	}
}

template<class T>
BinaryTreeNode<T>* BinaryTree<T>::parent(BinaryTreeNode<T>* current){
	if(root==NULL || current==NULL)
		return NULL;
	if(current == root)
		return NULL;
	using std::stack;
	stack<BinaryTreeNode<T>*> astack;
	BinaryTreeNode<T>* pointer = root;
	while(pointer || !astack.empty()){
		if(pointer){
			if(current==pointer->leftChild() || current==pointer->rightChild())
				return pointer;
			astack.push(pointer);
			pointer=pointer->leftChild();
		}else{
			pointer = astack.top();
			astack.pop();
			pointer=pointer->rightChild();
		}
	}
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::leftSibling(BinaryTreeNode<T>* current){
	if(current==NULL)
		return NULL;
	BinaryTreeNode<T>* par = this->parent();
	if(par==NULL || par->leftChild==current)
		return NULL;
	return par->leftChild();
}

template<class T>
BinaryTreeNode<T>* BinaryTree<T>::rightSibling(BinaryTreeNode<T>* current){
	if(current==NULL)
		return NULL;
	BinaryTreeNode<T>* par = this->parent();
	if(par==NULL || par->rightChild==current)
		return NULL;
	return par->rightChild();
}

Main

#include<iostream>
#include<string>
#include "BinaryTree.h"
using namespace std;
int *nextfun(string P){
	int m = P.length();
	int *ret = new int[m];
	ret[0] = 0;
	for(int i = 1 ; i < m ; i++){
		int k = ret[i-1];
		if(P[i] == P[k]){
			ret[i] = k + 1;
		}
		else{
			while(P[i]!=P[k] && k>0)
				k = ret[k-1];
			if(P[i] == P[k])
				ret[i] = k + 1;
			else
				ret[i] = 0;
		}
	}
	return ret;
}
int KMPStrMatch(string S,string P,int *N){
	int slen = S.length(),plen = P.length();
	if(slen < plen)
		return -1;
	int i,j=0;
	for(i = 0 ; i < slen ; i++){
		while(P[j] != S[i] && j > 0)
			j = N[j-1];
		if(P[j] == S[i])
			j++;
		cout<<"i="<<i<<";"<<"j="<<j<<endl;
		if(j == plen)
			return (i-j+1);
	}
	return -1;
}
int main(){
	/*string S="abaabababba";
	string P = "abaa";
	int* a = nextfun(P);
	for(int i = 0 ; i < 5 ; i++){
		cout<<a[i]<<" ";
	};
	cout<<endl;
	cout<<KMPStrMatch(S,P,a)<<endl;*/

	BinaryTree<char> a,b,c,d,e,f,g,h,i,nulltree;
	d.createTree('D', nulltree, nulltree);
	g.createTree('G', nulltree, nulltree);
	h.createTree('H', nulltree, nulltree);
	i.createTree('I', nulltree, nulltree);
	f.createTree('F', h, i);
	e.createTree('E', g, nulltree);
	b.createTree('B', d, e);
	c.createTree('C', nulltree, f);
	a.createTree('A', b, c);

	cout<<"pre order:"<<endl;
	a.preOrder(a.Root());
	cout<<endl<<"in order:"<<endl;
	a.inOrder(a.Root());
	cout<<endl<<"post order:"<<endl;
	a.postOrder(a.Root());
	cout<<endl;

	cout<<"preorder traverse without recusive:"<<endl;
	a.preOrderWithoutRec(a.Root());
	cout<<endl;

	cout<<"inorder traverse without recusive:"<<endl;
	a.inOrderWithoutRec(a.Root());
	cout<<endl;

	cout<<"postorder traverse without recusive:"<<endl;
	a.postOrderWithoutRec(a.Root());
	cout<<endl;


}



/* * 二叉树节点ADT接口 */ package dsa; public interface BinTreePosition extends Position { //判断是否有父亲(为使代码描述简洁) public boolean hasParent(); //返回当前节点父节点 public BinTreePosition getParent(); //设置当前节点父节点 public void setParent(BinTreePosition p); //判断是否为叶子 public boolean isLeaf(); //判断是否为左孩子(为使代码描述简洁) public boolean isLChild(); //判断是否有左孩子(为使代码描述简洁) public boolean hasLChild(); //返回当前节点的左孩子 public BinTreePosition getLChild(); //设置当前节点的左孩子(注意:this.lChild和c.parent都不一定为空) public void setLChild(BinTreePosition c); //判断是否为右孩子(为使代码描述简洁) public boolean isRChild(); //判断是否有右孩子(为使代码描述简洁) public boolean hasRChild(); //返回当前节点的右孩子 public BinTreePosition getRChild(); //设置当前节点的右孩子(注意:this.rChild和c.parent都不一定为空) public void setRChild(BinTreePosition c); //返回当前节点后代元素的数目 public int getSize(); //在孩子发生变化后,更新当前节点及其祖先的规模 public void updateSize(); //返回当前节点的高度 public int getHeight(); //在孩子发生变化后,更新当前节点及其祖先的高度 public void updateHeight(); //返回当前节点的深度 public int getDepth(); //在父亲发生变化后,更新当前节点及其后代的深度 public void updateDepth(); //按照中序遍历的次序,找到当前节点的直接前驱 public BinTreePosition getPrev(); //按照中序遍历的次序,找到当前节点的直接后继 public BinTreePosition getSucc(); //断绝当前节点与其父亲的父子关系 //返回当前节点 public BinTreePosition secede(); //将节点c作为当前节点的左孩子 public BinTreePosition attachL(BinTreePosition c); //将节点c作为当前节点的右孩子 public BinTreePosition attachR(BinTreePosition c); //前序遍历 public Iterator elementsPreorder(); //中序遍历 public Iterator elementsInorder(); //后序遍历 public Iterator elementsPostorder(); //层次遍历 public Iterator elementsLevelorder(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值