






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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
public class BinaryTree<T extends Comparable
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.