Docsity

Binary Trees and Binary Search Trees, Summaries of Computer science

Toggle sidebar
bg1
1
Data Structures
Trees
Trees allow us to implement a host of algorithms much faster than using linear data structures, such as arrays or linked lists
A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of
references to other nodes (the “children”)
Root: The node in the tree that does not have a parent, there is only one root in a tree. 1 is the root of the
tree Parent: Any node (except the root) has exactly one edge running upward to another node. The node
above it is called the parent of the node. Node 2 is parent for node 5 and 6
Child: Any node may have one or more lines running downward to other nodes. These nodes below a given
node are called its children.5 and 6 child for node 2
Leaf: or external node: node that does not have children 5,7,10,11,12,3
Ancestors of a node: An ancestor of a node is any other node on the path from the node to the root, this
include parent, grandparent, grand-grandparent,
Subtree: tree consisting of a node and its
descendants
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Binary Trees and Binary Search Trees and more Summaries Computer science in PDF only on Docsity!

Data Structures Trees Trees allow us to implement a host of algorithms much faster than using linear data structures, such as arrays or linked lists A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “children”) Root : The node in the tree that does not have a parent, there is only one root in a tree. 1 is the root of the tree Parent: Any node (except the root) has exactly one edge running upward to another node. The node above it is called the parent of the node. Node 2 is parent for node 5 and 6 Child: Any node may have one or more lines running downward to other nodes. These nodes below a given node are called its children.5 and 6 child for node 2 Leaf: or external node: node that does not have children 5,7,10,11,12, Ancestors of a node: An ancestor of a node is any other node on the path from the node to the root, this include parent, grandparent, grand-grandparent, Subtree: tree consisting of a node and its descendants

Depth of a node: Number of ancestors of that node Height of a tree: The maximum depth Traversing: To traverse a tree means to visit all the nodes in a specified order Edge ✦ It is the link between any two nodes total of Edge=Number of nodes - 1

Binary Tree Implementation Using Linked Structure

• key: An integer value

• data: A value of type E, which can be any type of object

• leftChild: A reference to another Node object, which represents the left child of the

current node

• rightChild: A reference to another Node object, which represents the right child of the

current node

public class BinaryTree<T extends Comparable> { //This line declares a new class called BinaryTree. The class is generic, which means that it can be used to represent binary trees of any type of object that implements the Comparable interface. The Comparable interface defines a single method, compareTo(), which compares two objects of type T and returns an integer value that indicates whether the first object is less than, equal to, or greater than the second object.

Finding Minimum Value in a Tree: Iterative Solution For finding the maximum value, go to the rightmost branch. Finding Minimum Value in a Tree: Recursive Solution For finding the maximum value, go to the rightmost branch To illustrate a binary tree search in Java, let's consider an example where we implement a basic binary search tree (BST) and a search function to find a particular value in the tree. In a binary search tree, each node contains a key and references to its left and right children. For every node, all keys in the left subtree are smaller than the node's key, and all keys in the right subtree are larger.

Max min iterative solution Max min^ recursive^ solution

Insert a new node To insert a node, we must first find the place to insert it. ✤ We follow the path from the root to the appropriate node, which will be the parent of the new node. ✤ When this parent is found, the new node is connected as its left or right child, depending on whether the new node’s key is less or greater than that of the parent.