Example #1
0
TreeNode<T> * LCA(const unique_ptr<TreeNode<T>> & root,
                  const unique_ptr<TreeNode<T>> & node1,
                  const unique_ptr<TreeNode<T>> & node2) { 

    if (root == nullptr) {
        return nullptr;
    }

    if (root.get() == left || root.get() == right) {
        return root;
    }

    TreeNode<T> * left = LCA(root->left, node1, node2),
                  right = LCA(root->right, node1, node2);

    if (left != nullptr && right != nullptr) {
        return root;
    }

    if (left == nullptr && right == nullptr) {
        return nullptr;
    }

    if (left == nullptr && right == node2.get()) {
        return right;
    }

    if (left == node1.get() && right == nullptr) {
        return left;
    }
}
Example #2
0
/**
 * We traverse from the bottom, and once we reach a node which matches one of the two nodes, we pass it up to its parent. 
 * The parent would then test its left and right subtree if each contain one of the two nodes. 
 *     - If yes, then the parent must be the LCA and we pass its parent up to the root. 
 *     - If not, we pass the lower node which contains either one of the two nodes (if the left or right subtree contains either p or q), 
 *               or NULL (if both the left and right subtree does not contain either p or q) up.
 */
TreeNode *LCA(TreeNode *root, int x, int y){
    if(!root) return nullptr;
    if(root->val == x || root->val == y) return root;

    TreeNode *left = LCA(root->left, x, y);
    TreeNode *right = LCA(root->right, x, y);

    if(left && right) return root;
    return left? left : right;
}
Example #3
0
TreeNode *LCA(TreeNode *root, TreeNode *n1, TreeNode *n2) {
	if (root == NULL) return NULL;

	if (root->val > n1->val && root->val > n2->val) 
		return LCA(root->left, n1, n2);
	
	if (root->val < n1->val && root->val < n2->val) 
		return LCA(root->right, n1, n2);

	return root;
}
Node* LCA(Node* root, Node* p, Node* q)
{
    if(!root || root == p || root == q))
        return root;
    Node* l = LCA(root->left, p, q);
    Node* r = LCA(root->right, p ,q);
    if(l && r)
        return root;
    else
        return l?l:r;
        
}
node *LCA(node *root, int n1, int n2){
	if(root == NULL)
		return NULL;
	if(root->data == n1 || root->data == n2){
		return root;
	}
	node *l = LCA(root->left, n1, n2);
	node *r = LCA(root->right, n1, n2);
	if(l && r){
		return root;
	}
	return ((l != NULL) ? l : r);
}
Example #6
0
int main() {
  Node * root = new Node( 1, new Node(2,
                                      new Node(4, NULL, NULL),
                                      new Node(5, NULL, NULL)),
                             new Node(3,
                                      new Node(6, NULL, NULL),
                                      new Node(7, NULL, NULL)));
  printf("%d\n", LCA(root, 4, 5)->val); // 2
  printf("%d\n", LCA(root, 4, 6)->val); // 1
  printf("%d\n", LCA(root, 3, 4)->val); // 1
  printf("%d\n", LCA(root, 2, 4)->val); // 2
  return 0;
}
Example #7
0
NV *LCA(NV *treep, NV *a, NV *b) {
	NV *p, *q;
	if (treep == NULL)
		return NULL;
	if (treep == a)
		return a;
	if (treep == b)
		return b;
	p = LCA(treep->left, a, b);
	q = LCA(treep->right, a, b);
	if (p && q)
		return treep;
	return q == NULL ? p : q;
}
void LCA(int p,int x)
{
    int tmp,root,y;
    flag[x]=++tot;
    tmp=a[x];
    while(tmp!=-1)
    {
         if(tree[tmp].y!=p)
         {
             LCA(x,tree[tmp].y);
             Union(x,tree[tmp].y);
         }
         tmp=tree[tmp].next;
    }
    tmp=b[x];
    while(tmp!=-1)
    {
         y=e[tmp].y;
         if(flag[y]>0&&flag[y]<flag[x])
         {
              root=find(y);
              d[x]++;
              d[y]++;
              d[root]-=2;
         }
         tmp=e[tmp].next;
    }
}
int main()
{
    int i,x,y;
    while(EOF!=scanf("%d %d",&n,&m))
    {
          tot=0;
          for(i=1;i<=n;i++)
              a[i]=b[i]=-1,c[i]=0,d[i]=0,flag[i]=0,father[i]=-1;
          for(i=1;i<n;i++)
          {
              scanf("%d %d",&x,&y);
              add(tree,a,x,y);
              add(tree,a,y,x); 
          }
          tot=0;
          for(i=1;i<=m;i++)
          {
              scanf("%d %d",&x,&y);
              add(e,b,x,y);
              add(e,b,y,x);
          }
          sum=0;
          LCA(-1,1);
          dfs(-1,1);
          printf("%d\n",sum);
    }
    return 0; 
}
Example #10
0
int main(void){
	int i;

	scanf("%d %d", &nodeNum, &roadNum);

	for (i = 0; i < roadNum; i++) {
		scanf("%d %d %d", &E[i].a, &E[i].b, &E[i].cost );

		E[i].a--; E[i].b--;
		
		if(!E[i].cost) {
			addEdge(E[i].a, E[i].b);
			addEdge(E[i].b, E[i].a);
		} else {
			totalCost += E[i].cost;
		}
	}

	dfs(0);

	LCA();

	qsort(E, roadNum, sizeof(edge), edgeCmp);

	dp();

	printf("%d\n", totalCost - DP[0][0]);
	return 0;
}
Example #11
0
void combiOutObjectObject(Model* mdl, OutObject* oo, Object*o) {
	OutAttribute* oa = (OutAttribute*)malloc(sizeof(OutAttribute));
	Attribute* att;
	int pt;

	// loop through all the attributes of the object
	for(int i = 0; i < vectSize(o->attributes); ++i) {
		att = &vectAt(o->attributes, i);
		oa = &vectAt(oo->attributes, i);

		// combine the original OutObject with the new Object
		switch(att->type) {
			case TYPE_INT:
				addToInterval(&oa->inter, att->value);
				break;
			case TYPE_ENUM:
				vectIndexOf(oa->oenu.oenu, att->value, pt);
				if (pt == -1) {
					vectPush(int, oa->oenu.oenu, att->value);
				}
				break;
			case TYPE_TREE:
				// looking for tree model (root) in the model (same rank), then LCA
				oa->tree = LCA(&vectAt(mdl->ma, i).mt.tree, oa->tree, att->value)->id;
				break;
		}		
	}
Example #12
0
void find(int s) {
	for (int i = 0; i < n; i++) {
		Next[i] = -1; f[i] = i;
		mark[i] = 0; visited[i] = -1;
	}
	head = tail = 0; Q[tail++] = s; mark[s] = 1;
	for (; head < tail && Link[s] == -1; )
	for (int i = 0, x = Q[head++]; i < (int) E[x].size(); i++)
	if (Link[x] != E[x][i] && getf(x) != getf(E[x][i]) && mark[E[x][i]] != 2) {
		int y = E[x][i];
		if (mark[y] == 1) {
			int p = LCA(x, y);
			if (getf(x) != p) Next[x] = y;
			if (getf(y) != p) Next[y] = x;
			go(x, p);
			go(y, p);
		}
		else if (Link[y] == -1) {
			Next[y] = x;
			for (int j = y; j != -1; ) {
				int k = Next[j];
				int tmp = Link[k];
				Link[j] = k;
				Link[k] = j;
				j = tmp;
			}
			break;
		}
		else {
			Next[y] = x;
			mark[Q[tail++] = Link[y]] = 1;
			mark[y] = 2;
		}
	}
}
Example #13
0
int main(int argc, char *argv[]) {
	freopen("in.txt", "r", stdin);
	int n, m, i;
	int u, v, val;
	char ch;
	while(scanf("%d%d", &n, &m) != EOF) {
		init();
		for (i = 0; i < m; ++i) {
			scanf("%d%d%d %c", &u, &v, &val, &ch);
			add(u, v, val);
			add(v, u, val);
		}
		scanf("%d", &m);
		for (i = 0; i < m; ++i) {
			scanf("%d%d", &u, &v);
			add2(u, v);  // 询问双向构造
			add2(v, u);
		}
		// 有向图通过入度找树根, 无向图LCA(1)
		LCA(1);
		for (i = 0; i < tot2; i += 2) {
			printf("%d\n", ans[i]);
		}
	}
	return 0;
}
Example #14
0
uint32_t max_hit_tid(map<uint32_t, uint32_t> &hit_times) {

	 set<uint32_t> max_taxa;
	 uint32_t max_taxon = 0, max_score = 0;
	 map<uint32_t, uint32_t>::iterator it = hit_times.begin();
	 
	 // Sum each taxon's LTR path
	 while (it != hit_times.end()) {
		 uint32_t taxon = it->first;
		 uint32_t score = hit_times[taxon];

		 if (score > max_score) {
			 max_taxa.clear();
			 max_score = score;
			 max_taxon = taxon;
		 }
		 else if (score == max_score) {
			 if (max_taxa.empty())
				 max_taxa.insert(max_taxon);
			 max_taxa.insert(taxon);
		 }

		 ++it;
	 }
	 // If two LTR paths are tied for max, return LCA of all
	 if (! max_taxa.empty()) {
		 set<uint32_t>::iterator sit = max_taxa.begin();
		 max_taxon = *sit;
		 for (sit++; sit != max_taxa.end(); sit++)
			 max_taxon = LCA(max_taxon, *sit);
	 }

	 return max_taxon;

}
Example #15
0
Node * LCA(Node* root, int lv, int rv) {
  // base condition
  if (root == NULL)
    return NULL;
  if (root->val == lv || root->val == rv)
    return root;

  // recursion
  Node* left_lca = LCA(root->left, lv, rv);
  Node* right_lca = LCA(root->right, lv, rv);

  if (left_lca && right_lca)
    return root;
  
  return (left_lca != NULL) ? left_lca : right_lca;
}
Example #16
0
 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
     if(root == nullptr) {
         return nullptr;
     }
     TreeNode* ans = nullptr;
     LCA(root, p, q, ans);
     return ans;
 }
