Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures.
Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly under an element are called its children. The element directly above something is called its parent. For example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally, elements with no children are called leaves.
Main applications of trees include:
Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child.
Binary Tree Representation in C: A tree is represented by a pointer to the topmost node in tree. If the tree is empty, then value of root is NULL.
A Tree node contains following parts:
In C, we can represent a tree node using structures. Below is an example of a tree node with an integer data.
Let us create a simple tree with 4 nodes in C. The created tree would be as following.
Summary: Tree is a hierarchical data structure. Main uses of trees include maintaining hierarchical data, providing moderate access and insert/delete operations. Binary trees are special cases of tree where every node has at most two children.
119 docs|30 tests
|
1. What is a binary tree? |
2. What are the properties of a binary tree? |
3. How do binary trees differ from other tree data structures? |
4. What is the time complexity of searching for a specific element in a binary tree? |
5. Can a binary tree have duplicate values? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|