public BinarySearchTreeNode Insert(BinarySearchTreeNode root, int data)
        {
            if (root == null)
            {
                root = new BinarySearchTreeNode(data);
                return(root);
            }

            if (data < root.GetBinarySearchNodeData())
            {
                root.SetBinarySearchNodeLeft(Insert(root.GetBinarySearchTreeNodeLeft(), data));
            }
            else
            {
                root.SetBinarySearchNodeRight(Insert(root.GetBinarySearchTreeNodeRight(), data));
            }

            return(root);
        }
        private void _PrintNthLargestValue(BinarySearchTreeNode root, int temp, int N)
        {
            if (root == null || (temp > N))
            {
                return;
            }

            _PrintNthLargestValue(root.GetBinarySearchTreeNodeRight(), temp, N);

            temp++;

            if (temp == N)
            {
                Console.WriteLine("The  " + N +
                                  " rd/(th)largest value is " + root.GetBinarySearchNodeData());
                return;
            }


            _PrintNthLargestValue(root.GetBinarySearchTreeNodeLeft(), temp, N);
        }
Exemplo n.º 3
0
        private static BinarySearchTree ConstructTreeFromInput()
        {
            BinarySearchTree binarySearchTree = null;

            Console.WriteLine("Enter the number of elements in BST");
            try
            {
                int noElements = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the elements separated by space");
                String[] elements = Console.ReadLine().Split(' ');
                binarySearchTree = new BinarySearchTree();
                BinarySearchTreeNode binarySearchTreeNode = null;
                for (int i = 0; i < noElements; i++)
                {
                    binarySearchTreeNode = binarySearchTree.Insert(binarySearchTreeNode, int.Parse(elements[i]));
                }
                binarySearchTree.SetBinarySearchTreeRoot(binarySearchTreeNode);
            }
            catch (Exception exception) {
                Console.WriteLine("Thrown exception is " + exception.Message);
            }

            return(binarySearchTree);
        }
 public void SetBinarySearchTreeRoot(BinarySearchTreeNode root)
 {
     this.root = root;
 }
 public BinarySearchTree(BinarySearchTreeNode binarySearchTreeNode)
 {
     root = binarySearchTreeNode;
 }
 public void SetBinarySearchNodeRight(BinarySearchTreeNode right)
 {
     this.right = right;
 }
 public void SetBinarySearchNodeLeft(BinarySearchTreeNode left)
 {
     this.left = left;
 }