aaaa
This commit is contained in:
parent
7962e14215
commit
3047558ef6
@ -0,0 +1,290 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>TreeASDV.java</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<style type="text/css">
|
||||
<!--
|
||||
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.ST1 {color: #9876aa}
|
||||
.ST2 {color: #ffc66d}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java</td></tr></table>
|
||||
<pre>
|
||||
<span class="comment">/*</span>
|
||||
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
||||
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.projecttrees_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.LinkedList;
|
||||
<span class="literal">import</span> java.util.ListIterator;
|
||||
<span class="literal">import</span> java.util.Queue;
|
||||
<span class="literal">import</span> java.util.Stack;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> TreeASDV<T <span class="literal">extends</span> Comparable> {
|
||||
|
||||
<span class="literal">private</span> Node<T> <span class="ST1">root</span>;
|
||||
|
||||
<span class="literal">class</span> Node<T> {
|
||||
|
||||
T <span class="ST1">data</span>;
|
||||
Node<T> <span class="ST1">leftChild</span>;
|
||||
Node<T> <span class="ST1">rightChild</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">insert</span>(T t) {
|
||||
Node<T> newNode = <span class="literal">new</span> Node<>();
|
||||
newNode.<span class="ST1">data</span> = t;
|
||||
|
||||
<span class="literal">if</span> (<span class="ST1">root</span> == <span class="literal">null</span>) {
|
||||
<span class="ST1">root</span> = newNode;
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
Node<T> current = <span class="ST1">root</span>;
|
||||
Node<T> parent = <span class="literal">null</span>;
|
||||
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span>) {
|
||||
parent = current;
|
||||
<span class="literal">if</span> (t.compareTo(current.<span class="ST1">data</span>) >= <span class="number">0</span>) {
|
||||
current = current.<span class="ST1">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">// At this point, 'parent' is the node where the new node should be inserted as a child</span>
|
||||
<span class="literal">if</span> (t.compareTo(parent.<span class="ST1">data</span>) >= <span class="number">0</span>) {
|
||||
parent.<span class="ST1">rightChild</span> = newNode;
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST1">leftChild</span> = newNode;
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">void</span> <span class="ST2">inOrder</span>(Node<T> p) {
|
||||
<span class="literal">if</span> (p == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
inOrder(p.<span class="ST1">leftChild</span>);
|
||||
System.<span class="ST3">out</span>.print(p.<span class="ST1">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
inOrder(p.<span class="ST1">rightChild</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">inOrder</span>() {
|
||||
inOrder(<span class="literal">t</span><span class="literal">his</span>.<span class="ST1">root</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> Node<T> <span class="ST2">findNode</span>(T t) {
|
||||
Node<T> currentNode = <span class="ST1">root</span>;
|
||||
<span class="literal">while</span> (currentNode != <span class="literal">null</span>) {
|
||||
<span class="literal">if</span> (t.compareTo(currentNode.<span class="ST1">data</span>) == <span class="number">0</span>) {
|
||||
<span class="literal">return</span> currentNode;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (t.compareTo(currentNode.<span class="ST1">data</span>) > <span class="number">0</span>) {
|
||||
currentNode = currentNode.<span class="ST1">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
currentNode = currentNode.<span class="ST1">leftChild</span>;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">null</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">remove</span>(T t) {
|
||||
<span class="comment">// Initialize parent and current nodes for traversal</span>
|
||||
Node<T> parent = <span class="literal">null</span>;
|
||||
Node<T> current = <span class="ST1">root</span>;
|
||||
|
||||
<span class="comment">// Search for the node to be removed</span>
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span> && !current.<span class="ST1">data</span>.equals(t)) {
|
||||
parent = current;
|
||||
<span class="literal">if</span> (t.compareTo(current.<span class="ST1">data</span>) > <span class="number">0</span>) {
|
||||
current = current.<span class="ST1">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">// If node not found, return false</span>
|
||||
<span class="literal">if</span> (current == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// Case 1: Node with no children</span>
|
||||
<span class="literal">if</span> (current.<span class="ST1">leftChild</span> == <span class="literal">null</span> && current.<span class="ST1">rightChild</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">if</span> (current == <span class="ST1">root</span>) {
|
||||
<span class="ST1">root</span> = <span class="literal">null</span>; <span class="comment">// Removing root node</span>
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST1">leftChild</span> == current) {
|
||||
parent.<span class="ST1">leftChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a left child</span>
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST1">rightChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a right child</span>
|
||||
}
|
||||
} <span class="comment">// Case 2: Node with one child</span>
|
||||
<span class="literal">else</span> <span class="literal">if</span> (current.<span class="ST1">leftChild</span> == <span class="literal">null</span> || current.<span class="ST1">rightChild</span> == <span class="literal">null</span>) {
|
||||
Node<T> child = (current.<span class="ST1">leftChild</span> != <span class="literal">null</span>) ? current.<span class="ST1">leftChild</span> : current.<span class="ST1">rightChild</span>;
|
||||
<span class="literal">if</span> (current == <span class="ST1">root</span>) {
|
||||
<span class="ST1">root</span> = child; <span class="comment">// Replace root with its child</span>
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST1">leftChild</span> == current) {
|
||||
parent.<span class="ST1">leftChild</span> = child; <span class="comment">// Replace parent's left child with the node's child</span>
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST1">rightChild</span> = child; <span class="comment">// Replace parent's right child with the node's child</span>
|
||||
}
|
||||
} <span class="comment">// Case 3: Node with two children</span>
|
||||
<span class="literal">else</span> {
|
||||
Node<T> successor = getSuccessor(current);
|
||||
current.<span class="ST1">data</span> = successor.<span class="ST1">data</span>; <span class="comment">// Replace data with successor's data</span>
|
||||
<span class="comment">// Remove successor node (successor will have at most one right child)</span>
|
||||
remove(successor.<span class="ST1">data</span>);
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// Helper method to find in-order successor of a node</span>
|
||||
<span class="literal">private</span> Node<T> <span class="ST2">getSuccessor</span>(Node<T> node) {
|
||||
Node<T> current = node.<span class="ST1">rightChild</span>;
|
||||
Node<T> successorParent = node;
|
||||
Node<T> successor = node;
|
||||
|
||||
<span class="comment">// Find the leftmost node in the right subtree (in-order successor)</span>
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span>) {
|
||||
successorParent = successor;
|
||||
successor = current;
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// If the successor is not the right child of the node to be removed,</span>
|
||||
<span class="comment">// adjust the successor's parent's leftChild reference</span>
|
||||
<span class="literal">if</span> (successor != node.<span class="ST1">rightChild</span>) {
|
||||
successorParent.<span class="ST1">leftChild</span> = successor.<span class="ST1">rightChild</span>;
|
||||
successor.<span class="ST1">rightChild</span> = node.<span class="ST1">rightChild</span>;
|
||||
}
|
||||
|
||||
<span class="literal">return</span> successor;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> ListIterator<T> <span class="ST2">listIterator</span>() {
|
||||
<span class="comment">//ListIterator it = new ListIterator<T>();</span>
|
||||
<span class="literal">return</span> <span class="literal">null</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">breadthFirstTraversal</span>() {
|
||||
<span class="literal">if</span> (<span class="ST1">root</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
Queue<Node<T>> queue = <span class="literal">new</span> LinkedList<>();
|
||||
queue.offer(<span class="ST1">r</span><span class="ST1">oot</span>);
|
||||
|
||||
<span class="literal">while</span> (!queue.isEmpty()) {
|
||||
Node<T> current = queue.poll();
|
||||
System.<span class="ST3">out</span>.print(current.<span class="ST1">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
|
||||
<span class="literal">if</span> (current.<span class="ST1">leftChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST1">leftChild</span>);
|
||||
}
|
||||
<span class="literal">if</span> (current.<span class="ST1">rightChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST1">rightChild</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">height</span>() {
|
||||
<span class="literal">return</span> calculateHeight(<span class="ST1">r</span><span class="ST1">oot</span>);
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">calculateHeight</span>(Node<T> node) {
|
||||
<span class="literal">if</span> (node == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
|
||||
<span class="literal">int</span> leftHeight = calculateHeight(node.<span class="ST1">leftChild</span>);
|
||||
<span class="literal">int</span> rightHeight = calculateHeight(node.<span class="ST1">rightChild</span>);
|
||||
|
||||
<span class="literal">return</span> <span class="number">1</span> + Math.<span class="ST4">max</span>(leftHeight, rightHeight);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">isFullBST</span>() {
|
||||
<span class="literal">int</span> height = height();
|
||||
<span class="literal">int</span> nodeCount = countNodes(<span class="ST1">r</span><span class="ST1">oot</span>);
|
||||
<span class="literal">return</span> nodeCount == (<span class="number">1</span> << height) - <span class="number">1</span>; <span class="comment">// Formula for full binary tree</span>
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">countNodes</span>(Node<T> node) {
|
||||
<span class="literal">if</span> (node == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
<span class="literal">return</span> <span class="number">1</span> + countNodes(node.<span class="ST1">leftChild</span>) + countNodes(node.<span class="ST1">rightChild</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">inorder</span>() {
|
||||
<span class="literal">if</span> (<span class="ST1">root</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
Stack<Node<T>> stack = <span class="literal">new</span> Stack<>();
|
||||
Node<T> current = <span class="ST1">root</span>;
|
||||
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span> || !stack.isEmpty()) {
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span>) {
|
||||
stack.push(current);
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
}
|
||||
current = stack.pop();
|
||||
System.<span class="ST3">out</span>.print(current.<span class="ST1">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
current = current.<span class="ST1">rightChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
|
||||
TreeASDV<Integer> tree = <span class="literal">new</span> TreeASDV<>();
|
||||
<span class="comment">// Insert some elements into the tree</span>
|
||||
tree.insert(<span class="number">5</span>);
|
||||
tree.insert(<span class="number">3</span>);
|
||||
tree.insert(<span class="number">7</span>);
|
||||
tree.insert(<span class="number">2</span>);
|
||||
tree.insert(<span class="number">4</span>);
|
||||
tree.insert(<span class="number">6</span>);
|
||||
tree.insert(<span class="number">8</span>);
|
||||
|
||||
<span class="comment">// Test breadth-first traversal</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Breadth-First Traversal:</span><span class="string">"</span>);
|
||||
tree.breadthFirstTraversal();
|
||||
System.<span class="ST3">out</span>.println();
|
||||
|
||||
<span class="comment">// Test height calculation</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Height of the tree: </span><span class="string">"</span> + tree.height());
|
||||
|
||||
<span class="comment">// Test if the tree is a full binary tree</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Is the tree a full binary tree? </span><span class="string">"</span> + tree.isFullBST());
|
||||
|
||||
<span class="comment">// Test inorder traversal without recursion</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Inorder Traversal without Recursion:</span><span class="string">"</span>);
|
||||
tree.inorder();
|
||||
System.<span class="ST3">out</span>.println();
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-shared-configuration>
|
||||
<!--
|
||||
This file contains additional configuration written by modules in the NetBeans IDE.
|
||||
The configuration is intended to be shared among all the users of project and
|
||||
therefore it is assumed to be part of version control checkout.
|
||||
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
||||
-->
|
||||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
||||
<!--
|
||||
Properties that influence various parts of the IDE, especially code formatting and the like.
|
||||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
||||
That way multiple projects can share the same settings (useful for formatting rules for example).
|
||||
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
||||
-->
|
||||
<netbeans.hint.jdkPlatform>Graal_JDK_20</netbeans.hint.jdkPlatform>
|
||||
</properties>
|
||||
</project-shared-configuration>
|
@ -4,7 +4,9 @@
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.projecttrees_calebfontenot;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Queue;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
@ -22,37 +24,38 @@ public class TreeASDV<T extends Comparable> {
|
||||
Node<T> rightChild;
|
||||
}
|
||||
|
||||
public boolean insert(T t)
|
||||
{
|
||||
Node<T> currentNode = root;
|
||||
Node<T> trailCurrent = root;
|
||||
Node<T> newNode = new Node<T>();
|
||||
public boolean insert(T t) {
|
||||
Node<T> newNode = new Node<>();
|
||||
newNode.data = t;
|
||||
System.out.println(t);
|
||||
|
||||
if (this.root == null) {
|
||||
this.root = newNode;
|
||||
if (root == null) {
|
||||
root = newNode;
|
||||
return true;
|
||||
}
|
||||
|
||||
while (currentNode != null) {
|
||||
trailCurrent = currentNode;
|
||||
if (t.compareTo(currentNode.data) >= 0) {
|
||||
currentNode = currentNode.rightChild;
|
||||
Node<T> current = root;
|
||||
Node<T> parent = null;
|
||||
|
||||
while (current != null) {
|
||||
parent = current;
|
||||
if (t.compareTo(current.data) >= 0) {
|
||||
current = current.rightChild;
|
||||
} else {
|
||||
currentNode = currentNode.leftChild;
|
||||
current = current.leftChild;
|
||||
}
|
||||
if (t.compareTo(trailCurrent.data) >= 0) { // Make the node a right child.
|
||||
trailCurrent.rightChild = newNode;
|
||||
} else { // Make the node a left child.
|
||||
trailCurrent.leftChild = newNode;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void inOrder(Node<T> p)
|
||||
{
|
||||
// At this point, 'parent' is the node where the new node should be inserted as a child
|
||||
if (t.compareTo(parent.data) >= 0) {
|
||||
parent.rightChild = newNode;
|
||||
} else {
|
||||
parent.leftChild = newNode;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void inOrder(Node<T> p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
@ -62,13 +65,11 @@ public class TreeASDV<T extends Comparable> {
|
||||
inOrder(p.rightChild);
|
||||
}
|
||||
|
||||
public void inOrder()
|
||||
{
|
||||
public void inOrder() {
|
||||
inOrder(this.root);
|
||||
}
|
||||
|
||||
public Node<T> findNode(T t)
|
||||
{
|
||||
public Node<T> findNode(T t) {
|
||||
Node<T> currentNode = root;
|
||||
while (currentNode != null) {
|
||||
if (t.compareTo(currentNode.data) == 0) {
|
||||
@ -82,103 +83,178 @@ public class TreeASDV<T extends Comparable> {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean remove(T t)
|
||||
{
|
||||
// case: no children
|
||||
if (root.leftChild == null & root.rightChild == null) {
|
||||
root = null;
|
||||
return true;
|
||||
}
|
||||
Node<T> current = this.root;
|
||||
Node<T> currentParent = this.root;
|
||||
public boolean remove(T t) {
|
||||
// Initialize parent and current nodes for traversal
|
||||
Node<T> parent = null;
|
||||
Node<T> current = root;
|
||||
|
||||
while (current.data.equals(t)) {
|
||||
if (t.compareTo(current.data) == 0) {
|
||||
break;
|
||||
}
|
||||
else if (t.compareTo(currentParent.data) > 0) {
|
||||
currentParent = current;
|
||||
// Search for the node to be removed
|
||||
while (current != null && !current.data.equals(t)) {
|
||||
parent = current;
|
||||
if (t.compareTo(current.data) > 0) {
|
||||
current = current.rightChild;
|
||||
} else {
|
||||
currentParent = current;
|
||||
current = current.leftChild;
|
||||
}
|
||||
}
|
||||
|
||||
// If node not found, return false
|
||||
if (current == null) {
|
||||
return false;
|
||||
}
|
||||
if (current.data.compareTo(currentParent.data) <= 0) {
|
||||
currentParent.leftChild = null;
|
||||
} else {
|
||||
currentParent.rightChild = null;
|
||||
}
|
||||
// case: single child
|
||||
if (current.leftChild == null || current.rightChild == null) {
|
||||
// if the current is the right child of its parent
|
||||
if (current.data.compareTo(t) > 0) {
|
||||
|
||||
// Case 1: Node with no children
|
||||
if (current.leftChild == null && current.rightChild == null) {
|
||||
if (current == root) {
|
||||
root = null; // Removing root node
|
||||
} else if (parent.leftChild == current) {
|
||||
parent.leftChild = null; // Removing a left child
|
||||
} else {
|
||||
|
||||
parent.rightChild = null; // Removing a right child
|
||||
}
|
||||
} // Case 2: Node with one child
|
||||
else if (current.leftChild == null || current.rightChild == null) {
|
||||
Node<T> child = (current.leftChild != null) ? current.leftChild : current.rightChild;
|
||||
if (current == root) {
|
||||
root = child; // Replace root with its child
|
||||
} else if (parent.leftChild == current) {
|
||||
parent.leftChild = child; // Replace parent's left child with the node's child
|
||||
} else {
|
||||
parent.rightChild = child; // Replace parent's right child with the node's child
|
||||
}
|
||||
// case: two children
|
||||
} // Case 3: Node with two children
|
||||
else {
|
||||
// take a left
|
||||
Node<T> p = current;
|
||||
p = current.leftChild;
|
||||
Node<T> pTrailParent = p;
|
||||
Node<T> pTrail = p;
|
||||
while (p != null) { // keep going right
|
||||
pTrailParent = pTrail;
|
||||
pTrail = p;
|
||||
p = p.rightChild;
|
||||
}
|
||||
// swap the data of pTrail with the current
|
||||
current.data = pTrail.data;
|
||||
if (pTrail == pTrailParent) {
|
||||
pTrailParent.leftChild = pTrail.leftChild;
|
||||
} else {
|
||||
pTrailParent.rightChild = pTrail.leftChild;
|
||||
Node<T> successor = getSuccessor(current);
|
||||
current.data = successor.data; // Replace data with successor's data
|
||||
// Remove successor node (successor will have at most one right child)
|
||||
remove(successor.data);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator(){
|
||||
ListIterator it = new ListIterator<T>();
|
||||
// Helper method to find in-order successor of a node
|
||||
private Node<T> getSuccessor(Node<T> node) {
|
||||
Node<T> current = node.rightChild;
|
||||
Node<T> successorParent = node;
|
||||
Node<T> successor = node;
|
||||
|
||||
// Find the leftmost node in the right subtree (in-order successor)
|
||||
while (current != null) {
|
||||
successorParent = successor;
|
||||
successor = current;
|
||||
current = current.leftChild;
|
||||
}
|
||||
|
||||
// If the successor is not the right child of the node to be removed,
|
||||
// adjust the successor's parent's leftChild reference
|
||||
if (successor != node.rightChild) {
|
||||
successorParent.leftChild = successor.rightChild;
|
||||
successor.rightChild = node.rightChild;
|
||||
}
|
||||
|
||||
return successor;
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator() {
|
||||
//ListIterator it = new ListIterator<T>();
|
||||
return null;
|
||||
}
|
||||
public void breathFirst() {
|
||||
Stack<Node<T>> stack = new Stack();
|
||||
Node<T> p = root;
|
||||
System.out.println(p.data + " ");
|
||||
stack.push(p.rightChild);
|
||||
|
||||
while (stack.empty() == false) {
|
||||
// go to the left
|
||||
p = p.leftChild;
|
||||
if (p != null) {
|
||||
System.out.println(p.data);
|
||||
}
|
||||
Node<T> rightChild = stack.pop();
|
||||
System.out.println();
|
||||
|
||||
public void breadthFirstTraversal() {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Queue<Node<T>> queue = new LinkedList<>();
|
||||
queue.offer(root);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Node<T> current = queue.poll();
|
||||
System.out.print(current.data + " ");
|
||||
|
||||
if (current.leftChild != null) {
|
||||
queue.offer(current.leftChild);
|
||||
}
|
||||
if (current.rightChild != null) {
|
||||
queue.offer(current.rightChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
TreeASDV tree = new TreeASDV();
|
||||
tree.insert(100);
|
||||
tree.insert(80);
|
||||
tree.insert(90);
|
||||
tree.insert(95);
|
||||
tree.insert(93);
|
||||
tree.insert(70);
|
||||
tree.inOrder();
|
||||
tree.remove(100);
|
||||
|
||||
public int height() {
|
||||
return calculateHeight(root);
|
||||
}
|
||||
|
||||
private int calculateHeight(Node<T> node) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int leftHeight = calculateHeight(node.leftChild);
|
||||
int rightHeight = calculateHeight(node.rightChild);
|
||||
|
||||
return 1 + Math.max(leftHeight, rightHeight);
|
||||
}
|
||||
|
||||
public boolean isFullBST() {
|
||||
int height = height();
|
||||
int nodeCount = countNodes(root);
|
||||
return nodeCount == (1 << height) - 1; // Formula for full binary tree
|
||||
}
|
||||
|
||||
private int countNodes(Node<T> node) {
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
return 1 + countNodes(node.leftChild) + countNodes(node.rightChild);
|
||||
}
|
||||
|
||||
public void inorder() {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<Node<T>> stack = new Stack<>();
|
||||
Node<T> current = root;
|
||||
|
||||
while (current != null || !stack.isEmpty()) {
|
||||
while (current != null) {
|
||||
stack.push(current);
|
||||
current = current.leftChild;
|
||||
}
|
||||
current = stack.pop();
|
||||
System.out.print(current.data + " ");
|
||||
current = current.rightChild;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
TreeASDV<Integer> tree = new TreeASDV<>();
|
||||
// Insert some elements into the tree
|
||||
tree.insert(5);
|
||||
tree.insert(3);
|
||||
tree.insert(7);
|
||||
tree.insert(2);
|
||||
tree.insert(4);
|
||||
tree.insert(6);
|
||||
tree.insert(8);
|
||||
|
||||
// Test breadth-first traversal
|
||||
System.out.println("Breadth-First Traversal:");
|
||||
tree.breadthFirstTraversal();
|
||||
System.out.println();
|
||||
|
||||
// Test height calculation
|
||||
System.out.println("Height of the tree: " + tree.height());
|
||||
|
||||
// Test if the tree is a full binary tree
|
||||
System.out.println("Is the tree a full binary tree? " + tree.isFullBST());
|
||||
|
||||
// Test inorder traversal without recursion
|
||||
System.out.println("Inorder Traversal without Recursion:");
|
||||
tree.inorder();
|
||||
System.out.println();
|
||||
tree.inOrder();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user