From 63a9066c6cdd10751ba5e9142fc3341f50392688 Mon Sep 17 00:00:00 2001 From: kim Date: Thu, 16 Apr 2009 00:44:32 +0000 Subject: [PATCH] Added IsFirstChild and LastChild git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@311 3cdefd35-fc62-479d-8e8d-bae585ffb9ca --- XMLTree.cpp | 12 +++++++++++- XMLTree.h | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/XMLTree.cpp b/XMLTree.cpp index 99d0395..249143c 100644 --- a/XMLTree.cpp +++ b/XMLTree.cpp @@ -357,7 +357,9 @@ bool XMLTree::IsChild(treeNode x, treeNode y) if (!is_ancestor(Par, x, y)) return false; return depth(Par, x) == (depth(Par, y) + 1); } - +bool XMLTree::IsFirstChild(treeNode x){ + return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == NULLT)); +} // NumChildren(x): number of children of node x. Constant time with the data structure // of Sadakane. int XMLTree::NumChildren(treeNode x) @@ -499,6 +501,14 @@ treeNode XMLTree::FirstChild(treeNode x) return first_child(Par, x); } +treeNode XMLTree::LastChild(treeNode x) +{ + if (x == Root() || isleaf(Par,x) || x == NULLT) + return x; + else + return find_open(Par,find_close(Par,parent(Par,x))-1); +} + // NextSibling(x): returns the next sibling of node x, assuming it exists. treeNode XMLTree::NextSibling(treeNode x) { diff --git a/XMLTree.h b/XMLTree.h index 9b79ae3..1268cb6 100644 --- a/XMLTree.h +++ b/XMLTree.h @@ -153,6 +153,9 @@ public: /** IsChild(x,y): returns whether node x is parent of node y. */ bool IsChild(treeNode x, treeNode y); + + /** IsChild(x,y): returns whether node x is the first child of its parent */ + bool IsFirstChild(treeNode x); /** NumChildren(x): number of children of node x. Constant time with the * data structure of Sadakane. */ @@ -191,8 +194,13 @@ public: * Very fast in BP. */ treeNode FirstChild(treeNode x); + /** LastChild(x): returns the last child of node x. + * Implemented by Kim naively. */ + treeNode LastChild(treeNode x); + /** NextSibling(x): returns the next sibling of node x, assuming it * exists. */ + treeNode NextSibling(treeNode x); /** PrevSibling(x): returns the previous sibling of node x, assuming it -- 2.17.1