Code is in Java In pictures I will provide full code, AVL Tree utilizes BinNode Class that will provide here and in image The AVL tree is an extension of a BinaryTree and uses its search method... but for some reason search method cannot find the data even though it should be in the tree (For AVL it does not work but for Binary Tree it does)... can you please provide help in fixing the SearchMethod to work with AVL Tree? //Binary Tree Search Method public BinNode search(String val) { return search(this.getRoot(), val); } private BinNode search(BinNode r, String val) { if (r == null) { return null; } int comparison = val.compareTo(r.getData()); if (comparison < 0) { return search(r.getLeft(), val); } else if (comparison > 0) { return search(r.getRight(), val); } else { return r; } } //Bin Node Class public class BinNode { private String data; private BinNode left; private BinNode right; private int height; private boolean color; private BinNode parent; public BinNode(){ data = ""; left = null; right = null; parent = null; color = false; // Default color is black } public boolean getColor() { return color; } public void setColor(boolean color) { this.color = color; } public BinNode getParent() { return parent; } public void setParent(BinNode parent) { this.parent = parent; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public BinNode(String d){ data = d; left = null; right = null; } public void setData(String d){ this.data = d; } public String getData(){ return this.data; } public void setLeft(BinNode l){ this.left = l; } public BinNode getLeft(){ return this.left; } public void setRight(BinNode r){ this.right = r; } public BinNode getRight(){ return this.right; } }   //AVL Tree Class public class AVLTree extends BinaryTree { private BinNode root; AVLTree() { root = null; } private int height(BinNode node) { return (node == null) ? 0 : node.getHeight(); } private int getBalance(BinNode node) { return (node == null) ? 0 : height(node.getLeft()) - height(node.getRight()); } private BinNode rightRotate(BinNode y) { BinNode x = y.getLeft(); BinNode T2 = x.getRight(); x.setRight(y); y.setLeft(T2); y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1); x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1); return x; } private BinNode leftRotate(BinNode x) { BinNode y = x.getRight(); BinNode T2 = y.getLeft(); y.setLeft(x); x.setRight(T2); x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1); y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1); return y; } @Override public void insert(String data) { root = insert(root, data); } private BinNode insert(BinNode node, String data) { if (node == null) { return new BinNode(data); } int cmp = data.compareTo(node.getData()); if (cmp < 0) { node.setLeft(insert(node.getLeft(), data)); } else if (cmp > 0) { node.setRight(insert(node.getRight(), data)); } else { // Duplicate values are not allowed in this AVL tree return node; } node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight()))); int balance = getBalance(node); if (balance > 1) { if (data.compareTo(node.getLeft().getData()) < 0) { return rightRotate(node); } else { node.setLeft(leftRotate(node.getLeft())); return rightRotate(node); } } if (balance < -1) { if (data.compareTo(node.getRight().getData()) > 0) { return leftRotate(node); } else { node.setRight(rightRotate(node.getRight())); return leftRotate(node); } } return node; } @Override public void remove(String data) { root = remove(root, data); } private BinNode remove(BinNode node, String data) { if (node == null) { return node; } int cmp = data.compareTo(node.getData()); if (cmp < 0) { node.setLeft(remove(node.getLeft(), data)); } else if (cmp > 0) { node.setRight(remove(node.getRight(), data)); } else { // Node found, perform deletion if (node.getLeft() == null || node.getRight() == null) { BinNode temp = (node.getLeft() != null) ? node.getLeft() : node.getRight(); if (temp == null) { temp = node; node = null; } else { node = temp; } } else { BinNode temp = minValueNode(node.getRight()); node.setData(temp.getData()); node.setRight(remove(node.getRight(), temp.getData())); } } if (node == null) { return node; } node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight())));

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 13PE
icon
Related questions
Question

Code is in Java

In pictures I will provide full code, AVL Tree utilizes BinNode Class that will provide here and in image

