Tree

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Tree What is a tree?

A tree is another data structure that you can use to store information. Unlike stacks and queues, which are linear data structures, trees are hierarchical data structures.

Tree ---j <-- root / \ f k / \ \ a h z <-- leaves

When do we use IT? There are many reasons to use a tree to store information. One reason might be because you want to store information that naturally forms a hierarchy. For example, the file system on a computer:

file system ----------/ <-- root / \ ... home / \ ugrad course / / | \ ... cs101 cs112 cs113

Binary Tree (Dyadic tree.) A binary tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data element. The "root" pointer points to the topmost node in the tree. The left and right pointers recursively point to smaller "subtrees" on either side. Binary- used in Boolean

I.

Node

a structure which may contain a value, a condition, or represent a separate data structure (which could be a tree of its own). Each node in a tree has zero or more child nodes, which are below it in the tree (by convention, trees are drawn growing downwards). A node that has a child is called the child's parent node (or ancestor node, or superior). A node has at most one parent. a. An internal node -(also known as an inner node or branch node[1]) is any node of a tree that has child nodes. b. Similarly, an external node (also known as an outer node, leaf node, or terminal node) is any node that does not have child nodes. c. The topmost node in a tree is called the root node. Being the topmost node, the root node will not have a parent. d. Leaf Node - Nodes at the bottommost level of the tree are called leaf nodes. Since they are at the bottommost level, they do not have any children.

II.

Subtrees

A subtree is a portion of a tree data structure that can be viewed as a complete tree in it. Any node in a tree T, together with all the nodes below it, comprises a subtree of T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree. Size The size of a node is the number of descendants it has including itself. Depth/Height The depth of a node n is the length of the path from the root to the node. The set of all nodes at a given depth is sometimes called a level of the tree. The root node is at depth zero.

You might also like