大话数据结构-树( 七 )

inOrderTraverse() {if (null == root) {return null;}return inOrderTraverse(root);}/*** 中序遍历** @param rootNode 根结点* @return 遍历结果* @author Korbin* @date 2023-01-18 18:21:11**/public List inOrderTraverse(BinaryTreeNode rootNode) {if (null == rootNode) {return null;}List list = new ArrayList<>();BinaryTreeNode leftChild = rootNode.getLeftChild();if (null != leftChild) {list.addAll(inOrderTraverse(leftChild));}list.add(rootNode);BinaryTreeNode rightChild = rootNode.getRightChild();if (null != rightChild) {list.addAll(inOrderTraverse(rightChild));}return list;}/*** 初始化** @author Korbin* @date 2023-01-19 11:08:29**/public void init() {root = new BinaryTreeNode<>();}/*** 在树中结点上 , 添加一个非空树为其左子树或右子树** @param node指定结点* @param left是否左子树 , true为左子树 , false为右子树* @param childTree 待添加的树* @author Korbin* @date 2023-01-18 18:16:13**/public void insert(BinaryTreeNode node, boolean left, BinaryTreeNode childTree) {BinaryTreeNode treeNode = findNode(node, root);if (null != treeNode) {if (left) {treeNode.setLeftChild(childTree);} else {treeNode.setRightChild(childTree);}}}/*** 层序遍历** @return 遍历结果* @author Korbin* @date 2023-01-18 18:21:11**/public List levelOrderTraverse() {if (null == root) {return null;}List list = new ArrayList<>();LinkQueue queue = new LinkQueue<>();queue.init();LinkListNode rootNode = new LinkListNode<>();rootNode.setData(root);queue.insert(rootNode);while (!queue.isEmpty()) {LinkListNode element = queue.delete();BinaryTreeNode node = element.getData();list.add(node);BinaryTreeNode leftChild = node.getLeftChild();if (null != leftChild) {LinkListNode leftNode = new LinkListNode<>();leftNode.setData(leftChild);queue.insert(leftNode);}BinaryTreeNode rightChild = node.getRightChild();if (null != rightChild) {LinkListNode rightNode = new LinkListNode<>();rightNode.setData(rightChild);queue.insert(rightNode);}}return list;}/*** 获得树中结点的双亲 , 若为根节点则返回null** @param node 指定结点* @return 指定结点的双亲* @author Korbin* @date 2023-01-18 18:15:36**/public BinaryTreeNode parent(BinaryTreeNode node) {if (null == node || root.equals(node)) {return null;}return parent(root, node);}/*** 获得树中结点的双亲 , 若为根节点则返回null** @param node指定结点* @param rootNode 根结点* @return 指定结点的双亲* @author Korbin* @date 2023-01-18 18:15:36**/public BinaryTreeNode parent(BinaryTreeNode rootNode, BinaryTreeNode node) {if (null == node || rootNode.equals(node)) {return null;}BinaryTreeNode result = null;BinaryTreeNode leftChild = rootNode.getLeftChild();if (null != leftChild) {if (leftChild.equals(node)) {return rootNode;} else {result = parent(leftChild, node);}}if (null == result) {BinaryTreeNode rightChild = rootNode.getRightChild();if (null != rightChild) {if (rightChild.equals(node)) {return rootNode;} else {result = parent(rightChild, node);}}}return result;}/*** 后序遍历** @return 遍历结果* @author Korbin* @date 2023-01-18 18:21:11**/public List postOrderTraverse() {return postOrderTraverse(root);}/*** 后序遍历** @param rootNode 根结点* @return 遍历结果* @author Korbin* @date 2023-01-20 09:19:10**/public List postOrderTraverse(BinaryTreeNode rootNode) {if (null == rootNode) {return null;}List list = new ArrayList<>();BinaryTreeNode leftChild = rootNode.getLeftChild();if (null != leftChild) {list.addAll(postOrderTraverse(leftChild));}BinaryTreeNode rightChild = rootNode.getRightChild();if (null != rightChild) {list.addAll(postOrderTraverse(rightChild));}list.add(rootNode);return list;}/*** 前序遍历** @return 遍历结果* @author Korbin* @date 2023-01-18 18:21:11**/public List preOrderTraverse() {if (null == root) {return null;}return preOrderTraverse(root);}/*** 前缀遍历查找结点** @param rootNode 根结点* @return 前缀遍历结果* @author Korbin* @date 2023-01-19 18:04:46**/public List