二叉树(Java)

由于在有序数组中插入数据项太慢,在链表中查找太慢,而树是兼有这两种性能的数据结构。


class treeNode {
	public int iData;
	public double dData;
	public treeNode leftChild;
	public treeNode rightChild;
	
	public void displayNode() {
		System.out.print("{");
		System.out.print(iData);
		System.out.print(",");
		System.out.print(dData);
		System.out.print("}");
	}
}

public class tree {
	private treeNode root;
	
	public tree() {
		root = null;
	}
	
	public treeNode find(int key) {
		treeNode current = root;
		while(current != null) {
			if(current.iData == key) return current;
			else if(current.iData > key) current = current.leftChild;
			else current = current.rightChild;
		}
		return null;
	}
	
	public void insert(int id, double dd) {
		treeNode newNode = new treeNode();
		newNode.iData = id;
		newNode.dData = dd;
		if(root == null)
			root = newNode;
		else {
			treeNode current = root;
			treeNode parent = current;
			while(true) {
				if(current.iData > id) {
					current = current.leftChild;
					if(current == null) {
						parent.leftChild = newNode;
						return;
					}
				}
				else if(current.iData < id) {
					current = current.rightChild;
					if(current == null) {
						parent.rightChild = newNode;
						return;
					}
				}
				else return;		
			}
		}
	}
	
	public void traverse(int traverseType) {
		switch(traverseType) {
		case 1:
			System.out.println("PreOrder:");
			preOrder(root);
			break;
		case 2:
			System.out.println("InOrder:");
			inOrder(root);
			break;
		case 3:
			System.out.println("PostOrder:");
			postOrder(root);
			break;
		default:
			System.out.println("Initial.");
			break;
		}
	}
	
	//中序遍历,左子树-根节点-右子树
	private void inOrder(treeNode localRoot) {
		if(localRoot != null) {
			inOrder(localRoot.leftChild);
			System.out.print(localRoot.iData + " ");
			inOrder(localRoot.rightChild);
		}
	}
	
	//前序遍历,根节点-左子树-右子树
	private void preOrder(treeNode localRoot) {
		if(localRoot != null) {
			System.out.print(localRoot.iData + " ");
			preOrder(localRoot.leftChild);
			preOrder(localRoot.rightChild);
		}
	}
	
	//后序遍历,左子树-右子树-根节点
	private void postOrder(treeNode localRoot) {
		if(localRoot != null) {
			postOrder(localRoot.leftChild);
			postOrder(localRoot.rightChild);
			System.out.print(localRoot.iData + " ");
		}
	}
	
	public boolean delete(int key) {
		treeNode current = root;
		treeNode parent = root;
		boolean isLeftChild = true;
		
		while(current.iData != key) {
			parent = current;
			if(current.iData > key) {
				isLeftChild = true;
				current = current.leftChild;
			}
			else {
				isLeftChild = false;
				current = current.rightChild;
			}
			if(current == null) return false;
		}
		
		//删除的节点是叶子节点,可直接删除
		if(current.leftChild == null && current.rightChild == null) {
			if(current == root) {
				root = null;
			}
			else if(isLeftChild) {
				parent.leftChild = null;
			}
			else
				parent.rightChild = null;
		}
		
		//删除的节点有左子节点,直接将左子节点上移
		else if(current.rightChild == null) {
			if(current == root) 
				root = current.leftChild;
			else if(isLeftChild)
				parent.leftChild = current.leftChild;
			else
				parent.rightChild = current.leftChild;
		}
		
		//删除的节点有右子节点,直接将右子节点上移
		else if(current.leftChild == null) {
			if(current == root)
				root = current.rightChild;
			else if(isLeftChild)
				parent.leftChild = current.rightChild;
			else
				parent.rightChild = current.rightChild;
		}
		
		//删除的节点有左右两个子节点,让后继节点代替
		else {
			treeNode successor = getSuccessor(current);
			if(current == root)
				root = successor;
			else if(isLeftChild)
				parent.leftChild = successor;
			else
				parent.rightChild = successor;
			successor.leftChild = current.leftChild;
		}
		return true;
	}
	
