7.9.Trees

Definition 7.13 (Tree)

Let G = (V, E) be a graph with the properties
1.
One node is distinguished called root.

2.
Every node c V \ { root } is connected to one other node p called the parent of c.

3.
A tree is connected in the sense, that if we start at any node n other than the root, move to the parent of n, more to the parent of the parent of n, and so on, we reach the root.

G is a tree T.

Definition 7.14 (Child)

If p is the parent of node c, we also say c is a child of p.

A node may have zero or more children, but every node other the root has exactly one parent.

Definition 7.15 (Ancestor, Descendant)

Let < > be a path in a tree T with = root. The nodes 1 i < k, are called an ancestor of and the node , 1 i < k, a descendant of node

Definition 7.16 (Siblings)

Nodes that have the same parent are called siblings.

Definition 7.17 (Leaf)

A leaf is a node of a tree T that has no children.

Definition 7.18 (Height and Depth)

In a tree T the height of a node n is the length of a longest path from n to a leaf. The height of a tree is the height of the root. The depth, or level, of a node n is the length of the path from the root to n.

Definition 7.19 (Labeled Trees)

A labeled tree is a tree in which a label or value is associated with each node of the tree.

Definition 7.20 (Subtree)

In a tree T, a node n together with all of its descendants, if any, is called a subtree of T.

We define binary trees recursivly as follows:

Definition 7.21 (Binary Tree)

The empty tree is a binary tree.

If r is a node and and are binary trees, then is the tree with root r, left subtree and right subtree is a binary tree.

Definition 7.22 (Binary Search Tree)

A binary search tree (BST) is a labeled tree in which the following property holds at every node x in the tree: All nodes of the left subtree of x have labels less than the label of x, and all nodes of the right subtree of x have labels greater than the label of x. The property is called the binary search tree property.

Examples:

• Draw four BST for the nodes: 1, 2, 3, 4, 5, 6