190 likes | 270 Views
Topics. Definition and Application of Binary Trees Binary Search Tree Operations. Definition and Application of Binary Trees. Binary tree : a nonlinear data structure in which each node may point to 0, 1, or two other nodes The nodes that a node N points to are the children of N.
E N D
Topics Definition and Application of Binary Trees Binary Search Tree Operations
Definition and Application of Binary Trees • Binary tree: a nonlinear data structure in which each node may point to 0, 1, or two other nodes • The nodes that a node N points to are the children of N NULL NULL NULL NULL NULL NULL
Terminology • If a node N is a child of another node P, then P is called the parent of N • A node that has no children is called a leaf • In a binary tree there is a unique node with no parent, it is called the root of the tree
Binary Tree Terminology • Tree pointer: like a head pointer for a linked list, it points to the first node in the binary tree • Root node: the node with no parent NULL NULL NULL NULL NULL NULL
Binary Tree Terminology • Leaf nodes: nodes that have no children The nodes containing 7 and 43 are leaf nodes 31 19 59 7 NULL 43 NULL NULL NULL NULL NULL
Binary Tree Terminology • Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59 31 19 59 7 43 NULL NULL NULL NULL NULL NULL
Binary Tree Terminology The parent of the node containing 43 is the node containing 59 31 19 59 7 43 NULL NULL NULL NULL NULL NULL
Binary Tree Terminology • A descendant of a node is defined as follows: (1) A node is considered its own descendant (2) A child of a descendant of a node N is a descendant of N So the set of all descendants of a node includes the node itself, its children, the children of the children, etc.
Binary Tree Terminology • A subtree of a binary tree is a part of the tree consisting of all descendants of a given node N • Such a subtree is said to be rooted at N, and N is called the root of the subtree
Subtrees of Binary Trees • A subtree of a binary tree is itself a binary tree • A nonempty binary tree consists of a root node, with the rest of its nodes forming two subtrees, called the left and right subtree
Binary Tree Terminology • The node containing 31 is the root • The nodes containing 19 and 7 form the left subtree • The nodes containing 59 and 43 form the right subtree 31 19 59 7 43 NULL NULL NULL NULL NULL NULL
Binary Tree Node • A node in a binary tree is like a node in a linked list, except it has two node pointer fields: class TreeNode { public: int value; TreeNode *left; TreeNode *right; }; It is convenient to use a constructor to assist in creation of nodes
Binary Tree Operations • Create a binary tree • Insert a node into a binary tree at some position • Delete a node from a binary tree – remove a node and adjust links to preserve the binary tree • Update a node in a binary tree • Retrieve value of a node in a binary tree
Inserting an item into a Binary Tree • If tree is empty, create a new node as root, with empty left and right subtrees • Otherwise, insert the item as a leaf (as either the right or left child), update the links
Since the right subtree is NULL, insert 23 here Inserting an item into a Binary Tree root 65 87 59 7 43 NULL NULL NULL NULL NULL NULL
Traversing a Binary Tree Three traversal methods: • Inorder: • Traverse left subtree of node • Process data in node • Traverse right subtree of node • Preorder: • Process data in node • Traverse left subtree of node • Traverse right subtree of node • Postorder: • Traverse left subtree of node • Traverse right subtree of node • Process data in node
Traversing a Binary Tree 65 87 59 7 43 NULL NULL NULL NULL NULL NULL
Uses of Binary Trees • Binary search tree: a binary tree whose data is organized to simplify searches • Left subtree at each node contains data values less than the data in the node • Right subtree at each node contains values greater than the data in the node 31 19 59 7 NULL 43 NULL NULL NULL NULL NULL
Uses of Binary Trees • Decision Tree: a binary tree whose data is organized to answer yes/no question • Left subtree at each node contains yes responses • Right subtree at each node contains no responses Is it an animal? Does it fly? Flower NULL NULL Dog Bird NULL NULL NULL NULL NULL