0% found this document useful (0 votes)
13 views20 pages

17 2 Tree Ops

The document covers the fundamentals of binary trees and binary search trees (BST) in Java, including their definitions, properties, and implementations. It details methods for calculating the size and height of binary trees, as well as operations for adding and removing nodes in a BST. Additionally, it explains different types of binary trees such as full, complete, balanced, and degenerate trees.

Uploaded by

nam589hojin12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

17 2 Tree Ops

The document covers the fundamentals of binary trees and binary search trees (BST) in Java, including their definitions, properties, and implementations. It details methods for calculating the size and height of binary trees, as well as operations for adding and removing nodes in a BST. Additionally, it explains different types of binary trees such as full, complete, balanced, and degenerate trees.

Uploaded by

nam589hojin12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

JAVA 17

Fundamentals of Computer Science 2


Chapter 17: Binary Tree

Instructor: Varik Hoang


Java 2

01
JAVA 17

Objectives
— Binary Tree Basics
— Tree Traversals
— Common Tree Operations
— Binary Search Trees

02
JAVA 17

Binary Tree - Size Implementation


public class BinaryTree
{
private TreeNode root;
...
public void size()
{
size(root);
}

private void size(TreeNode current)


{
// base case
if (current == null)
return 0;

// recursive case
int left = size(current.left);
int right = size(current.right);
return 1 + left + right; // count 1 for current
}

class TreeNode { ... }


}

22
JAVA 17

Binary Tree - Height Implementation


public class BinaryTree
{
private TreeNode root;
...
public void height()
{
height(root) - 1; // count edges instead of nodes
}

private void height(TreeNode current)


{
// base case
if (current == null)
return 0;

// recursive case
int left = height(current.left);
int right = height(current.right);
return 1 + (left < right ? right : left); // count 1 for current
}

class TreeNode { ... }


}

23
JAVA 17

Full & Complete & Balanced Binary Tree


— Full: all the nodes have no child or two children
— Complete: all the levels are completely filled except possibly the lowest one, which
is filled from the left
— Balanced: One whose subtrees differ in height by at most 1 and balanced themselves
— Perfect: every node has exact two children and all leaf nodes are at the same level
— Degenerate: every node has one single child even left or right
— Skewed: is a pathological/degenerate tree either dominated by the left nodes or the
right nodes

24
JAVA 17

Binary Search Tree (BST)


— Binary search tree (BST) is a binary tree that is either
— Empty
— A node R such that
— Elements of R's left subtree has value less than R's data
— Elements of R's right subtree has value greater than R's data.
— BSTs store their elements in sorted order

25
JAVA 17

Generic Node
— A single element of a structure such as a BST
— Each node contains:
— one data value
— one reference to the left node
— one reference to the right node

class TreeNode<Type>
{
Type data;
TreeNode<Type> left;
TreeNode<Type> right;
}
...
TreeNode myNode = new TreeNode<Type>();
myNode.data = 18;
myNode.left = null;
myNode.right = null;

26
JAVA 17

Binary Search Tree (BST)


— A binary search tree is a structural parallel to the binary search algorithm
— The algorithm can efficiently search through subtrees divided the data in half
— Left subtree contains items which have values ≤ the value of current node
— Right subtree contains items which have values > the value of current node
— Sometimes we want to allow duplicates:
— The duplicated value appears on the left subtree
— Do nothing since the value is already in the tree
— Increase the counter variable in the node

27
JAVA 17

BST Search

28
JAVA 17

BST Class
public class BST<Type extends Comparable<Type>>
{
private TreeNode<Type> root;

public BST()
{
this.root = null;
}

class TreeNode { ... }


}

29
JAVA 17

BST Add

30
JAVA 17

BST Class - Add Method Implementation


public class BST<Type extends Comparable<Type>>
{
...
public void add(Type value)
{
add(value);
root = add(value, root);
}

private TreeNode<Type> add(Type value, TreeNode<Type> current)


{
if (current == null) // base case
current = new TreeNode<Type>(value);
else if (value.compareTo(current.data) < 0) // recursive case
current.left
add(value, current.left);
= add(value, current.left);
else
current.right
add(value, current.right);
= add(value, current.right);
return current;
}

class TreeNode { ... }


}

31
JAVA 17

BST Class - Remove Method


— Remove leaf (with no child)
— Remove node with only left child
— Remove node with only right child
— Remove node with both children

32
JAVA 17

BST Class - Remove Method (cont.)


— Remove leaf (with no child)
— Remove node with only left child
— Remove node with only right child
— Remove node with both children


Removing -3

33
JAVA 17

BST Class - Remove Method (cont.)


— Remove leaf (with no child)
— Remove node with only left child
— Remove node with only right child
— Remove node with both children


Removing 55

34
JAVA 17

BST Class - Remove Method (cont.)


— Remove leaf (with no child)
— Remove node with only left child
— Remove node with only right child
— Remove node with both children


Removing 29

35
JAVA 17

BST Class - Remove Method (cont.)


— Remove leaf (with no child)
— Remove node with only left child
— Remove node with only right child
— Remove node with both children


Removing 55

36
JAVA 17

BST Class - Remove Method Implementation


... ...
private TreeNode<Type> remove(Type value, TreeNode<Type> current) public void remove(Type value)
{ {
if (current == null) // base case root = remove(value, root);
return null; }

if (value.compareTo(current.data) < 0) // recursively go down private Type findMin(TreeNode current)


current.left = remove(value, current.left); {
else if current.right = remove(value, current.right); Type min = current.data;
else while (current.left != null)
{ {
if (root.left == null) // node with only right child min = current.left.data;
return root.right; current = current.left;
else if (root.right == null) // node with only left child }
return root.left;
return min;
// node with two children -> find in-order successor }
root.data = findMin(root.right); ...
// delete the in-order successor
root.right = remove(root.right, root.data);
}
return current;
}
...
37
JAVA 17

Questions & Answer

38
JAVA 17

39

You might also like