The AVL tree is an extension of a BinaryTree and uses its search method... but for some reason search method cannot find the data even though it should be in the tree (For AVL it does not work but for Binary Tree it does)... can you please provide help in fixing the SearchMethod to work with AVL Tree?

//Binary Tree Search Method

public BinNode search(String val)
{
return search(this.getRoot(), val);
}
private BinNode search(BinNode r, String val)
{

if (r == null) {
return null;
}

int comparison = val.compareTo(r.getData());

if (comparison < 0) {
return search(r.getLeft(), val);
} else if (comparison > 0) {
return search(r.getRight(), val);
} else {
return r;
}
}

//Bin Node Class

public class BinNode
{
private String data;
private BinNode left;
private BinNode right;

private int height;

private boolean color;

private BinNode parent;


public BinNode(){
data = "";
left = null;
right = null;
parent = null;
color = false; // Default color is black
}

public boolean getColor() {
return color;
}

public void setColor(boolean color) {
this.color = color;
}
public BinNode getParent() {
return parent;
}

public void setParent(BinNode parent) {
this.parent = parent;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
public BinNode(String d){
data = d;
left = null;
right = null;
}

public void setData(String d){
this.data = d;
}
public String getData(){
return this.data;
}
public void setLeft(BinNode l){
this.left = l;
}
public BinNode getLeft(){
return this.left;
}
public void setRight(BinNode r){
this.right = r;
}
public BinNode getRight(){
return this.right;
}
}

 

//AVL Tree Class

public class AVLTree extends BinaryTree {

private BinNode root;

AVLTree() {
root = null;
}

private int height(BinNode node) {
return (node == null) ? 0 : node.getHeight();
}

private int getBalance(BinNode node) {
return (node == null) ? 0 : height(node.getLeft()) - height(node.getRight());
}

private BinNode rightRotate(BinNode y) {
BinNode x = y.getLeft();
BinNode T2 = x.getRight();

x.setRight(y);
y.setLeft(T2);

y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1);
x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1);

return x;
}

private BinNode leftRotate(BinNode x) {
BinNode y = x.getRight();
BinNode T2 = y.getLeft();

y.setLeft(x);
x.setRight(T2);

x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1);
y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1);

return y;
}

@Override
public void insert(String data) {
root = insert(root, data);
}

private BinNode insert(BinNode node, String data) {
if (node == null) {
return new BinNode(data);
}

int cmp = data.compareTo(node.getData());

if (cmp < 0) {
node.setLeft(insert(node.getLeft(), data));
} else if (cmp > 0) {
node.setRight(insert(node.getRight(), data));
} else {
// Duplicate values are not allowed in this AVL tree
return node;
}

node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight())));

int balance = getBalance(node);

if (balance > 1) {
if (data.compareTo(node.getLeft().getData()) < 0) {
return rightRotate(node);
} else {
node.setLeft(leftRotate(node.getLeft()));
return rightRotate(node);
}
}

if (balance < -1) {
if (data.compareTo(node.getRight().getData()) > 0) {
return leftRotate(node);
} else {
node.setRight(rightRotate(node.getRight()));
return leftRotate(node);
}
}

return node;
}

@Override
public void remove(String data) {
root = remove(root, data);
}

private BinNode remove(BinNode node, String data) {
if (node == null) {
return node;
}

int cmp = data.compareTo(node.getData());

if (cmp < 0) {
node.setLeft(remove(node.getLeft(), data));
} else if (cmp > 0) {
node.setRight(remove(node.getRight(), data));
} else {
// Node found, perform deletion
if (node.getLeft() == null || node.getRight() == null) {
BinNode temp = (node.getLeft() != null) ? node.getLeft() : node.getRight();

if (temp == null) {
temp = node;
node = null;
} else {
node = temp;
}
} else {
BinNode temp = minValueNode(node.getRight());
node.setData(temp.getData());
node.setRight(remove(node.getRight(), temp.getData()));
}
}

if (node == null) {
return node;
}

node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight())));

int balance = getBalance(node);

