Esempio n. 1
0
        /// <summary>
        /// Declare a symbol having the specified structure.
        /// </summary>
        public unsafe SemanticID DeclareSymbol(Guid symbols, SemanticID st, string label, SemanticContexts sc = SemanticContexts.RECEPTOR_CONTEXT)
        {
            TreeNode * pnode  = (TreeNode *)nodes[symbols];
            SemanticID symbol = _d_declare_symbol(pnode, st, label, (UInt16)sc);

            return(symbol);
        }
Esempio n. 2
0
    /** dfs is a depth first search
     * Inputs : TreeNode *root -> pointer to a root maybe
     *        : vector<int> that keeps lonely child values
     * No output
     **/
    void dfs(TreeNode *root, vector <int>&res)
    {
        //first is the root emtpy?
        if (!root)
        {
            return;
        }

        //if the node is an innner node, it has both children : recurseive call both sides
        if ((root->left) && (root->right))
        {
            dfs(root->left, res);
            dfs(root->right, res);
            return;
        }

        //Must not have both children, but it has a left child : recursion on left
        if (root->left)
        {
            res.push_back(root->left->val);
            dfs(root->left, res);
        }

        //Must not have both or left, but has right child : recursion on right
        if (root->right)
        {
            res.push_back(root->right->val);
            dfs(root->right, res);
        }
    }
Esempio n. 3
0
        /// <summary>
        /// Create a root node.
        /// </summary>
        /// <param name="symbol">The node symbol.</param>
        /// <returns>A GUID associated with this node instance.</returns>
        public unsafe Guid CreateRootNode(SemanticID structures)
        {
            TreeNode *node = _t_new_root(structures);
            Guid      guid = RegisterNode(node);

            return(guid);
        }
Esempio n. 4
0
        public unsafe string Dump(Guid g_symbols, Guid g_structures, Guid nodeID)
        {
            Defs      defs = CreateDefs(g_symbols, g_structures);
            TreeNode *node = (TreeNode *)nodes[nodeID];

            return(Dump(defs, node));
        }
Esempio n. 5
0
    TreeNode *upsideDownBinaryTree(TreeNode *root) {
        //using a dummy node to help to store the new tree
        TreeNode dummy(0);
        TreeNode *head =  &dummy, *left=NULL, *right=NULL;

        while ( root!=NULL ) {
            //find the right & left
            left = root->right;
            right = root;

            //move root the next
            root = root->left;

            //replace the right with current root
            right->left = head->left;
            right->right = head->right;

            //move the dummy to the root
            dummy.right = right;
            dummy.left = left;

            //reset the head to the root
            head = &dummy;

        }

        return head->right;
    }
Esempio n. 6
0
        /// <summary>
        /// Returns a GUID for the associated TreeNode* of a string converted into a tree.
        /// </summary>
        public unsafe Guid GetTree(string str)
        {
            TreeNode *node   = makeASCIITree(str);
            Guid      nodeID = RegisterNode(node);

            return(nodeID);
        }
Esempio n. 7
0
        public unsafe bool MatchTest(Guid semtrexID, Guid matchAgainstID)
        {
            TreeNode *semtrex      = GetNode(semtrexID);
            TreeNode *matchAgainst = GetNode(matchAgainstID);
            int       ret          = _t_match(semtrex, matchAgainst);

            return(ret == 1);
        }
Esempio n. 8
0
        /// <summary>
        /// Return a Guid associated with the T* instance.
        /// </summary>
        protected unsafe Guid RegisterNode(TreeNode *node)
        {
            Guid guid = Guid.NewGuid();

            nodes[guid] = (IntPtr)node;

            return(guid);
        }
Esempio n. 9
0
        public unsafe Guid ParseSemtrex(Guid g_symbols, Guid g_structures, string expression)
        {
            Defs      defs   = CreateDefs(g_symbols, g_structures);
            TreeNode *node   = parseSemtrex(&defs, expression);
            Guid      nodeID = RegisterNode(node);

            return(nodeID);
        }
