Example #1
0
        //public void Remove(Node root, )
        public void Insert(Node root, Node newNode)
        {
            while (root != null)
            {
                if (newNode.data > root.data)
                {
                    if (root.rightNode == null)
                    {
                        root.rightNode = newNode;
                        break;
                    }

                    root = root.rightNode;
                }
                else
                {
                    if (root.leftNode == null)
                    {
                        root.leftNode = newNode;
                        break;
                    }
                    root = root.leftNode;
                }
            }
        }
Example #2
0
        public bool Search(int searchValue)
        {
            bool searchflag = false;
            current_Node = root;

            while (true) {
                if (current_Node.NodeValue == searchValue) {
                    searchflag = true;
                    break;
                } else if (current_Node.NodeValue > searchValue) {
                    if (current_Node.lesserSubNode == null) {
                        searchflag = false;
                        break;
                    } else {
                        current_Node = current_Node.lesserSubNode;
                    }
                } else if (current_Node.NodeValue < searchValue) {
                    if (current_Node.greaterSubNode == null) {
                        searchflag = false;
                        break;
                    } else {
                        current_Node = current_Node.greaterSubNode;
                    }
                }
            }
            return searchflag;
        }
Example #3
0
        public Node AddNode(int data)
        {
            Node temp = new Node(data);
            if (root == null)
            {
                root = temp;
            }
            count++;

            return temp;
        }
        public bool Add(int data)
        {
            Node toAdd = new Node ();
            toAdd.NodeValue = data;
            toAdd.lesserSubNode = null;
            toAdd.greaterSubNode = null;
            toAdd.levelindex = 0;

            levelCalculator = 0;

            if (root == null) {
                root = toAdd;
                current_Node = toAdd;
                return true;
            }

            current_Node = root;
            while (true) {
                if (current_Node.NodeValue == data) {
                    return false;

                } else if (current_Node.NodeValue > data) {
                    levelCalculator++;
                    if (current_Node.lesserSubNode == null) {
                        toAdd.levelindex = levelCalculator;
                        current_Node.lesserSubNode = toAdd;
                        current_Node = toAdd;
                        count++;
                        if (level < levelCalculator)
                            level = levelCalculator;
                        return true;

                    } else {
                        current_Node = current_Node.lesserSubNode;
                    }
                } else {
                    levelCalculator++;
                    if (current_Node.greaterSubNode == null) {
                        toAdd.levelindex = levelCalculator;
                        current_Node.greaterSubNode = toAdd;
                        current_Node = toAdd;
                        count++;
                        if (level < levelCalculator)
                            level = levelCalculator;
                        return true;

                    } else {
                        current_Node = current_Node.greaterSubNode;
                    }
                }
            }
        }
Example #5
0
        public void InsertintoBST(int data)
        {
            Node Newnode = new Node();
            Newnode.data = data;

            if(root == null)
            {
                Newnode.left = null;
                Newnode.right = null;
                root = new Node();

            }
            else
            {

            }
        }
Example #6
0
File: BST.cs Project: hlimbo/BST
        public void add(Person p)
        {
            //trivial case
            if (root == null)
            {
                root = new Node(p);
                return;
            }

            Node current = root;
            while (current != null)
            {
                if (String.Compare(p.Name,current.person.Name) <= 0)
                {
                    //if node to the right is empty, the current's left node is instantiated with the new data.
                    //otherwise, current traverses left of the tree.
                    if (current.left == null)
                    {
                        current.left = new Node(p);
                        break;
                    }
                    else
                        current = current.left;
                }
                else
                {
                    if (current.right == null)
                    {
                        current.right = new Node(p);
                        break;
                    }
                    else
                        current = current.right;
                }
            }
        }
Example #7
0
File: BST.cs Project: hlimbo/BST
        public void postOrder(Node root)
        {
            //lrv pattern
            if (root == null)
                return;

            Node left = root.left;
            Node right = root.right;
            postOrder(left);
            postOrder(right);
            root.print();
        }
Example #8
0
File: BST.cs Project: hlimbo/BST
        public void preOrder(Node root)
        {
            //vlr pattern

             if (root == null)
                return;

            Node left = root.left;
            Node right = root.right;
            root.print();
            preOrder(left);
            preOrder(right);
        }
Example #9
0
File: BST.cs Project: hlimbo/BST
        public void inOrder(Node root)
        {
            //lvr pattern
            if (root == null)
                return;

            Node left = root.left;
            Node right = root.right;

            inOrder(left);
            records.Add(root);
            root.print();
            inOrder(right);
        }
Example #10
0
File: BST.cs Project: hlimbo/BST
        public int Leaves(Node root)
        {
            //trivial case
            if (root == null)
                return 0;

            if (root.left == null && root.right == null)
                return 1;

            int leftleaves = Leaves(root.left);
            int rightleaves = Leaves(root.right);

            return leftleaves + rightleaves;
        }
Example #11
0
 public void Delete(int deleteValue)
 {
     root = DeleteKey (root, deleteValue);
 }
Example #12
0
 public Node(int data)
 {
     this.Value = data;
     left = null;
     right = null;
 }
