Class 5: Data Structures 2S2019
Class 5: Data Structures 2S2019
Is a special case of the Tree data structure, in which a node can only have at most 2
children hence the name binary. Since a binary tree can only have 2 nodes they are
usually called left and right child.
Binary Search Tree (BST)
Is a special case of the Binary tree which is Ordered sometimes called Sorted Binary
Tree. In this tree the left subtree of any node only contains nodes with keys lesser than
the node’s key and the right subtree of any node only contains nodes with keys greater
than the node’s key. In this case the nodes “key” is a special attribute of each node that
is used to keep their keys in sorted order, this tree is often used to make searching
more efficiently (fast lookup) hence the name Binary Search Tree. This tree allows every
operation to skip about half of the tree thus making the average complexity of a search
operation of O(log n).
AVL Tree
Is a modification of the Balanced Search Tree (BST) where the difference between
heights of left and right subtrees of any node cannot be more than one for all nodes,
this is referred as “Self-balancing”. AVL trees solve the problem with normal Binary
Search Trees which commonly take O(h) time where h is the height of the Binary each
Tree, by making sure that the height of the tree always remains O(log n) after every
insertion or deletion, we guarantee O(log n) time for all these operations.
■ Height (of a tree): The number of edges between the tree’s root and its furthest leaf.
■ Depth (of a node): The number of edges from the root to the node.
■ Level (of a node): Depth + 1.
Other Trees
■ Red-Black Tree
■ AA Tree
■ Splay Tree
■ B Tree
Tree Traversal
Unlike linear data structures, which can only be traversed in one logical way, Trees can
be traversed in 3 main different ways.
■ Inorder: Left -> Root -> Right
■ Preorder: Root -> Left -> Right
■ Postorder: Left -> Right -> Root
■ Inorder: 1,2,3,5,6,7,8
■ Preorder: 5,2,1,3,7,6,8
■ Postorder: 1,3,2,6,8,7,5