Esempio n. 10
0
        /* create a new tree based on the matched elements from a semtrex match
         *
         * @param[in] defs definitions of the semantic context
         * @param[in] match a match from a call to _t_matchr
         * @param[in] parent the parent tree to add the embodiment into
         *
         */
        public unsafe Guid Embody(Guid g_symbols, Guid g_structures, Guid matchID, Guid semtrexID)
        {
            Defs      defs       = CreateDefs(g_symbols, g_structures);
            TreeNode *match      = GetNode(matchID);
            TreeNode *semtrex    = GetNode(semtrexID);
            TreeNode *resultTree = _t_embody_from_match(&defs, match, semtrex);

            return(RegisterNode(resultTree));
        }
Esempio n. 11
0
    /**
     * Problem : getLonelyNodes : algortithm that searches a binary tree for
     * single children nodes.
     * Inputs: Binary Tree, Pointer to the root
     * Outputs : Vector <int> : contains all values of 'lonelyNodes' in any order
     * Constraints : num Nodes in tree is range{1,1000}
     *             : nod Values is range {1, 10^6}
     * Test Cases : root = {1,2,3,null,4} : returns 4
     **/
    vector <int> getLonelyNodes(TreeNode *root)
    {
        //first create a vector for the result
        vector <int> res;

        //then call depth first search
        dfs(root, res);
        return(res);
    }
Esempio n. 12
0
        public unsafe SemanticID DefineStructure(Guid structures, string name, SemanticID[] symbolArray, SemanticContexts sc = SemanticContexts.RECEPTOR_CONTEXT)
        {
            TreeNode *structs = (TreeNode *)nodes[structures];

            _dv_define_structure(structs, name, symbolArray.Length, __arglist(symbolArray));
            SemanticID st = new SemanticID()
            {
                context = (ushort)sc, flags = (ushort)SemanticTypes.SEM_TYPE_STRUCTURE, id = (uint)_t_children(structs)
            };

            return(st);
        }
Esempio n. 13
0
        public unsafe string CreateVisualTree(Guid g_symbols, Guid g_structures, Guid g_tree)
        {
            Defs      defs = CreateDefs(g_symbols, g_structures);
            TreeNode *tree = GetNode(g_tree);
            string    ret  = String.Empty;

            fixed(char *buf = new char[50000])
            {
                _t2json(&defs, tree, -1, buf);
                ret = Marshal.PtrToStringAnsi((IntPtr)buf);
            }

            return(ret);
        }
Esempio n. 14
0
 int maxDepth(TreeNode *root)
 {
     if (root == nullptr)
     {
         return(0);
     }
     else if (root->left == nullptr && root->right == nullptr)
     {
         return(1);
     }
     else
     {
         return(max(maxDepth(root->left), maxDepth(root->right)) + 1);
     }
 }
Esempio n. 15
0
        // Match a tree against a semtrex and get back match results
        //	* @param[in] semtrex the semtrex pattern tree
        //	* @param[in] t the tree to match against the pattern
        //	* @param[inout] rP a pointer to a T to be filled with a match results tree
        // * @returns 1 or 0 if matched or not
        public unsafe Tuple <bool, Guid> Match(Guid semtrexID, Guid treeToMatchID)
        {
            TreeNode *semtrex     = GetNode(semtrexID);
            TreeNode *treeToMatch = GetNode(treeToMatchID);
            TreeNode *resultTree;
            int       matchState = _t_matchr(semtrex, treeToMatch, &resultTree);
            Guid      guid       = Guid.Empty;

            if (matchState == 1)
            {
                guid = RegisterNode(resultTree);
            }

            return(new Tuple <bool, Guid>(matchState == 1, guid));
        }
