import java.util.Scanner;
import java.io.*;
class BinaryTree {
int data;
BinaryTree left;
BinaryTree right;
static int count=0;
public BinaryTree(int num) {
this.data = num;
this.left = null;
this.right = null;
}
public void addNode(int num) {
if (num < this.data) {
if (this.left != null) {
this.left.addNode(num);
} else {
this.left = new BinaryTree(num);
}
} else {
if (this.right != null) {
this.right.addNode(num);
} else {
this.right = new BinaryTree(num);
}
}
}
public void traversePreOrder() {
System.out.print( this.data + " ");
if( this.left != null ) {
this.left.traversePreOrder();
}
if( this.right != null ) {
this.right.traversePreOrder();
}
}
public void traverseInOrder() {
if( this.left != null ) {
this.left.traverseInOrder();
}
System.out.print( this.data + " ");
if( this.right != null ) {
this.right.traverseInOrder();
}
}
public void traversePostOrder() {
if( this.left != null ) {
this.left.traversePostOrder();
}
if( this.right != null ) {
this.right.traversePostOrder();
}
System.out.print( this.data + " ");
}
public void largestValue() {
if(null == this) {
System.out.println("Tree is empty");
return;
}
//we found the max value
if(this.right == null)
System.out.println("Largest element is: " +this.data);
else
this.right.largestValue();
}
public int smallestValue() {
if(null == this) {
System.out.println("Tree is empty");
return Integer.MIN_VALUE;
}
//we found the max value
if(this.left == null)
return this.data;
else
return this.left.smallestValue();
}
void find(int value){
if(this.data == value) {
System.out.println("value found");
return;
}
else if(value < this.data){
if(this.left==null){
System.out.println("value not found");
return;
}
else
this.left.find(value);
}
else{
if(this.right==null){
System.out.println("value not found");
return;
}
else
this.right.find(value);
}
}
public int countNodes() {
if( null == this ) {
System.out.println("bye");
return 0;
}
else
return 1 + this.left.countNodes( ) + this.right.countNodes( );
}
public static void main(String ar[]) throws Exception{
Scanner sc=new Scanner(new File("data.txt"));
int j=0;
BinaryTree tree = new BinaryTree( sc.nextInt() );
int[] nums=new int[100];
while(sc.hasNext()){
nums[j++]=sc.nextInt();
}
//int[] nums = {15, 200, 25, -5, 0, 100, 20, 12, 126, 1000,
-150};
for(int i : nums ) {
tree.addNode( i );
}
System.out.println("\nPreorder Traversal");
tree.traversePreOrder();
System.out.println("\nInorder Traversal");
tree.traverseInOrder();
System.out.println("\nPostorder Traversal");
tree.traversePostOrder();
System.out.println("");
tree.largestValue();
int min=tree.smallestValue();
System.out.println("Smallest element is: " +min);
tree.find(201);
int total=tree.countNodes();
System.out.println("total Nodes are: "+total);
}
}
-----------------------
contents of the file are
11
15
200
25
-5
0
100
20
12
126
1000
-150
where first element is the size and rest are the elements.
Problemi: Create a new Java Application that has the following methods: 1. A method that generate...
Create a new Java Application that has the following methods: A method that generate a binary search tree (BST) where the number of nodes and the range of the values are from a file. A recursive method to print the BST in preorder traversal A recursive method to print the BST in post-order traversal A recursive method to print the BST in in-order traversal A recursive method to count the number of all nodes in BST A method to find...
please use java application to solve methods:
5,6,10,12,13 (no need for the rest)
Problem1: Create a new Java Application that has the following methods: 6 A method that generate a binary search tree (BST) where the number of nodes and the range of the values are 1. from a file. 2. A recursive method to print the BST in preorder traversal 3. A recursive method to print the BST in post-order traversal 4. A recursive method to print the BST...
Please Only use C language In this assignment, you have to read a text file (in.txt) that contains a set of words. The first line of the file contains 3 numbers (N, S, D). These numbers represent the sequence of input words in your file. N: represents the number of words to read to build a binary search tree. You have to write a recursive insert code to create and insert these words into the binary search tree. After inserting...
*** using java application
Q1. BST: 1. Write a method that takes an array A, as parameter (A contains the elements of a BST traversed in preorder) and returns the corresponding BST 2. Write a method to list the nodes on the path from the root to a given node in the BST. 3. Write a recursive method to check if two BSTs are same. 4. Write a non-recursive method to check if two BSTs are same 5. Write a...
Goal: design and implement a dictionary. implement your dictionary using AVL tree . Problem: Each entry in the dictionary is a pair: (word, meaning). Word is a one-word string, meaning can be a string of one or more words (it’s your choice of implementation, you can restrict the meaning to one-word strings). The dictionary is case-insensitive. It means “Book”, “BOOK”, “book” are all the same . Your dictionary application must provide its operations through the following menu (make sure that...
Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...
Overview: file you have to complete is
WordTree.h, WordTree.cpp, main.cpp
Write a program in C++ that reads an input text
file and counts the occurrence of individual words in the file. You
will see a binary tree to keep track of words and their counts.
Project description:
The program should open and read an input file (named
input.txt) in turn, and build a binary search tree
of the words and their counts. The words will be stored in
alphabetical order...
Write a client method that returns a count of the number of
nodes in a binary search tree that contain scores less than or
equal to a passed-in argument (parameter) value. The method header
is:
int countLowerScores (BinarySearchTree tree, int maxValue)
The BinarySearchTree contains these methods in the
picture.
public class BinarySearchTreecT> implements BSTInterfacecT> //reference to the root of this BST public BSTNode<T> root; public Comparator<T> comp: IIused for all comparisons public boolean found: / used by remove public BinarYSearchTree()...
using java to write,show me the output. please write some
common.
You CAN NOT use inbuild functions for Tree ADT operations.
using code below to finsih
public class Main
{
public static void main(String[] args) {
BinaryTree tree = new
BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
tree.root.left.left.left = new Node(8);
tree.root.left.left .right= new Node(9);...
(in C) Binry Srch tree problem: 1. Need to read words from a “in” text file and build a binary search tree. (1st line will state number of elements) 2. strcmp() can be used to compare data and decide were to insert a new node. 3. The output should include: -print the tree as in-order traversal. -print the character count in the tree. -Ask user input for a word to search, print an output if either found or not. (typing...