예제 #1
0
파일: Program.cs 프로젝트: naumanmunir/BST
        //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;
                }
            }
        }
예제 #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;
        }
예제 #3
0
파일: Program.cs 프로젝트: naumanmunir/BST
        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;
                    }
                }
            }
        }
예제 #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
            {

            }
        }
예제 #6
0
파일: BST.cs 프로젝트: 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;
                }
            }
        }
예제 #7
0
파일: BST.cs 프로젝트: 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();
        }
예제 #8
0
파일: BST.cs 프로젝트: 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);
        }
예제 #9
0
파일: BST.cs 프로젝트: 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);
        }
예제 #10
0
파일: BST.cs 프로젝트: 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;
        }
예제 #11
0
 public void Delete(int deleteValue)
 {
     root = DeleteKey (root, deleteValue);
 }
예제 #12
0
파일: Node.cs 프로젝트: Bvaptsarov/Homework
 public Node(int data)
 {
     this.Value = data;
     left = null;
     right = null;
 }
예제 #13
0
 public Node(int data)
 {
     NodeValue = data;
     greaterSubNode = null;
     lesserSubNode = null;
 }
예제 #14
0
파일: Program.cs 프로젝트: naumanmunir/BST
 public void Print(Node root)
 {
     if(root != null)
     {
         Print(root.leftNode);
         Console.WriteLine(root.data + " ");
         Print(root.rightNode);
     }
 }
예제 #15
0
파일: BST.cs 프로젝트: 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;
        }
예제 #16
0
파일: Program.cs 프로젝트: naumanmunir/BST
 public Node(int d)
 {
     this.data = d;
     leftNode = null;
     rightNode = null;
 }
예제 #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;
                    }

                }

            }
        }
예제 #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);
            }
        }
예제 #19
0
        /// <summary>
        /// 中序遍历(先输出根节点,再输出左子树,最后输出右子树)
        /// 中序遍历输出的顺序总是有序的。
        /// </summary>
        public void InOrder(Node node)
        {
            if (null != node)
            {

                InOrder(node.LChild);

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

                InOrder(node.RChild);

            }
        }
예제 #20
0
파일: BST.cs 프로젝트: 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;
        }
예제 #21
0
 public Node(Node theinputnode)
 {
     greaterSubNode = theinputnode.greaterSubNode;
     lesserSubNode = theinputnode.lesserSubNode;
     NodeValue = theinputnode.NodeValue;
 }
예제 #22
0
        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);

            }
        }
예제 #23
0
파일: Program.cs 프로젝트: naumanmunir/BST
 public Tree()
 {
     root = null;
 }
예제 #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;
 }