https://m7madmomani2.github.io/reading-notes2
A tree is a nonlinear data structure, compared to arrays, linked lists, stacks and queues which are linear data structures. A tree can be empty with no nodes or a tree is a structure consisting of one node called the root and zero or one or more subtrees.
Common Terminology 1) Node - A node is the individual item/data that makes up the data structure 2) Root - The root is the first/top Node in the tree 3) Left Child - The node that is positioned to the left of a root or node 4) Right Child - The node that is positioned to the right of a root or node 5) Edge - The edge in a tree is the link between a parent and child node 6) Leaf - A leaf is a node that does not contain any children 7) Height - The height of a tree is determined by the number of edges from the root to the bottommost node.
Depth first traversal is where we prioritize going through the depth (height) of the tree first. There are multiple ways to carry out depth first traversal, and each method changes the order in which we search/print the root. Here are three methods for depth first traversal:
In all of our examples, we’ve been using a Binary Tree. Trees can have any number of children per node, but Binary Trees restrict the number of children to two (hence our left and right children).
There is no specific sorting order for a binary tree. Nodes can be added into a binary tree wherever space allows. Here is what a binary tree looks like:
A Binary Search Tree (BST) is a type of tree that does have some structure attached to it. In a BST, nodes are organized in a manner where all values that are smaller than the root are placed to the left, and all values that are larger than the root are placed to the right.
Here is how we would change our Binary Tree example into a Binary Search Tree: