示例#1
0
int main(){
  struct Node root,a,b,c,d,e;
  init(&root,4,&a,&b);
  init(&a,0,NULL,&d);
  init(&b,22,&c,NULL);
  init(&c,18,NULL,&e);
  init(&d,2,NULL,NULL);
  init(&e,21,NULL,NULL);
  
  /*                  root:4
		     /      \
		    a:0      b:22
		   /  \     /   \
		  NL   d:2 c:18 NL
		      / \  /  \
		     NL NL NL  e:21
		              / \
			     NL NL
  */
  printf("Here is the ordered list\n");
  display(&root);
  printf("Here are those values in the subtree with root 22\n");
  display(&b);
  printf(" 5 is %s in the tree\n",inTree(5,&root)?"":"not");
  printf(" 22 is %s in the tree\n",inTree(22,&root)?"":"not");
  return 0;
}
示例#2
0
int inTree(int x, struct Node *root){
  if(root != NULL){
    if(x == root->x)
      return true;
    else if(x < root->x)
      return inTree(x, root->left);
    else 
      return inTree(x, root->right);
  }
  return false;
}
    void inTree(TreeNode* root, TreeNode* p, TreeNode* q,int dp,bool first){
        if(root == NULL){return;}
        if(root == p){pf = true;}
        if(root == q){qf = true;}
        if((pf||qf) && !(pf&&qf)){
            if(dp<maxdep||maxdep<0){LCA = root;maxdep = dp;}
        }
        if(first){
            inTree(root->left,p,q,dp+1,true);
            inTree(root,p,q,dp,false);
            inTree(root->right,p,q,dp+1,true);
        }
        

    }
 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
     pf = false;
     qf = false;
     LCA = root;
     maxdep = -1;
     inTree(root,p,q,0,true);
     return LCA;
     
 }
示例#5
0
int main(int argc, char *argv[]){
	BSTree tree = newBSTree(5);
	assert(inTree(tree,5));
	insert(tree, 3);
	assert(inTree(tree,3));
	insert(tree, 7);
	assert(inTree(tree,7));
	insert(tree, 2);
	assert(inTree(tree,2));
	insert(tree, 4);
	assert(inTree(tree,4));
	insert(tree, 6);
	assert(inTree(tree,6));
	insert(tree, 8);
	assert(inTree(tree,8));

	prefixPrint(tree);

	dropTree(tree);
	return EXIT_SUCCESS;
}