Fortune left-law basis class4- title 7-1 to determine whether a tree is a binary tree search

1. Title: judging whether a tree is a binary tree search

2. Analysis

(1) binary tree search

Refers to any node binary tree search subtree head, the head node is less than the left subtree, right subtree is greater than the head node.
Here Insert Picture Description
For node 3, the left subtree is smaller than 3, right subtree 4 is greater than 3;
for node 5, left subtree 3 is less than 5, right subtree 8 is greater than 5;
For node 6, the right subtree 7 is greater than 6;
for node 10 , less than 10 9 left subtree;
for node 8, less than 6 8 left subtree, right subtree 10 is greater than 8;

(2) problem-solving ideas

Use inorder traversal , in order traversal output order is just left, right, and left less than just a binary tree search in less than a right, then the whole preorder output are arranged in ascending binary search tree.

3. core code

Establish (1) of the tree

Storage list using a binary tree, a design data field, two pointer field (points about the child).

class Tree
{
public:
	int val;
	Tree *left;
	Tree *right;
	//构造函数
	Tree(int x){
		this->val = x;
		this->left = NULL;
		this->right = NULL;
	}
};

Main function:

	Tree *head = new Tree(5);
	head->left = new Tree(3);
	head->left->left = new Tree(2);
	head->left->right = new Tree(4);
	head->right = new Tree(8);
	head->right->left = new Tree(6);
	head->right->right = new Tree(10);
	head->right->left->right= new Tree(7);

(2) non-recursive traversal sequence

When the stack is not empty, the loop or the current node is not empty, the current node is not empty, the stack and the current node pointing to its left subtree; current node is empty, the stack, the stack number of the current node pointing out right subtree.

tips: Why use the stack to store? See the first order of the non-recursive part

void inOrderUnRecur(Tree* head)
{
	stack<Tree*> t;
	while(!t.empty() || head != NULL)
	{
		//当前节点不为空,入栈并把当前节点指向其左子树
		if(head != NULL)
		{
			t.push(head);
			head = head->left;
		}
		//当前节点为空时,出栈,当前节点指向出栈数的右子树
		else
		{
			head = t.top();
			cout<<t.top()->data<<" ";
			t.pop();
			head = head->right;
		}
	}
}

(3) changes preorder

The timing in the print output in preorder as compared with replacement of the ascending node, preorder stack used to store the output order, whether the current need only compare the number of pop-up stack needs larger than the number of pop-up (ascending) can.

bool isBST(Tree *head)
{
	stack<Tree *> t;
	int pre = INT_MIN;//使用pre存储前一个弹出的数,INT_MIN是系统int型最小的数,因为第一次比较必须小于当前节点
	while(!t.empty() || head != NULL)
	{
		if(head != NULL)
		{
			t.push(head);
			head = head->left;
		}
		else
		{
			head = t.top();
			//比较:如果上个弹出的数大,不是搜索二叉树
			if(t.top()->val < pre)
				return false;
			pre = t.top()->val;//否则更新pre为现在弹出的数
		
			t.pop();
			head = head->right;
		}
	}
	return true;//所有比较完成,前面并没有返回,运行到这里时返回true是搜索树
}

4. The complete code

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


class Tree
{
public:
	int val;
	Tree *left;
	Tree *right;
	Tree(int x){
		this->val = x;
		this->left = NULL;
		this->right = NULL;
	}
};

bool isBST(Tree *head)
{
	stack<Tree *> t;
	int pre = INT_MIN;//使用pre存储前一个弹出的数,INT_MIN是系统int型最小的数
	while(!t.empty() || head != NULL)
	{
		if(head != NULL)
		{
			t.push(head);
			head = head->left;
		}
		else
		{
			head = t.top();
			//比较:如果上个弹出的数大,不是搜索二叉树
			if(t.top()->val < pre)
				return false;
			pre = t.top()->val;//否则更新pre为现在弹出的数
		
			t.pop();
			head = head->right;
		}
	}
	return true;
}

int main()
{
	Tree *head = new Tree(5);
	head->left = new Tree(3);
	head->left->left = new Tree(2);
	head->left->right = new Tree(4);
	head->right = new Tree(8);
	head->right->left = new Tree(6);
	head->right->right = new Tree(10);
	head->right->left->right= new Tree(7);

	cout<<isBST(head);


	system("pause");
	return 0; 
}
Published 51 original articles · won praise 1 · views 1372

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/104038033