if (balance > 1) {
if (getBalance(node.getLeft()) >= 0) {
return rightRotate(node);
} else {
node.setLeft(leftRotate(node.getLeft()));
return rightRotate(node);
}
}

if (balance < -1) {
if (getBalance(node.getRight()) <= 0) {
return leftRotate(node);
} else {
node.setRight(rightRotate(node.getRight()));
return leftRotate(node);
}
}

return node;
}

private BinNode minValueNode(BinNode node) {
BinNode current = node;
while (current.getLeft() != null) {
current = current.getLeft();
}
return current;
}

public void display() {
display(root);
}

private void display(BinNode node) {
if (node != null) {
display(node.getLeft());
System.out.print(node.getData() + " ");
display(node.getRight());
}
}
}

### Java Class: BinNode

The `BinNode` class is designed to represent a node within a binary tree structure, often used in various tree data structures like AVL trees or Red-Black trees. Below is an explanation of the class and its components:

#### Class Definition
- **public class BinNode**
  - Declares a public class named `BinNode`.

#### Instance Variables
- **private String data;**
  - Holds the data as a string.

- **private BinNode left;**
  - Reference to the left child node of the current node.

- **private BinNode right;**
  - Reference to the right child node.

- **private int height;**
  - Used to store the height of the node, applicable in AVL or Red-Black trees.

- **private boolean color;**
  - Indicates the color of the node, necessary for Red-Black trees. Default is black (`false`).

- **private BinNode parent;**
  - A reference to the parent node, useful for navigating or restructuring the tree.

#### Constructors
- **public BinNode()**
  - Default constructor initializing `data` to an empty string, and all node references (`left`, `right`, `parent`) to `null`. Sets the color to black.

- **public BinNode(String d)**
  - Overloaded constructor that initializes the `data` with the provided string and sets `left`, `right` references to `null`.

#### Getter and Setter Methods
- **public boolean getColor()**
  - Returns the node's color.

- **public void setColor(boolean color)**
  - Sets the node's color.

- **public BinNode getParent()**
  - Returns the parent node.

- **public void setParent(BinNode parent)**
  - Sets the parent node reference.

- **public int getHeight()**
  - Returns the height of the node.

- **public void setHeight(int height)**
  - Sets the height of the node.

- **public void setData(String d)**
  - Sets the data for the node.

- **public String getData()**
  - Returns the data stored in the node.

- **public void setLeft(BinNode l)**
  - Sets the left child node.

- **public BinNode getLeft()**
  - Retrieves the left child node.

- **public void setRight(BinNode r)**
  - Sets the
Transcribed Image Text:### Java Class: BinNode The `BinNode` class is designed to represent a node within a binary tree structure, often used in various tree data structures like AVL trees or Red-Black trees. Below is an explanation of the class and its components: #### Class Definition - **public class BinNode** - Declares a public class named `BinNode`. #### Instance Variables - **private String data;** - Holds the data as a string. - **private BinNode left;** - Reference to the left child node of the current node. - **private BinNode right;** - Reference to the right child node. - **private int height;** - Used to store the height of the node, applicable in AVL or Red-Black trees. - **private boolean color;** - Indicates the color of the node, necessary for Red-Black trees. Default is black (`false`). - **private BinNode parent;** - A reference to the parent node, useful for navigating or restructuring the tree. #### Constructors - **public BinNode()** - Default constructor initializing `data` to an empty string, and all node references (`left`, `right`, `parent`) to `null`. Sets the color to black. - **public BinNode(String d)** - Overloaded constructor that initializes the `data` with the provided string and sets `left`, `right` references to `null`. #### Getter and Setter Methods - **public boolean getColor()** - Returns the node's color. - **public void setColor(boolean color)** - Sets the node's color. - **public BinNode getParent()** - Returns the parent node. - **public void setParent(BinNode parent)** - Sets the parent node reference. - **public int getHeight()** - Returns the height of the node. - **public void setHeight(int height)** - Sets the height of the node. - **public void setData(String d)** - Sets the data for the node. - **public String getData()** - Returns the data stored in the node. - **public void setLeft(BinNode l)** - Sets the left child node. - **public BinNode getLeft()** - Retrieves the left child node. - **public void setRight(BinNode r)** - Sets the
### BinaryTree Class Overview

