Microsoft Interview Hundred Questions 004--specify the BST path and

Source: Internet
Author: User
Microsoft Interview hundred questions 004 1. to the effect of the topic:Give a binary lookup tree BST define the path: from the root node to the leaf node termination is treated as a path definition path and: the sum of the weights of all nodes on the path and given a weight, request a path to meet the right value and all the paths
2. Ideas for solving problems:Because we're asking to solve all the situations, it's natural for us to think of depth-first traversal ( DFSWe start from the root node, each time down to all the root nodes, meet the condition print once (stored in memory of the remaining domination) here we can do the proper pruning to speed up.
1. The current path and already greater than the given weight, the next traversal point is greater than 0 (this path can be pruned) 2. The current path and is less than the given weight, the next traversal point is less than 0 (this path can be pruned)
The above is the main two pruning methods, because the situation of BST is too much, can be involved in a lot of pruning, here is no longer too much to repeat
3. Code implementation:
#include "iostream" #include "Cstdio" #include "CString" #include "cstdlib" #define N using namespace std;
	typedef struct Node {int key;
	struct node* left;
struct node* right;

}point; 

Class Errorsame {};
			Class BST {Public:bst () {root=null;
			num=key=savenum=0;
		memset (save,0,sizeof (save));
		} ~bst () {Clear (root);
		} void Add (int);
		void del (int);
		point* find (int);
		void Clear (point*);
		void preorder (point*);
		void Midorder (point*);
		void Aftorder (point*);
		void Rankorder ();
		point* Returnroot () {return root;
		} void Dfs (Point*,int); void Right () {cout<< ' please your key! '
			<<endl;
		cin>>key;
		} private:point* Root;
		int num;
		int key;
		int save[n];
int savenum;

};
	void BST::d FS (point* P,int now) {now+=p->key; if (p->left==null&&p->right==null) {if (Now==key) {for (int i=1;i<=savenum;i++) cout<<save[i
		    ]<< ';
	    	cout<<p->key<<endl; REturn;
	else return;
		else {save[++savenum]=p->key;
		DFS (P->left,now);
		savenum--;
		save[++savenum]=p->key;
		DFS (P->right,now);
	savenum--;
	} void Bst::clear (point* p) {if (p==null) return;
		else {clear (p->left);
		Clear (P->right);
	Free (p);
		} void Bst::add (int p) {if (num==0) {root=new point;
		root->right=root->left=null;
		root->key=p;
		num++;
	return;
			else {try {point *k=root;
		    point* W=root;
	     		while (W!=null) {if (w->key==p) throw errorsame ();
	     			else {if (w->key>p) w=w->left;
	     		else w=w->right;
		    } if (W!=null) k=w;
		    	} if (k->key>p) {point *a=new point ();
		    	a->left=a->right=null;a->key=p;
		    k->left=a;
		    	else {point *a=new point ();
		    	a->left=a->right=null;a->key=p;
		    k->right=a;
		} num++;
catch (Errorsame e)		{cout<< ' try to add ' same point in the tree! '
		<<endl;
	} void BST::d el (int p) {point* now=root;
	point* Father=null;
			while (now->key!=p) {if (now->key>p) {father=now;
		now=now->left;
			else {father=now;
		now=now->right; } if (now==null) {cout<< "Can not find" point! "
			<<endl;
		return;
		} if (father==null) {point* help=root;
		if (root->right==null) root=root->left;
				else {if (root->right->left==null) {root->right->left=root->left;
			root=root->right;
				else {point* z=null;
				point* k=root->right;
				point* W=root; 
					while (K->left!=null) {if (k->left->left==null) z=k;
				k=k->left;
				} z->left=k->right;
		    	k->left=root->left;
		    	k->right=root->right;
			Root=k;
	} free (Help);
		else if (now->right==null) {point* help=now; if (Father->left==now) Father->left=now->left;
		else father->right=now->left;
	Free (help);
			} else {if (now->right->left==null) {point* help=now;
			now->right->left=now->left;
			if (Father->left==now) father->left=now->right;
			else father->right=now->right;
		Free (help);
			else {point* Z;
			point* k=now->right;
				while (K->left!=null) {if (k->left->left==null) z=k;
			k=k->left;
				} if (Father->left==now) {z->left=k->right;
				father->left=k;
				k->left=now->left;
			k->right=now->right;
				else {z->left=k->right;
				father->right=k;
				k->left=now->left;
			k->right=now->right;
	point* bst::find (int p) {point* a=root)}}}} 
		while (A!=null) {if (a->key==p) return A;
		if (a->key>p) a=a->left;
	else a=a->right;
	} void BST::p reorder (point* p) {if (p==null) return;
	else {printf ("%d", p->key);	Preorder (P->left);
	Preorder (p->right);
	} void Bst::midorder (point* p) {if (p==null) return;
		else {midorder (p->left);
		printf ("%d", p->key);
	Midorder (P->right);
	} void Bst::aftorder (point* p) {if (p==null) return;
		else {aftorder (p->left);
		Aftorder (P->right);
	printf ("%d", p->key);
	} void Bst::rankorder () {point* queue[100];
	int head=1;
	int tail=2;
	Queue[1]=root;
		while (Head!=tail) {if (queue[head]->left!=null) queue[tail++]=queue[head]->left;
		if (queue[head]->right!=null) queue[tail++]=queue[head]->right;
	head++;

for (int i=1;i<=tail-1;i++) printf ("%d", Queue[i]->key);}
	int main () {BST my;
	My.add (10);
	My.add (5);
	My.add (4);
	My.add (7);
	My.add (12);
	My.right ();
	My.dfs (My.returnroot (), 0);
return 0;  }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.