Exemple #1
0
        public void TestCountNodes()
        {
            Node no1 = new Node(new WordToken("1"));
            Node no2 = new Node(new WordToken("2"));
            Node no3 = new Node(new WordToken("3"));
            Node no4 = new Node(new WordToken("4"));
            Node no5 = new Node(new WordToken("5"));
            Node no6 = new Node(new WordToken("6"));
            binaryTree.insertNode(no1, no2);
            binaryTree.insertNode(binaryTree.root, no3);
            binaryTree.insertNode(binaryTree.root, no4);
            binaryTree.insertNode(binaryTree.root, no5);
            binaryTree.insertNode(binaryTree.root, no6);

            List<Token> tokenList = new List<Token>();
            binaryTree.convertTreeToList(binaryTree.root, ref tokenList);
            Assert.AreEqual(tokenList.Count, binaryTree.count);
            Assert.AreNotEqual(tokenList.Count+1, binaryTree.count);

            binaryTree = null;
            Assert.IsNull(binaryTree);

            binaryTree = new BinaryTreeImp();
            Assert.AreEqual(0, binaryTree.count);
        }
Exemple #2
0
        public void displayTree(Node root)
        {
            if (root == null) return;

            displayTree(root.right);
            System.Console.Write(root.token + " ");
            displayTree(root.left);
        }
Exemple #3
0
 public Evaluater(Node root, InvertedIndex index)
 {
     queue = new Queue<Token>();
     this.stack = new Stack<IEnumerable<string>>();
     _index = index;
     evaluaterOrder(root);
     result = stack.Pop();
 }
Exemple #4
0
        private void evaluaterOrder(Node node)
        {
            if (node == null) return;

            evaluaterOrder(node.left);
            evaluaterOrder(node.right);
            //when both left and right branch has been visited for a node
            //queue.Enqueue(root.token);
            evaluateTwoNodes(node);
        }
Exemple #5
0
        public void convertTreeToList(Node root, ref List<Token> nodeList)
        {
            var temp = root;
            //List<Token> nodeList = new List<Token>();
            if (root == null) return;
            nodeList.Add(temp.token);

            convertTreeToList(temp.right, ref nodeList);
            //System.Console.Write(root.token + " ");

            convertTreeToList(temp.left, ref nodeList);
        }
Exemple #6
0
        private void _buildTreeWordsOnly()
        {
            BinaryTree.root = new Node(_tokens.getNextToken());
            branchesStack.Push(BinaryTree.root);

            while (!_tokens.isEmpty())
            {
                Token wordToken = _tokens.getNextToken();
                Node OrNode = new Node(new OrToken());
                newSimpleTree(OrNode, new Node(wordToken));
                newSimpleTree(branchesStack.Pop(), branchesStack.Pop());
            }
        }
Exemple #7
0
 private void evaluateTwoNodes(Node node)
 {
     if (node.token.GetType() == typeof(OrToken) || node.token.GetType() == typeof(AndToken) || node.token.GetType() == typeof(AndNotToken))
     {
         evaluate(node.token);
     }
     else if (node.token.GetType() == typeof(WordToken))
     {
         stack.Push(_index.getSetLinks((WordToken)node.token));
     }
     else
         throw new System.ArgumentException("Could not determine token type", "node.token");
 }
Exemple #8
0
        public void insertNode(Node root, Node newLeafNode)
        {
            Node temp;
            temp = root;
            if (_count == 0) this.root = temp;

            if (temp.left == null)
            {
                temp.left = newLeafNode;
                _count++;
            }
            else if (temp.right == null)
            {
                temp.right = newLeafNode;
                _count++;
            }
            else
                insertNode(temp.right, newLeafNode); //NOTE: should not happen as we build trees with two balances leafs only
        }
Exemple #9
0
        public void TestContainsNodes()
        {
            Node no1 = new Node(new WordToken("1"));
            Node no2 = new Node(new WordToken("2"));
            Node no3 = new Node(new WordToken("3"));
            Node no4 = new Node(new WordToken("4"));
            Node no5 = new Node(new WordToken("5"));
            Node no6 = new Node(new WordToken("6"));
            Node no7 = new Node(new WordToken("7"));
            binaryTree.insertNode(no1, no2);
            binaryTree.insertNode(binaryTree.root, no3);
            binaryTree.insertNode(binaryTree.root, no4);
            binaryTree.insertNode(binaryTree.root, no5);
            binaryTree.insertNode(binaryTree.root, no6);

            List<Token> tokenList = new List<Token>();
            binaryTree.convertTreeToList(binaryTree.root, ref tokenList);
            Assert.Contains(no1.token, tokenList);
            Assert.Contains(no2.token, tokenList);
            Assert.Contains(no3.token, tokenList);
            Assert.Contains(no4.token, tokenList);
            Assert.Contains(no5.token, tokenList);
            Assert.Contains(no6.token, tokenList);
        }
Exemple #10
0
 public BinaryTreeImp()
 {
     _root = null;
 }
Exemple #11
0
 public Node(Token token)
 {
     this.token = token;
     left = null;
     right = null;
 }
Exemple #12
0
 private void newSimpleTree(Node root, Node leaf)
 {
     BinaryTree = new BinaryTreeImp();
     BinaryTree.insertNode(root, leaf);
     branchesStack.Push(BinaryTree.root);
 }
Exemple #13
0
        private Node createTree()
        {
            Node root = new Node(new AndToken());

            return root;
        }
Exemple #14
0
        public void TestInsertNodeCount1()
        {
            Node b = new Node(new WordToken("2"));
            //binaryTree.root = b;

            Node a = new Node(new WordToken("1"));
            Node c = new Node(new WordToken("3"));
            Node e = new Node(new WordToken("9"));
            Node d = new Node(new WordToken("6"));

            binaryTree.insertNode(b, a);
            binaryTree.insertNode(b, c);
            binaryTree.insertNode(b, e);
            binaryTree.insertNode(b, d);

            Assert.AreEqual(5, binaryTree.count);
        }
Exemple #15
0
 public void TestAddNode()
 {
     Node and1 = new Node(new AndToken());
     Assert.IsInstanceOf(typeof(Node), and1);
     Assert.IsNotNull(and1);
 }
Exemple #16
0
        private int countNodes(Node root)
        {
            if (root == null) return _count;

            _count = countNodes(root.right);

            _count = countNodes(root.left);
            _count++;
            return _count;
        }
Exemple #17
0
        public void testRoot()
        {
            Node no1 = new Node(new WordToken("1"));
            Node no2 = new Node(new WordToken("2"));
            Node no3 = new Node(new WordToken("3"));
            Node no4 = new Node(new WordToken("4"));
            Node no5 = new Node(new WordToken("5"));
            Node no6 = new Node(new WordToken("6"));
            Node no7 = new Node(new WordToken("7"));
            binaryTree.insertNode(no3, no2);
            binaryTree.insertNode(binaryTree.root, no1);
            binaryTree.insertNode(binaryTree.root, no4);
            binaryTree.insertNode(binaryTree.root, no5);
            binaryTree.insertNode(binaryTree.root, no6);

            Assert.AreSame(no3, binaryTree.root);
            Assert.AreNotSame(no1, binaryTree.root);
        }