From 0ff07294b721ab4585db64c634d173d8b19f14ab Mon Sep 17 00:00:00 2001 From: ShawnN003 Date: Tue, 20 May 2025 11:40:27 -0700 Subject: [PATCH] class finished --- src/Traverse.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/Traverse.java b/src/Traverse.java index 7945513..b91d20c 100644 --- a/src/Traverse.java +++ b/src/Traverse.java @@ -28,6 +28,50 @@ public static void main(String[] args) { v45.neighbors = new ArrayList<>(List.of(v23)); v23.neighbors = new ArrayList<>(List.of()); v67.neighbors = new ArrayList<>(List.of(v91)); + + sum(v3); + } + + public static int sum(Vertex current) + { + Set> visited = new HashSet<>(); + int result = sum(current, visited); + System.out.println(result); + return 0; + } + + public static int sum(Vertex current, Set> visited) + { + if(current == null || visited.contains(current)) return 0; //adding zero at the end of the tree + visited.add(current); //checking for loops + int total = 0; + total += current.data; + + for(Vertex neighbor : current.neighbors) + { + int neighborSum = sum(neighbor,visited); + total+= neighborSum; + } + return total; + } + + public static void traverse(Vertex current){ + Set> myVisited = new HashSet<>(); + traverse(current, myVisited); + } + + + + public static void traverse(Vertex current , Set> visited) + { + if(current == null || visited.contains(current)) return; //ends method when it's null + + System.out.println(current.data); //printing out the data + visited.add(current); //adding the data into a set + for(Vertex neighbor : current.neighbors) //??? + { + traverse(neighbor,visited); + } } }