	private treeNode getSuccessor(treeNode delNode) {
		treeNode successorParent = delNode;
		treeNode successor = delNode;
		treeNode current = delNode.rightChild;
		while(current != null) {
			successorParent = successor;
			successor = current;
			current = current.leftChild;
		}
		if(successor != delNode.rightChild) {
			successorParent.leftChild = successor.rightChild;
			successor.rightChild = delNode.rightChild;
		}
		return successor;
	}
	
	public static void main(String[] args) {
		tree t = new tree();
		t.insert(50, 1.5);
		t.insert(25, 1.2);
		t.insert(75, 1.7);
		t.find(50).displayNode();
		t.traverse(2);
		t.traverse(1);
		t.traverse(3);
		t.delete(75);
		t.traverse(2);
	}
}

还看到了两个概念:

满二叉树:除最后一层无任何子节点外,每一层上的所有结点都有两个子结点的二叉树。

完全二叉树:与满二叉树形态类似,但是最后一层并没有满。

### Java二叉树的实现与应用 #### 什么是二叉树二叉树是一种特殊的树形结构,在这种结构中,每个节点最多有两个子节点,通常称为左孩子和右孩子。二叉树具有以下特点: - 每个节点至多有两颗子树。 - 左子树和右子树是有顺序的,次序不能任意颠倒[^1]。 #### Java 中如何定义二叉树节点 在 Java 中可以使用类来表示二叉树的节点。一个典型的二叉树节点类可能如下所示: ```java class TreeNode { int value; TreeNode leftChild; TreeNode rightChild; public TreeNode(int value) { this.value = value; this.leftChild = null; this.rightChild = null; } } ``` 此代码片段展示了创建 `TreeNode` 类的方式,其中包含了整数值以及指向左右孩子的指针[^2]。 #### 创建二叉树类并添加插入方法 为了更方便地操作二叉树,还可以进一步封装成一个完整的二叉树类,并提供一些实用的方法比如插入新节点等。下面是一个简单的例子展示怎样向二叉搜索树(BST)中插入一个新的键值对: ```java public class BinaryTree { private TreeNode root; // 插入新的key-value到二叉搜索树里 public void insert(int key, String name){ root = insertRec(root, key, name); } /* 辅助函数用于递归地构建二叉搜索树 */ private TreeNode insertRec(TreeNode root, int key, String name){ if (root == null){ root = new TreeNode(key); return root; } if (key < root.key) root.left = insertRec(root.left, key,name); else if (key > root.key) root.right = insertRec(root.right,key,name); return root; } } ``` 这段程序实现了通过递归来完成元素插入的功能。 #### 遍历方式 对于任何类型的二叉树来说,遍历都是非常重要的功能之一。常见的三种遍历模式分别是前序遍历、中序遍历和后序遍历。这里给出了一种基于栈的非递归版本的前序遍历实现方案: ```java void preOrderIterative() { Stack<TreeNode> stack = new Stack<>(); while (!stack.isEmpty() || root != null){ if (root!=null){ System.out.print(root.data+" "); stack.push(root); root=root.left; }else{ root=stack.pop().right; } } } ``` 上述代码利用了显式的堆栈来进行迭代访问各个节点的操作[^3]。 #### 应用场景举例 除了作为基础的数据结构外,二叉树还有许多具体的应用场合,例如但不限于: - **路径求和**:寻找从叶子到根节点路径上所有节点之和等于给定目标值的所有路径; - **转换为双向链表**:将一颗二叉查找树转化为按升序排列的双向循环列表; - **获取镜像图像**:计算某棵树关于垂直轴线翻转后的样子; - **层次链接**:把同级之间的兄弟姐妹们串联起来形成额外的关系网; 这些只是冰山一角而已,实际上还有很多其他有趣而有用的用途等待探索。 #### 计算最大深度 要获得一棵二叉树的最大深度,可以通过比较其左右两个分支的高度得出结论。当已知左侧最深深度 l 和右侧最深深度 r 后,则整个树木的整体高度就应该是两者较大者再加上一层,即 max(l,r)+1[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值