Example #13
0
 public Node(int data)
 {
     NodeValue = data;
     greaterSubNode = null;
     lesserSubNode = null;
 }
Example #14
0
 public void Print(Node root)
 {
     if(root != null)
     {
         Print(root.leftNode);
         Console.WriteLine(root.data + " ");
         Print(root.rightNode);
     }
 }
Example #15
0
File: BST.cs Project: hlimbo/BST
        //recursively counts the left height and right height of the tree.
        //returns the max height of tree.
        public int Height(Node root)
        {
            if (root == null)
                return -1;

            int leftht = Height(root.left);
            int rightht = Height(root.right);

            if (leftht > rightht)
                return leftht + 1;

            return rightht + 1;
        }
Example #16
0
 public Node(int d)
 {
     this.data = d;
     leftNode = null;
     rightNode = null;
 }
Example #17
0
        private void BuildBST(int data)
        {
            var node = new Node() { Data = data, LChild = null, RChild = null };
            if (null == Root)
            {
                Root = node;
                return;
            }

            //指向当前节点。从根节点开始查找可以插入的位置。
            //如果需要创建的值小于该节点,则往当前节点的左子树查找。否则往右子树查找。
            //如果在左子树查找,且左子树的LChlid为空,说明这就是需要插入的位置
            //右子树也是同样的道理
            var current = Root;
            while (current != null)
            {
                if (data < current.Data)
                {
                    if (null == current.LChild)
                    {
                        current.LChild = node;
                        break;
                    }
                    else
                    {
                        current = current.LChild;
                    }
                }
                else
                {
                    if (null == current.RChild)
                    {
                        current.RChild = node;
                        break;
                    }
                    else
                    {
                        current = current.RChild;
                    }

                }

            }
        }
Example #18
0
        /// <summary>
        /// 先序遍历
        /// </summary>
        /// <param name="node"></param>
        public void PreOrder(Node node)
        {
            if (null != node)
            {
                Console.Write(node.Data+",");

                PreOrder(node.LChild);

                PreOrder(node.RChild);
            }
        }
Example #19
0
        /// <summary>
        /// 中序遍历(先输出根节点,再输出左子树,最后输出右子树)
        /// 中序遍历输出的顺序总是有序的。
        /// </summary>
        public void InOrder(Node node)
        {
            if (null != node)
            {

                InOrder(node.LChild);

                Console.Write(node.Data+",");

                InOrder(node.RChild);

            }
        }
Example #20
0
File: BST.cs Project: hlimbo/BST
        public string search(Node root,string name)
        {
            //trivial case - if tree's root is empty OR if subroot is empty, return an empty string.
             if (root == null)
                return "";

             string p = root.person.Name;

             if (p.Equals(name))
                 return p + "'s weight: " + root.person.Weight;

            string left = search(root.left, name);
            string right = search(root.right, name);

            return left + right;
        }
Example #21
0
 public Node(Node theinputnode)
 {
     greaterSubNode = theinputnode.greaterSubNode;
     lesserSubNode = theinputnode.lesserSubNode;
     NodeValue = theinputnode.NodeValue;
 }
        void print(Node root, double x1, double y1)
        {
            if (root != null) {

                UILabel myLabels = new UILabel (new CGRect (x1,y1,25,25));
                myLabels.Text = root.NodeValue.ToString ();
                myScroll.Add (myLabels);

            //				print (root.lesserSubNode, x1 - (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50);
            //				print (root.greaterSubNode, x1 + (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50);
            //
            //

                print (root.lesserSubNode, x1 - (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50);

                print (root.greaterSubNode, x1 + (25*Math.Pow(2,myNodalArray.level-(root.levelindex+1))), y1 + 50);

            }
        }
Example #23
0
 public Tree()
 {
     root = null;
 }
Example #24
0
        private Node DeleteKey(Node theRootNode, int Key)
        {
            if (theRootNode == null)
                return theRootNode;

            if (Key < theRootNode.NodeValue) {

                theRootNode.lesserSubNode = DeleteKey (theRootNode.lesserSubNode, Key);

            } else if (Key > theRootNode.NodeValue) {

                theRootNode.greaterSubNode = DeleteKey (theRootNode.greaterSubNode, Key);

            } else {

                if (theRootNode.lesserSubNode == null) {
                    return theRootNode.greaterSubNode;
                } else if (theRootNode.greaterSubNode == null) {
                    return theRootNode.lesserSubNode;
                }

                // node with two children: smallest in the right subtree
                Node temp = new Node (theRootNode.greaterSubNode);
                while (true) {
                    if (temp.lesserSubNode == null)
                        break;
                    temp = temp.lesserSubNode;
                }

                // Copy the inorder successor's content to this node

                theRootNode.NodeValue = temp.NodeValue;

                // Delete the inorder successor
                theRootNode.greaterSubNode = DeleteKey (theRootNode.greaterSubNode, temp.NodeValue);
            }
            return theRootNode;
        }
 public BinarySearchTree()
 {
     count = 0;
     root = null;
     current_Node = root;
 }