Trees Flashcards
What is a Tree?
A tree is a widely used abstract data type that represents a hierarchical structure with a set of connected nodes. Each node in the tree can be connected to many children, but must be connected to exactly one parent, except for the root node, which has no parent.
Components of a tree?
-A tree is an undirected and connected acyclic graph.
-There are no cycles or loops. Each node can be like the root node of its own subtree, making recursion a useful technique for tree traversal.
Neighbor
Parent or child of a node
Ancestor
A node reachable by traversing its parent chain
Descendant
A node in the node’s subtree
Degree
Number of children of a node
Width
Number of nodes in a level
Level/Depth
Number of edges along the unique path between a node and the root node
Binary tree
Binary means two, so nodes in a binary tree have a maximum of two children.
Complete binary tree
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible.
Balanced binary tree
A binary tree structure in which the left and right subtrees of every node differ in height by no more than 1.
In-order traversa
Left -> Root -> Right
Result: 2, 7, 5, 6, 11, 1, 9, 5, 9
Pre-order traversal
Root -> Left -> Right
Result: 1, 7, 2, 6, 5, 11, 9, 9, 5
Post-order traversal
Left -> Right -> Root
Result: 2, 5, 11, 6, 7, 5, 9, 9, 1
Binary search tree (BST)
In-order traversal of a BST will give you all elements in order.