The `BinaryTree` class in this code provides a basic implementation of a binary search tree (BST), which is a type of data structure that maintains sorted order for efficient operations. Below is an explanation of the key functions and components present in the code.

#### Class Members
- `private BinNode root;`  
  Represents the root node of the binary tree.
  
- `private BinNode curr;`  
  Used for current node traversal.

#### Constructors
- `public BinaryTree()`
  - Initializes the tree with a new root `BinNode`.

#### Methods

1. **Getter for Root**
   - `public BinNode getRoot()`  
     Returns the current root of the tree.

2. **Checking If Tree is Empty**
   - `public boolean isEmpty()`  
     Checks if the root is `null`, returning true if the tree is empty.

3. **Insert Functionality**
   - `public void insert(String add)`  
     Calls the `insert` method to add a node with the specified data to the tree.

   - `private BinNode insert(BinNode node, String data)`
     - Inserts a new node in a recursive manner. Traverses left or right based on the comparison of node values and inserts accordingly.

4. **Count Nodes**
   - `private int countNodes(BinNode r)`  
     Recursively counts and returns the number of nodes in the subtree rooted at `r`.

5. **Search Functionality**
   - `public BinNode search(String val)`  
     Searches for a node with the specified value starting from the root.

   - `private BinNode search(BinNode r, String val)`  
     Recursive search function to find the node with specified data.

6. **Traversal Methods**
   - `public void inorder()`  
     Performs an in-order traversal of the tree and prints node values.

   - `private void inorder(BinNode r)`  
     Helper recursive function for in-order traversal.

   - `public void preorder()`  
     Performs a pre-order traversal of the tree and prints node values.

   - `private void preorder(BinNode r)`  
     Helper recursive function for pre-order traversal.

   - `public void postorder()`  
     Performs a post-order traversal of the tree and prints node values.

   - `private void postorder(BinNode r)`  
     Helper recursive function for post-order traversal.

7. **Finding
Transcribed Image Text:### BinaryTree Class Overview The `BinaryTree` class in this code provides a basic implementation of a binary search tree (BST), which is a type of data structure that maintains sorted order for efficient operations. Below is an explanation of the key functions and components present in the code. #### Class Members - `private BinNode root;` Represents the root node of the binary tree. - `private BinNode curr;` Used for current node traversal. #### Constructors - `public BinaryTree()` - Initializes the tree with a new root `BinNode`. #### Methods 1. **Getter for Root** - `public BinNode getRoot()` Returns the current root of the tree. 2. **Checking If Tree is Empty** - `public boolean isEmpty()` Checks if the root is `null`, returning true if the tree is empty. 3. **Insert Functionality** - `public void insert(String add)` Calls the `insert` method to add a node with the specified data to the tree. - `private BinNode insert(BinNode node, String data)` - Inserts a new node in a recursive manner. Traverses left or right based on the comparison of node values and inserts accordingly. 4. **Count Nodes** - `private int countNodes(BinNode r)` Recursively counts and returns the number of nodes in the subtree rooted at `r`. 5. **Search Functionality** - `public BinNode search(String val)` Searches for a node with the specified value starting from the root. - `private BinNode search(BinNode r, String val)` Recursive search function to find the node with specified data. 6. **Traversal Methods** - `public void inorder()` Performs an in-order traversal of the tree and prints node values. - `private void inorder(BinNode r)` Helper recursive function for in-order traversal. - `public void preorder()` Performs a pre-order traversal of the tree and prints node values. - `private void preorder(BinNode r)` Helper recursive function for pre-order traversal. - `public void postorder()` Performs a post-order traversal of the tree and prints node values. - `private void postorder(BinNode r)` Helper recursive function for post-order traversal. 7. **Finding
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Problems on Amortized Analysis
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning