基于栈的二叉树的遍历

基于栈的深度优先搜索

基于手写的栈和STL栈的深度优先

//基于系统栈的二叉树的中序遍历
#include <iostream>
#include <stack>
using namespace std;
struct Node
{
	int data;
	Node* left;
	Node* right;
	Node(int val):data(val),left(NULL),right(NULL){};
};

struct Tree
{
	Node* tnode;
	Tree(Node* t):tnode(t){};
	void Add(int val,Node* t){
		if(t == NULL){
			t = new Node(val);
		}
		else{
			if(t->data > val){
				if(t->left == NULL){
					Node* tmp = new Node(val);
					t->left = tmp;
				}
				else{
					Add(val,t->left);
				}
			}
			else{
				if(t->right == NULL){
					Node* tmp = new Node(val);
					t->right = tmp;				//千万注意别写错了
				}
				else{
					Add(val,t->right);
				}
			}
		}
	};

};
void StackTravel(Node* t){
	stack<Node*>  s;
	Node* cur = t;
	while(cur){
		cout<<cur->data<<endl;
		if(cur->right){
			s.push(cur->right);
		}
		if(cur->left){
			cur = cur->left;
		}
		else{
			if(s.empty()){
				cur = NULL;
			}
			else{
				cur = s.top();
				s.pop();
			}
		}
	}
}
void Print(Node* t){
	if(t){
		cout<<t->data<<endl;
		Print(t->left);
		Print(t->right);
	}
}
int main(int argc, char const *argv[])
{
	int arr[10] = {5,6,0,9,3,8,1,2,7,4};
	Tree* tree = NULL;
	Node* root = NULL;
	for(int i = 0;i < 10;i++){
		if(i == 0){
			root = new Node(arr[i]);
			tree = new Tree(root);				//前面是申明,这里用之前必须开辟空间了
			tree->tnode = root;
		}
		else{						//两条路,必须加else
		tree->Add(arr[i],root);
		}
	}
	Print(root);
	StackTravel(root);
	return 0;
}

//手写的栈的二叉树的遍历
#include <iostream>
using namespace std;
struct TreeNode
{
	int data;
	int position;
	TreeNode* left;
	TreeNode* right;
	TreeNode(){};
	TreeNode(int value,int pos):data(value),position(pos), left(NULL),right(NULL){};
};
class BinarySearchTree
{
private:
	TreeNode* tnode ;
public:
	BinarySearchTree(TreeNode* t):tnode(t){};
	~BinarySearchTree(){delete tnode;tnode = NULL;};
	void Add(int value,int position,TreeNode* t);
	void Print(TreeNode* tnode); 	//中序

	
};
void BinarySearchTree::Add(int value,int position,TreeNode* t){
	if(t== NULL) {
		TreeNode* root = new TreeNode(value,position);
		t = root;
	}
	else{
		
		if(value < t->data){
			if(t->left == NULL){
				TreeNode* tmp = new TreeNode(value,position);
				t->left = tmp;
			}
			else{
				Add(value,position,t->left);
			}
			
		}
		else{
			if(t->right == NULL){
				TreeNode* tmp = new TreeNode(value,position);
				t->right = tmp;
			}
			else{
				Add(value,position,t->right);
			}
			
		}
	}
}
void BinarySearchTree::Print(TreeNode* tnode){
	if(tnode){	//if不是while
		Print(tnode->left);
		cout<<tnode->data<<endl;
		Print(tnode->right);
		
	}
}

int Search(TreeNode* t,int target){
	if( t == NULL){
		return -1;
	} 
	else if(t->data == target){
		return t->position;
	}
	else if( t->data > target){
		return Search(t->left,target);
	}
	else{
		return Search(t->right,target);	
	}
}

//Stack定义
template <class T>
class Node		//class默认是private,struct默认是public
{
public:
	T data;
	Node<T>* next;
	Node(T val):data(val),next(NULL){};
	~Node(){delete next;next=NULL;};
	
};
template<class T>
class Stack
{
	Node<T>* top;
public:
	Stack():top(NULL){};
	void Pop();
	void Push(T val);
	bool IsEmpty();
	T TopVal();
	~Stack(){delete top;top = NULL;};
	
};
template<class T>
void Stack<T>::Push(T val){
	Node<T>* tmp = new Node<T>(val);
	tmp->next = top;
	top = tmp;
}
template <class T>
void Stack<T>::Pop(){
	if(top == NULL){
		cout<<"Stack is empty!"<<endl;
		return ; 
	}
	else{
		Node<T>* tmp = top;
		top = top->next;
		delete tmp;
		tmp = NULL;
	}
}
template <class T>
bool Stack<T>::IsEmpty(){
	return top == NULL;
}
template <class T>
T Stack<T>::TopVal(){
	return top->data;
}

void PreOrderStack(TreeNode* t){
	Stack<TreeNode*>* s =new Stack<TreeNode*>() ;
	TreeNode* cur = t;
	while(cur){
		cout<<cur->data<<endl;
		if(cur->right){
			s->Push(cur->right);
		}
		if(cur->left){
			cur = cur->left;
		}
		else{
			if(s->IsEmpty()){
				cur = NULL;
			}
			else{
				cur = s->TopVal();
				s->Pop();
			}
		}
	}
}
int main(int argc, char const *argv[])
{
	int arr[5] = {3,6,4,2,0};
	TreeNode* root = NULL;
	BinarySearchTree* bst  = NULL;
	for(int i = 0;i < 5;i++){
		if(i == 0){
			root = new TreeNode(arr[0],i);
		}
		else{
			bst->Add(arr[i],i,root);
		}
	}
	//bst->Print(root);
	//cout<<Search(root,0)<<endl;
	PreOrderStack(root);
	return 0;
}
 

posted @ 2020-03-05 23:59  MenciusYue  阅读(321)  评论(0)    收藏  举报