node *findLCA(node *root, int n1, int n2){
	node *res = LCA(root, n1, n2);
	if(res->data != n1 && res->data != n2)
		return res;
	if(res->data == n1){
		return (find(res, n2) ? res : NULL);
	}
	return (find(res, n1) ? res : NULL);
}
Node findLCA(Node *root, Node *p, Node *q) {
	// Check if the root exists
	if (!root)
		return NULL;
	// If p or q is a direct child of the root then root is the LCA
	if (root == p || root == q)
		return root;
	// Get LCA of p and q in the left subtree
	Node *L = LCA(root->left, p, q);
	// Get LCA of p and q in the right subtree
	Node *R = LCA(root->right, p, q);
	// If p or q is in the left subtree and the other is in the right, then root is the LCA
	if (L && R)
		return root;
	// Otherwise, L is the LCA
	else if (L)
		return L;
	// Otherwise, R is the LCA
	else
		return R;
}
Example #19
0
 pair<bool, bool> LCA(TreeNode* root, TreeNode* p, TreeNode* q, TreeNode* &ans) {
     pair<bool, bool> tans = make_pair(false, false);
     if (root == p) {
         tans.first = true;
     }
     if (root == q) {
         tans.second = true;
     }
     if (root->left != nullptr) {
         pair<bool, bool> t = LCA(root->left, p, q, ans);
         tans.first |= t.first;
         tans.second |= t.second;
     }
     if (root->right != nullptr) {
         pair<bool, bool> t = LCA(root->right, p, q, ans);
         tans.first |= t.first;
         tans.second |= t.second;
     }
     if (tans.first && tans.second && ans == nullptr) {
         ans = root;
     }
     return tans;
 }
