Trees
Last updated
Last updated
A node of a tree is a single element of a tree. A tree is a collection of nodes. A node of a binary tree has a value and two pointers: left and right. A node of a non-binary tree has a value and any number of pointers.
Full: A Full Binary Tree (FBT) is a tree in which every node other than the leaves has two children.
Perfect: A Perfect Binary Tree (PBT) is a tree with all leaf nodes at the same depth. All internal nodes have degree 2.
Complete: A Complete Binary Tree(CBT) is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Child: A child is a node that is a descendant of its parent.
Sibling: A sibling is a node that is a descendant of the same parent.
Leaf: A leaf is a node that has no children.
A binary search tree (BST) is a binary tree in which the value of each node is greater than the values in all of its left descendants and less than the values in all of its right descendants.
The number of nodes in a binary tree with height h is O(2^h). Finding a node in a binary tree with height h is O(h). Therefore, the time complexity of a binary search tree is O(log n), where n is the number of nodes in the tree. The worst case is O(n) when the tree is completely unbalanced.
Comparison of the time complexity of a binary search tree with a linked list:
Insert(): O(1) in a linked list. In a binary search tree, Omega (best case) and Theta (average case) are both (log n). However, worst case is O(n) and Big O measures worst case.
Lookup(): O(n) in a linked list, O(log n) in a binary search tree.
Remove(): O(n) in a linked list, O(log n) in a binary search tree.
The above code creates a empty tree.