Esempio n. 16
0
 /** lowestCommonAncestor
  * Description: Searches a tree, with two nodes, and determines whas is the
  * lowest common ancestor or parent to both p and q
  * Inputs : TreeNode : root -> root of the tree
  *        : TreeNode : p is one child in the tree
  *        : TreeNode : q is another child in the tree
  * Output : Assumed : TreeNode : lca -> lowest common ancestor of p and q
  * Constraints : nodes in the tree range [2,10^5]
  *             : -10^0 <= Node.val <= 10^9
  *             : All Node.val = unique
  *             : p != q
  *             : p and  q exist in the BST
  *
  * Data Structures / Algorithms : TreeNode (BST)
  * Time / Space complexity : inorder O(n) / O(1)
  * Test Case : easy : root = [6,2,8,0,null, null, null, null] given 2,8 : return 6
  *           : medium root =[6,2,8,0,4,7,9,null,null,3,5] given 2,4 : return 2
  **/
 TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q)
 {
     if (root == NULL || p == NULL || q == NULL)
     {
         return(NULL);
     }
     if (p->val < root->val && q->val < root->val)
     {
         return(lowestCommonAncestor(root->left, p, q));
     }
     if (p->val > root->val && q->val > root->val)
     {
         return(lowestCommonAncestor(root->right, p, q));
     }
     return(root);
 }
Esempio n. 17
0
    int maxPathSum(TreeNode *root)
    {
        int maxSum   = 0;
        int leftSum  = 0;
        int rightSum = 0;

        if (root->left == NULL && root->right == NULL)
        {
            return(root->val);
        }

        if (root->left != NULL)
        {
            leftSum  = maxPathSum(root-- > left);
            maxSum  += leftSum;
            leftSum += root->val;
        }

        if (root->right != NULL)
        {
            rightSum  = maxPathSum(root->right);
            maxSum   += rightSum;
            rightSum += root->val;
        }

        maxSum += root->val;

        if (maxSum > leftSum && maxSum > rightSum && maxSum > root->val)
        {
            return(maxSum);
        }

        if (root->val > leftSum && root->val > rightSum)
        {
            return(root->val);
        }

        if (leftSum > rightSum)
        {
            return(leftSum);
        }

        return(rightSum);
    }
Esempio n. 18
0
        protected unsafe string Dump(Defs defs, TreeNode *node)
        {
            string ret = String.Empty;

            try
            {
                fixed(char *buf = new char[10000])
                {
                    __t_dump(&defs, node, 0, buf);
                    ret = Marshal.PtrToStringAnsi((IntPtr)buf);
                }
            }
            catch (Exception ex)
            {
                // TODO: Log message
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debugger.Break();
            }

            return(ret);
        }
Esempio n. 19
0
    vector <int> preorderTraversal(TreeNode *root)
    {
        stack <TreeNode *> st;
        vector <int>       result;

        st.push(root);
        while (!st.empty())
        {
            TreeNode *node = st.top();                      // 中
            st.pop();
            if (node != NULL)
            {
                result.push_back(node->val);
            }
            else
            {
                continue;
            }
            st.push(node->right);                           // 右
            st.push(node->left);                            // 左
        }
        return(result);
    }
Esempio n. 20
0
 extern static unsafe SemanticID _d_declare_symbol(TreeNode *symbols, SemanticID sid, string label, UInt16 context);
Esempio n. 21
0
 extern static unsafe SemanticID _dv_define_structure(TreeNode *structures, [MarshalAs(UnmanagedType.LPStr)] string label, int num_params, __arglist);
Esempio n. 22
0
 extern static unsafe int _t_children(TreeNode *structures);
Esempio n. 23
0
 // [return: MarshalAs(UnmanagedType.LPStr)]
 extern static unsafe void __t_dump(Defs *defs, TreeNode *t, int level, char *buf);
Esempio n. 24
0
 extern static unsafe int _t_match(TreeNode *semtrex, TreeNode *matchAgainst);
Esempio n. 25
0
 extern static unsafe int _t_matchr(TreeNode *semtrex, TreeNode *matchAgainst, TreeNode **matchResult);
Esempio n. 26
0
 extern static unsafe TreeNode *_t_embody_from_match(Defs *d, TreeNode *matchResult, TreeNode *semtrex);
Esempio n. 27
0
 extern static unsafe void _t2json(Defs *d, TreeNode *tree, int indent, char *buf);