Example #20
0
void LCA(int u) {
	int i, v;
	fa[u] = u;   // 初始化祖先为自己本身
	vis[u] = 1;  // 标记已经访问
	for (i = head[u]; i != -1; i = edge[i].next) {
		v = edge[i].v;
		if(!vis[v]) {
			dis[v] = dis[u] + edge[i].val;  // 距离根节点距离
			LCA(v);
			fa[v] = u; // 将当前点设为该集合的祖先节点
		}
	}
	for (i = qhead[u]; i != -1; i = qu[i].next) {
		v = qu[i].v;
		if(vis[v]) {  // 因为树遍历的顺序不确定
			ans[i] = ans[i ^ 1] = dis[u] + dis[v] - 2 * dis[getfa(v)];
		}
	}
}
Example #21
0
uint32_t resolve_tree(map<uint32_t, uint32_t> &hit_times)
 {
	 //if (hit_times.size() == 1) return hit_times.begin()->first;
	 set<uint32_t> max_taxa;
	 uint32_t max_taxon = 0, max_score = 0;
	 map<uint32_t, uint32_t>::iterator it = hit_times.begin();
	 
	 // Sum each taxon's LTR path
	 while (it != hit_times.end()) {
		 uint32_t taxon = it->first;
		 uint32_t node = taxon;
		 uint32_t score = 0;
		 while (node > 0) {
			 //cout<<node<<endl;
			 score += hit_times[node];
			 node = taxonomyTree[node];
		 }

		 if (score > max_score) {
			 max_taxa.clear();
			 max_score = score;
			 max_taxon = taxon;
		 }
		 else if (score == max_score) {
			 if (max_taxa.empty())
				 max_taxa.insert(max_taxon);
			 max_taxa.insert(taxon);
		 }

		 ++it;
	 }

	 // If two LTR paths are tied for max, return LCA of all
	 if (! max_taxa.empty()) {
		 set<uint32_t>::iterator sit = max_taxa.begin();
		 max_taxon = *sit;
		 for (sit++; sit != max_taxa.end(); sit++)
			 max_taxon = LCA(max_taxon, *sit);
	 }

	 return max_taxon;
}
int main(void) 
{
    int m,c,u,v,d;
    int in[N];
    while(scanf("%d%d%d",&n,&m,&c)!=EOF)
    {
        memset(edge,0,sizeof(edge));
        memset(vis,0,sizeof(vis));
        memset(head,-1,sizeof(head));
        memset(in,0,sizeof(in));
        for(int i=0;i<=n;i++) set[i]=i;
        num=0;
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&d);
            addedge(u,v,d);
            addedge(v,u,d);
            in[v]++;
            merge(u,v);
        }
        for(int i=1;i<=n;i++) //Á¬½ÓÉ­ÁÖµÄËùÓÐÊ÷ 
            if(in[i]==0){
                addedge(0,i,0);
                addedge(i,0,0);
            }
        num=0;
        dis[0]=0;
        dfs(0,1);
        init();
        //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",root[i]); 
        //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",dep[i]);
        //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",dis[i]);
        //for(int i=1;i<=2*n-1;i++) printf(i==2*n-1?"%d\n":"%d ",rank[i]);
        while(c--){
            scanf("%d%d",&u,&v);
            //printf("*%d %d\n",find(u),find(v));
            if(find(u)!=find(v)) puts("Not connected");
            else printf("%d\n",dis[u]+dis[v]-2*dis[LCA(u,v)]);
        }
    }
    return 0;
}
Example #23
0
void LCA(int u,int father)  
{  
    for(int i=Adj[u];~i;i=edge[i].next)  
    {  
        int v=edge[i].to;  
        if(v==father) continue;  
        Distan[v]=Distan[u]+edge[i].distan;  
        LCA(v,u);  
        fa[v]=u;          
    }  
    vis[u]=true;  
    for(int i=Adj2[u];~i;i=edge2[i].next)  //每一询问加两条边
    {  
        int v=edge2[i].to;  
        int id=edge2[i].distan;  
        if(vis[v])    
        {  
            ans[id]=Distan[u]+Distan[v]-2*Distan[Find(v)];  
            //LCA即 Find(v)
        }  
    }  
}  
int Sum(Node* a,Node* b){
	Node* lca=LCA(a,b);
	return lineSum(a,lca)+lineSum(b,lca)-lca->a[lca->loc].Sum;
}
short Max(Node* a,Node* b){
	Node* lca=LCA(a,b);
	return max(lineMax(a,lca),lineMax(b,lca));
}
Example #26
0
int main()
{
	#if 1
	AddNode(&pRoot,4);
	AddNode(&pRoot,6);
	AddNode(&pRoot,2);
	AddNode(&pRoot,3);
	AddNode(&pRoot,5);
	AddNode(&pRoot,7);
	AddNode(&pRoot,1);
	
	//SubTree

	pRootSub = getbtNode(2);
	pRootSub->left = getbtNode(1);
	pRootSub->right = getbtNode(3);//true subtree case
	//pRootSub->right = getbtNode(4);//fasle subtree case
	#else//for Sum Tree 
	pRoot = getbtNode(30);
	pRoot->left = getbtNode(10);
	pRoot->right = getbtNode(5);
	pRoot->left->left = getbtNode(6);
	pRoot->left->right = getbtNode(4);
	pRoot->right->right = getbtNode(3);
	pRoot->right->left = getbtNode(2);
		
	#endif
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Inorder Traversal ::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Preorder Traversal ::"<<endl;
	
	Preorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Postorder Traversal ::"<<endl;
	Postorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Level Order Traversal ::"<<endl;
	LOrder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Size of the Tree ::"<<endl;
	cout<<GetTreeSize(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Height of the Tree ::"<<endl;
	cout<<GetTreeHeight(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	#if 0
	DeleteTree(&pRoot);
	
	AddNode(&pRoot,1);
	
	cout<<"Inorder Traversal after Deletion::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	#endif
		
	MirrorTree(pRoot);	
	cout<<"Inorder Traversal after MirrorTree operation::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	MirrorTree(pRoot);
	cout<<"Inorder Traversal after MirrorTree operation for second time::"<<endl;
	Inorder(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Max Element of the Tree ::"<<endl;
	cout<<MaxTreeElement(pRoot);
	cout<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	int path[7];
	cout<<"Root To Leaf Paths ::"<<endl;
	RootToLeafPath(pRoot,path,0);
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<"Number of Leaf Nodes::"<<NumberOfLeafNodes(pRoot)<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"Ancestors of 7 are ::"<<endl;
	GetAncestors(pRoot,7);
	cout<<endl;

	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<"CheckSumTree ::"<<IsSumTree(pRoot)<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<endl<<"Lowest Common Ancestor of 1 and 3 ::"<<LCA(pRoot,1,3)->data<<endl;
	cout<<endl<<"Lowest Common Ancestor of 1 and 7 ::"<<LCA(pRoot,1,7)->data<<endl;
	cout<<endl<<"Lowest Common Ancestor of 4 and 6 ::"<<LCA(pRoot,4,6)->data<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	cout<<endl<<"Tree has Path Sum 9 ::"<<hasPathSum(pRoot,9)<<endl;
	cout<<endl<<"Tree has Path Sum 100 ::"<<hasPathSum(pRoot,100)<<endl;
	cout<<endl<<"Tree has Path Sum 15 ::"<<hasPathSum(pRoot,15)<<endl;
	cout<<endl<<"##########################################################################################################"<<endl;
	
	cout<<endl<<"Subtree ::"<<IsSubTree(pRoot,pRootSub)<<endl;
	
	cout<<endl<<"##########################################################################################################"<<endl;
	return 0;
}
int main(int argc, char *argv[])
{
    std::cerr << "reading graph...";
    Eigen::SparseMatrix<double> g = readGraph(std::string(argv[1]));
    std::cerr << "done." << std::endl;

#if DEBUG
    std::cout << "Adjacency Matrix" << std::endl;
    std::cout << g << std::endl;
#endif

    std::vector<C_tuple*> C;
    std::cerr << "getting C vector...";
    getC(g, C);

    std::cerr << "sorting...";
    __gnu_parallel::sort(C.begin(), C.end(), C_tuple_compare);
    //std::sort(C.begin(), C.end(), C_tuple_compare);
    std::cerr << "done." << std::endl;

#if DEBUG
    for (uint64_t i = 0; i < C.size(); i++)
    {
        std::cout << C[i]->i << ", " << C[i]->j << ", " << C[i]->value<< std::endl;
    }
#endif

    std::cerr << "creating T...";
    node* root = createT(g, C);
    std::cerr << "done." << std::endl;

#if DEBUG
    //postorder(printNode, root);
    levelorder(printNode, root);

    node* left = root->leftChild;
    while (left->leftChild) left = left->leftChild;

    node* right = root->rightChild;
    while (right->rightChild) right = right->rightChild;

    node* lca = LCA(left, right);
    std::cout << "lca for " << left->vertex << ", " << right->vertex << ": " << lca->vertex << std::endl;

    left = right->parent->leftChild;
    lca = LCA(left, right);
    std::cout << "lca for " << left->vertex << ", " << right->vertex << ": " << lca->vertex << std::endl;
#endif

    std::cerr << "counting vertices and edges...";
    countVerticesAndEdges(g, root);
    std::cerr << "done." << std::endl;
    std::cerr << "computing density...";
    computeDensity(root);
    std::cerr << "done." << std::endl;

#if DEBUG
    std::cout << "\nPrinting after the countVerticesAndEdges\n" << std::endl;
    postorder(printNode, root);
#endif

    std::cerr << "extracting subgraphs...";
    extractSubgraphs(root, 0.75);
    std::cerr << "done." << std::endl;
}
Example #28
0
int main(int argc, char** argv) {
	
	BinaryTree tree;
	
	 if (argc != 3) {
                std::cout << "Invalid number of arguments.\nUsage: " << argv[0] << " <inputFile> <outFile>\n";
                return 1;
        }

	tree.readFile(argv[1]);
	tree.DFS();
	tree.gvout(argv[2]);
	
	cout<<"is 10 anc 23 "<<isAncestor(tree,10,23)<<endl;
	cout<<"is 10 anc 40 "<<isAncestor(tree,10,40)<<endl;
	cout<<"is 40 anc 10 "<<isAncestor(tree,40,10)<<endl;
	cout<<"is 2 anc 47 "<<isAncestor(tree,2,47)<<endl;
	cout<<"is 1 anc 160 "<<isAncestor(tree,1,160)<<endl;
	cout<<"is 160 anc 1 "<<isAncestor(tree,160,1)<<endl;
	cout<<"is 160 anc 95 "<<isAncestor(tree,160,95)<<endl;
	cout<<"is 5 anc 11 "<<isAncestor(tree,5,11)<<endl;
	cout<<"is 11 anc 5 "<<isAncestor(tree,11,5)<<endl;

	cout << "LCA of 1 and 160 is " << LCA(tree,1,160) << endl;
	cout << "LCA of 160 and 1 is " << LCA(tree,160,1) << endl;
	cout << "LCA of 160 and 95 is " << LCA(tree,160,95) << endl;
	cout << "LCA of 95 and 160 is " << LCA(tree,95,160) << endl;
	cout << "LCA of 2 and 2 is " << LCA(tree,2,2) << endl;
	cout << "LCA of 5 and 11 is " << LCA(tree,5,11) << endl;
	cout << "LCA of 11 and 5 is " << LCA(tree,11,5) << endl;
	cout << "LCA of 160 and 3 is " << LCA(tree,160,3) << endl;
	cout << "LCA of 10 and 11 is " << LCA(tree,10,11) << endl;

	cout << "Size of tree = " << subTreeSize(tree,1) << endl;
	cout << "Size of subtree at 160 = " << subTreeSize(tree,160) << endl;
	cout << "Size of subtree at 95 = " << subTreeSize(tree,95) << endl;
	cout << "Size of subtree at 5 = " << subTreeSize(tree,5) << endl;
	cout << "Size of subtree at 2 = " << subTreeSize(tree,2) << endl;

	cout << "Balance = " << isBalanced(tree, 1) << endl;
	cout << "Balance at 160 = " << isBalanced(tree, 160) << endl;
	cout << "Balance at 80 = " << isBalanced(tree, 80) << endl;
	cout << "Balance at 40 = " << isBalanced(tree, 40) << endl;
	cout << "Balance at 5 = " << isBalanced(tree, 5) << endl;
	cout << "Balance at 2 = " << isBalanced(tree, 2) << endl;
	cout << "Balance at 3 = " << isBalanced(tree, 3) << endl;

	/*
	cout << "LCA of 2 and 6 is " << LCA(tree,2,6) << endl;
	cout << "LCA of 2 and 4 is " << LCA(tree,2,4) << endl;
	cout << "LCA of 2 and 2 is " << LCA(tree,2,2) << endl;
	cout << "LCA of 7 and 4 is " << LCA(tree,7,4) << endl;
	
	cout << "Balance = " << isBalanced(tree, 1) << endl;
	cout << "Balance at 8= " << isBalanced(tree, 8) << endl;
	cout << "Balance at 4 = " << isBalanced(tree, 4) << endl;
	cout << "Balance at 3 = " << isBalanced(tree, 3) << endl;
	cout << "Balance at 2 = " << isBalanced(tree, 2) << endl;
	
	cout << "Size of tree = " << subTreeSize(tree,1) << endl;
	cout << "Size of subtree at 2 = " << subTreeSize(tree,2) << endl;
	cout << "Size of subtree at 3 = " << subTreeSize(tree,3) << endl;
	cout << "Size of subtree at 8 = " << subTreeSize(tree,8) << endl;
	*/
	cout <<"Diameter of tree =" << diameter(tree) <<endl;
	return 0;
}
Example #29
0
int query(int u, int v) {
    int lca = LCA(u, v);
    return max( query_up(u, lca), query_up(v, lca) );
}