Recap
Linear Data Structures: A linear data structure traverses the data elements sequentially, in which only one data element can directly be reached.
Non-Linear Data Structures:
Every data item is attached to several other data items in a way that is specific for reflecting relationships. The data items are not arranged in a sequential structure.
Examples of Big O notations
Objectives
A tree is a non-linear data structure where a node can have zero or more children. Each node contains a value. Like graphs, the connection between nodes is called edges. A tree is a type of graph, but not all of them are trees
Data structure resembles a tree. It starts with a root node and branch off with its descendants, and finally, there are leaves.
The node abe is the root and bart, lisa and maggie are the leaf nodes of the tree. Notice that the tree’s node can have a different number of descendants: 0, 1, 3, or any other value.
Tree data structures have many applications such as:
Databases
Priority Queues
Querying an LDAP (Lightweight Directory Access Protocol)
Representing the Document Object Model (DOM) for HTML on Websites.
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree.
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it contains.
In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. Preorder traversal is used to create a copy of the tree.
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order.
In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node. Postorder traversal is used to delete the tree.
Trees nodes can have zero or more children. However, when a tree has at the most two children, then it’s called binary tree.
Depending on how nodes are arranged in a binary tree, it can be full, complete and perfect:
The data of all the nodes in the left sub-tree of the root node should be ≤ the data of the root. The data of all the nodes in the right subtree of the root node should be > the data of the root.
In Fig. 1, consider the root node with data = 10.
Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node.
Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node.
To insert a node in a binary tree, we do the following:
Case 1: The node to be deleted is a leaf node
Case 2: The node to be deleted has only one child.
Case 3: The node to be deleted has two children.
The node 50 is to be deleted which is the root node of the tree.
The in-order traversal of the tree given below.
6, 25, 30, 50, 52, 60, 70, 75.
replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree, which will simply be deleted.
It is used to find a specific value in a sorted collection of items by reducing the search scope by half until an item is found or not found. It is used with a sorted collection of items.
Searching for an element in a binary search tree takes O(log2n) time. In worst case, the time it takes to search an element is O(n).
An AVL tree is a subtype of binary search tree. Named after it's inventors Adelson, Velskii and Landis, AVL trees have the property of dynamic self-balancing in addition to all the properties exhibited by binary search trees.
Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the tree will be unbalanced and need to be balanced.
Rotation is the process of moving nodes either to left or to right to make the tree balanced. There are four rotations and they are classified into two types.
In LL Rotation, every node moves one position to left from the current position. To understand LL Rotation, let us consider the following insertion operation in AVL Tree
In RR Rotation, every node moves one position to right from the current position. To understand RR Rotation, let us consider the following insertion operation in AVL Tree.
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation, at first, every node moves one position to the left and one position to right from the current position. To understand LR Rotation, let us consider the following insertion operation in AVL Tree.
The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at first every node moves one position to right and one position to left from the current position. To understand RL Rotation, let us consider the following insertion operation in AVL Tree.
You will do an insertion or deletion similar to a normal Binary Search Tree insertion. After inserting, you fix the AVL property using left or right rotations.
AVL tree controls the height of the binary search tree by not letting it to be skewed. The time taken for all operations in a binary search tree of height h is O(h). However, it can be extended to O(n) if the BST becomes worst case. By limiting this height to log n, AVL tree imposes an upper bound on each operation to be O(log n) where n is the number of nodes.
AVL trees are beneficial in the cases where you are designing some database where insertions and deletions are not that frequent but you have to frequently look-up for the items present in there.
Implement TreeNode Class
Create TreeNode Classes
The tree is a data structure where a node has 0 or more descendants/children.
Tree nodes don’t have cycles (acyclic). If it has cycles, it is a Graph data structure instead.
Trees with two children or less are called: Binary Tree
When a Binary Tree is sorted in a way that the left value is less than the parent and the right children is higher, then and only then we have a Binary Search Tree.
You can visit a tree in a pre/post/in-order fashion.